Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Allow loading cryptotoken extension from pak via brave extension API
Browse files Browse the repository at this point in the history
  • Loading branch information
evq committed Aug 16, 2017
1 parent 3a372c8 commit acf4e8a
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 28 deletions.
2 changes: 2 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ repack("packed_extra_resources") {
"$root_gen_dir/brave/brave_resources.pak",
"$root_gen_dir/brave/brave_strings.pak",
"$root_gen_dir/chrome/common_resources.pak",
"$root_gen_dir/chrome/component_extension_resources.pak",
"$root_gen_dir/blink/public/resources/blink_resources.pak",
"$root_gen_dir/content/app/strings/content_strings_en-US.pak",
"$root_gen_dir/content/browser/tracing/tracing_resources.pak",
Expand All @@ -121,6 +122,7 @@ repack("packed_extra_resources") {
":atom_resources",
":brave_resources",
"app:brave_strings",
"//chrome/browser/resources:component_extension_resources_grit",
"//chrome/common:resources",
"//chrome/renderer:resources",
"//content:resources",
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ source_set("browser") {
"extensions/api/atom_extensions_api_client.h",
"extensions/atom_browser_client_extensions_part.cc",
"extensions/atom_browser_client_extensions_part.h",
"extensions/atom_component_extension_resource_manager.cc",
"extensions/atom_component_extension_resource_manager.h",
"extensions/atom_extension_host_delegate.cc",
"extensions/atom_extension_host_delegate.h",
"extensions/atom_extensions_network_delegate.cc",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "atom/browser/extensions/atom_component_extension_resource_manager.h"

#include "base/logging.h"
#include "base/macros.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "brave/grit/brave_resources.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/grit/component_extension_resources_map.h"

namespace extensions {

AtomComponentExtensionResourceManager::
AtomComponentExtensionResourceManager() {
static const GritResourceMap kExtraComponentExtensionResources[] = {
{"cryptotoken/manifest.json", IDR_CRYPTOTOKEN_MANIFEST},
};

AddComponentResourceEntries(
kComponentExtensionResources,
kComponentExtensionResourcesSize);
AddComponentResourceEntries(
kExtraComponentExtensionResources,
arraysize(kExtraComponentExtensionResources));
}

AtomComponentExtensionResourceManager::
~AtomComponentExtensionResourceManager() {}

bool AtomComponentExtensionResourceManager::IsComponentExtensionResource(
const base::FilePath& extension_path,
const base::FilePath& resource_path,
int* resource_id) const {
base::FilePath directory_path = extension_path;
base::FilePath resources_dir;
base::FilePath relative_path;

if (!PathService::Get(chrome::DIR_RESOURCES, &resources_dir) ||
!resources_dir.AppendRelativePath(directory_path, &relative_path)) {
return false;
}
relative_path = relative_path.Append(resource_path);
relative_path = relative_path.NormalizePathSeparators();

std::map<base::FilePath, int>::const_iterator entry =
path_to_resource_id_.find(relative_path);
if (entry != path_to_resource_id_.end())
*resource_id = entry->second;

return entry != path_to_resource_id_.end();
}

void AtomComponentExtensionResourceManager::AddComponentResourceEntries(
const GritResourceMap* entries,
size_t size) {
for (size_t i = 0; i < size; ++i) {
base::FilePath resource_path = base::FilePath().AppendASCII(
entries[i].name);
resource_path = resource_path.NormalizePathSeparators();

DCHECK(path_to_resource_id_.find(resource_path) ==
path_to_resource_id_.end());
path_to_resource_id_[resource_path] = entries[i].value;
}
}

} // namespace extensions
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ATOM_BROWSER_EXTENSIONS_CHROME_COMPONENT_EXTENSION_RESOURCE_MANAGER_H_
#define ATOM_BROWSER_EXTENSIONS_CHROME_COMPONENT_EXTENSION_RESOURCE_MANAGER_H_

#include <stddef.h>

#include <map>

#include "base/files/file_path.h"
#include "base/macros.h"
#include "extensions/browser/component_extension_resource_manager.h"

struct GritResourceMap;

namespace extensions {

class AtomComponentExtensionResourceManager
: public ComponentExtensionResourceManager {
public:
AtomComponentExtensionResourceManager();
~AtomComponentExtensionResourceManager() override;

// Overridden from ComponentExtensionResourceManager:
bool IsComponentExtensionResource(const base::FilePath& extension_path,
const base::FilePath& resource_path,
int* resource_id) const override;

private:
void AddComponentResourceEntries(const GritResourceMap* entries, size_t size);

// A map from a resource path to the resource ID. Used by
// IsComponentExtensionResource.
std::map<base::FilePath, int> path_to_resource_id_;

DISALLOW_COPY_AND_ASSIGN(AtomComponentExtensionResourceManager);
};

} // namespace extensions

#endif // ATOM_BROWSER_EXTENSIONS_CHROME_COMPONENT_EXTENSION_RESOURCE_MANAGER_H_
19 changes: 1 addition & 18 deletions atom/browser/extensions/atom_extensions_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "atom/browser/browser.h"
#include "atom/browser/extensions/api/atom_extensions_api_client.h"
#include "atom/browser/extensions/atom_component_extension_resource_manager.h"
#include "atom/browser/extensions/atom_extension_host_delegate.h"
#include "atom/browser/extensions/atom_extension_system_factory.h"
#include "atom/browser/extensions/atom_extension_web_contents_observer.h"
Expand Down Expand Up @@ -228,24 +229,6 @@ class AtomRuntimeAPIDelegate : public RuntimeAPIDelegate {
DISALLOW_COPY_AND_ASSIGN(AtomRuntimeAPIDelegate);
};

class AtomComponentExtensionResourceManager
: public ComponentExtensionResourceManager {
public:
AtomComponentExtensionResourceManager() {}
~AtomComponentExtensionResourceManager() override {};

bool IsComponentExtensionResource(
const base::FilePath& extension_path,
const base::FilePath& resource_path,
int* resource_id) const override {
// TODO(bridiver) - bundle extension resources
return false;
};

private:
DISALLOW_COPY_AND_ASSIGN(AtomComponentExtensionResourceManager);
};

AtomExtensionsBrowserClient::AtomExtensionsBrowserClient()
: extension_cache_(new NullExtensionCache()) {
api_client_.reset(new AtomExtensionsAPIClient);
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/extensions/atom_extensions_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>

#include "atom/browser/extensions/atom_component_extension_resource_manager.h"
#include "base/compiler_specific.h"
#include "base/lazy_instance.h"
#include "base/macros.h"
Expand All @@ -29,7 +30,6 @@ class BrowserContext;
namespace extensions {

class AtomProcessManagerDelegate;
class AtomComponentExtensionResourceManager;
class ExtensionsAPIClient;

// Implementation of BrowserClient for Chrome, which includes
Expand Down
2 changes: 2 additions & 0 deletions brave/brave_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<include name="IDR_CHROME_CONTENT_PLUGIN_MANIFEST_OVERLAY" file="..\..\chrome\browser\chrome_content_plugin_manifest_overlay.json" type="BINDATA" />
<include name="IDR_CHROME_CONTENT_RENDERER_MANIFEST_OVERLAY" file="..\..\chrome\browser\chrome_content_renderer_manifest_overlay.json" type="BINDATA" />
<include name="IDR_CHROME_CONTENT_UTILITY_MANIFEST_OVERLAY" file="..\..\chrome\browser\chrome_content_utility_manifest_overlay.json" type="BINDATA" />

<include name="IDR_CRYPTOTOKEN_MANIFEST" file="..\..\chrome\browser\resources\cryptotoken\manifest.json" type="BINDATA" />
</includes>
</release>
</grit>
86 changes: 77 additions & 9 deletions brave/browser/api/brave_api_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
#include "atom/browser/extensions/atom_extension_system.h"
#include "atom/browser/extensions/tab_helper.h"
#include "atom/common/api/event_emitter_caller.h"
#include "brave/common/converters/callback_converter.h"
#include "brave/common/converters/gurl_converter.h"
#include "brave/common/converters/file_path_converter.h"
#include "brave/common/converters/value_converter.h"
#include "atom/common/node_includes.h"
#include "base/files/file_path.h"
#include "base/json/json_string_value_serializer.h"
#include "base/strings/string_util.h"
#include "brave/common/converters/callback_converter.h"
#include "brave/common/converters/file_path_converter.h"
#include "brave/common/converters/gurl_converter.h"
#include "brave/common/converters/value_converter.h"
#include "chrome/common/chrome_paths.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_thread.h"
Expand All @@ -28,6 +30,7 @@
#include "content/public/browser/web_contents.h"
#include "content/public/child/v8_value_converter.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/component_extension_resource_manager.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
Expand All @@ -36,9 +39,14 @@
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension.h"
#include "extensions/common/file_util.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/one_shot_event.h"
#include "extensions/strings/grit/extensions_strings.h"
#include "gin/dictionary.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"


using base::Callback;
using content::BrowserURLHandler;
Expand Down Expand Up @@ -97,10 +105,26 @@ scoped_refptr<extensions::Extension> LoadExtension(const base::FilePath& path,
return NULL;

std::vector<extensions::InstallWarning> warnings;
if (!extensions::file_util::ValidateExtension(extension.get(),
error,
&warnings))
return NULL;

int resource_id;
const extensions::ComponentExtensionResourceManager*
component_extension_resource_manager =
extensions::ExtensionsBrowserClient::Get()
->GetComponentExtensionResourceManager();

if (!(component_extension_resource_manager &&
component_extension_resource_manager->IsComponentExtensionResource(
path,
base::FilePath(extensions::kManifestFilename),
&resource_id))) {
// Component extensions contained inside the resources pak fail manifest validation
// so we skip validation.
if (!extensions::file_util::ValidateExtension(extension.get(),
error,
&warnings)) {
return NULL;
}
}
extension->AddInstallWarnings(warnings);

return extension;
Expand Down Expand Up @@ -154,6 +178,50 @@ Extension::~Extension() {
}
}

std::unique_ptr<base::DictionaryValue> Extension::LoadManifest(
const base::FilePath& extension_root,
std::string* error) {
int resource_id;
const extensions::ComponentExtensionResourceManager*
component_extension_resource_manager =
extensions::ExtensionsBrowserClient::Get()
->GetComponentExtensionResourceManager();

if (component_extension_resource_manager &&
component_extension_resource_manager->IsComponentExtensionResource(
extension_root,
base::FilePath(extensions::kManifestFilename),
&resource_id)) {
const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
base::StringPiece manifest_contents = rb.GetRawDataResource(resource_id);

JSONStringValueDeserializer deserializer(manifest_contents);
std::unique_ptr<base::Value> root(deserializer.Deserialize(NULL, error));
if (!root.get()) {
if (error->empty()) {
// If |error| is empty, than the file could not be read.
// It would be cleaner to have the JSON reader give a specific error
// in this case, but other code tests for a file error with
// error->empty(). For now, be consistent.
*error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE);
} else {
*error = base::StringPrintf(
"%s %s", extensions::manifest_errors::kManifestParseError, error->c_str());
}
return NULL;
}

if (!root->IsType(base::Value::Type::DICTIONARY)) {
*error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID);
return NULL;
}

return base::DictionaryValue::From(std::move(root));
} else {
return extensions::file_util::LoadManifest(extension_root, error);
}
}

void Extension::LoadOnFILEThread(const base::FilePath path,
std::unique_ptr<base::DictionaryValue> manifest,
extensions::Manifest::Location manifest_location,
Expand All @@ -162,7 +230,7 @@ void Extension::LoadOnFILEThread(const base::FilePath path,

std::string error;
if (manifest->empty()) {
manifest = extensions::file_util::LoadManifest(path, &error);
manifest = LoadManifest(path, &error);
}

if (!manifest || !error.empty()) {
Expand Down
2 changes: 2 additions & 0 deletions brave/browser/api/brave_api_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class Extension : public gin::Wrappable<Extension>,
v8::Isolate* isolate_; // not owned
BraveBrowserContext* browser_context_;

std::unique_ptr<base::DictionaryValue> LoadManifest(const base::FilePath& extension_root, std::string* error);

DISALLOW_COPY_AND_ASSIGN(Extension);
};

Expand Down

0 comments on commit acf4e8a

Please sign in to comment.