-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
180 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
components/brave_component_updater/browser/component_contents_accessor.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include "brave/components/brave_component_updater/browser/component_contents_accessor.h" | ||
|
||
#include <utility> | ||
#include <vector> | ||
|
||
#include "base/files/file_util.h" | ||
#include "brave/components/brave_component_updater/browser/component_contents_verifier.h" | ||
|
||
namespace component_updater { | ||
|
||
ComponentContentsAccessor::ComponentContentsAccessor( | ||
const base::FilePath& component_root) | ||
: component_root_(component_root), | ||
verifier_(CreateContentsVerifier(component_root)) {} | ||
|
||
ComponentContentsAccessor::~ComponentContentsAccessor() = default; | ||
|
||
// static | ||
scoped_refptr<ComponentContentsAccessor> ComponentContentsAccessor::Create( | ||
const base::FilePath& component_root) { | ||
return base::WrapRefCounted(new ComponentContentsAccessor(component_root)); | ||
} | ||
|
||
const base::FilePath& ComponentContentsAccessor::GetComponentRoot() const { | ||
return component_root_; | ||
} | ||
|
||
std::optional<std::string> ComponentContentsAccessor::GetFileAsString( | ||
const base::FilePath& relative_path) { | ||
std::string contents; | ||
if (!base::ReadFileToString(GetComponentRoot().Append(relative_path), | ||
&contents)) { | ||
return std::nullopt; | ||
} | ||
if (verifier_ && | ||
!verifier_->VerifyContents(relative_path, base::as_byte_span(contents))) { | ||
return std::nullopt; | ||
} | ||
|
||
return contents; | ||
} | ||
|
||
std::optional<std::vector<uint8_t>> ComponentContentsAccessor::GetFileAsBytes( | ||
const base::FilePath& relative_path) { | ||
auto contents = | ||
base::ReadFileToBytes(GetComponentRoot().Append(relative_path)); | ||
if (!contents) { | ||
return std::nullopt; | ||
} | ||
if (!verifier_->VerifyContents(relative_path, | ||
base::as_byte_span(*contents))) { | ||
return std::nullopt; | ||
} | ||
return std::move(*contents); | ||
} | ||
|
||
} // namespace component_updater |
50 changes: 50 additions & 0 deletions
50
components/brave_component_updater/browser/component_contents_accessor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#ifndef BRAVE_COMPONENTS_BRAVE_COMPONENT_UPDATER_BROWSER_COMPONENT_CONTENTS_ACCESSOR_H_ | ||
#define BRAVE_COMPONENTS_BRAVE_COMPONENT_UPDATER_BROWSER_COMPONENT_CONTENTS_ACCESSOR_H_ | ||
|
||
#include <memory> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "base/files/file_path.h" | ||
#include "base/memory/ref_counted.h" | ||
|
||
namespace component_updater { | ||
|
||
// Use on MAY_BLOCK sequence. | ||
class ComponentContentsAccessor | ||
: public base::RefCountedThreadSafe<ComponentContentsAccessor> { | ||
public: | ||
ComponentContentsAccessor(const ComponentContentsAccessor&) = delete; | ||
ComponentContentsAccessor(ComponentContentsAccessor&&) = delete; | ||
ComponentContentsAccessor& operator=(const ComponentContentsAccessor&) = | ||
delete; | ||
ComponentContentsAccessor& operator=(ComponentContentsAccessor&&) = delete; | ||
|
||
static scoped_refptr<ComponentContentsAccessor> Create( | ||
const base::FilePath& component_root); | ||
|
||
const base::FilePath& GetComponentRoot() const; | ||
|
||
std::optional<std::string> GetFileAsString( | ||
const base::FilePath& relative_path); | ||
std::optional<std::vector<uint8_t>> GetFileAsBytes( | ||
const base::FilePath& relative_path); | ||
|
||
private: | ||
friend class base::RefCountedThreadSafe<ComponentContentsAccessor>; | ||
explicit ComponentContentsAccessor(const base::FilePath& component_root); | ||
~ComponentContentsAccessor(); | ||
|
||
const base::FilePath component_root_; | ||
const std::unique_ptr<class ContentsVerifier> verifier_; | ||
}; | ||
|
||
} // namespace component_updater | ||
|
||
#endif // BRAVE_COMPONENTS_BRAVE_COMPONENT_UPDATER_BROWSER_COMPONENT_CONTENTS_ACCESSOR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.