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

Add cryptotokenPrivate and device support (hid and usb) for U2F #268

Merged
merged 6 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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 @@ -130,6 +131,7 @@ repack("packed_extra_resources") {
":atom_resources",
":brave_resources",
"app:brave_strings",
"//chrome/browser/resources:component_extension_resources",
"//chrome/common:resources",
"//chrome/renderer:resources",
"//content:resources",
Expand Down
4 changes: 4 additions & 0 deletions atom/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,15 @@ source_set("browser") {
}

if (enable_extensions) {
deps += [ "//electron:brave_resources" ]

sources += [
"extensions/api/atom_extensions_api_client.cc",
"extensions/api/atom_extensions_api_client.h",
"extensions/atom_browser_client_extensions_part.cc",
"extensions/atom_browser_client_extensions_part.h",
"extensions/atom_component_extensions.cc",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this uses brave_resources.h we need to add a dep for brave_resources here too

"extensions/atom_component_extensions.h",
"extensions/atom_extension_host_delegate.cc",
"extensions/atom_extension_host_delegate.h",
"extensions/atom_extensions_network_delegate.cc",
Expand Down
32 changes: 32 additions & 0 deletions atom/browser/extensions/atom_component_extensions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "atom/browser/extensions/atom_component_extensions.h"

#include <map>

#include "base/files/file_path.h"
#include "base/path_service.h"
#include "brave/grit/brave_resources.h"
#include "chrome/common/chrome_paths.h"

static const std::map<base::FilePath, int> componentExtensionManifests = {
{base::FilePath("cryptotoken"), IDR_CRYPTOTOKEN_MANIFEST},
};

bool IsComponentExtension(const base::FilePath& extension_path, int* resource_id) {
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.NormalizePathSeparators();

std::map<base::FilePath, int>::const_iterator entry =
componentExtensionManifests.find(relative_path);

if (entry != componentExtensionManifests.end())
*resource_id = entry->second;

return entry != componentExtensionManifests.end();
}
13 changes: 13 additions & 0 deletions atom/browser/extensions/atom_component_extensions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @file atom_component_extensions.h
* @brief FIXME
*
* @author Evey Quirk
*/
#ifndef _ATOM_COMPONENT_EXTENSIONS_H
#define _ATOM_COMPONENT_EXTENSIONS_H

#include "base/files/file_path.h"

bool IsComponentExtension(const base::FilePath& extension_path, int* resource_id);

#endif /* _ATOM_COMPONENT_EXTENSIONS_H */
37 changes: 18 additions & 19 deletions atom/browser/extensions/atom_extensions_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "base/version.h"
#include "brave/browser/brave_browser_context.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/chrome_component_extension_resource_manager.h"
#include "chrome/browser/extensions/chrome_extension_api_frame_id_map_helper.h"
#include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/renderer_host/chrome_navigation_ui_data.h"
Expand Down Expand Up @@ -71,6 +72,7 @@

#include "electron/brave/common/extensions/api/generated_api_registration.h"
#include "extensions/browser/api/generated_api_registration.h"
#include "chrome/browser/extensions/api/cryptotoken_private/cryptotoken_private_api.h"

namespace {

Expand Down Expand Up @@ -228,29 +230,11 @@ 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);
process_manager_delegate_.reset(new AtomProcessManagerDelegate);
resource_manager_.reset(new AtomComponentExtensionResourceManager);
resource_manager_.reset(new ChromeComponentExtensionResourceManager);
}

AtomExtensionsBrowserClient::~AtomExtensionsBrowserClient() {}
Expand Down Expand Up @@ -439,6 +423,21 @@ void AtomExtensionsBrowserClient::RegisterExtensionFunctions(
ExtensionFunctionRegistry* registry) const {
api::GeneratedFunctionRegistry::RegisterAll(registry);
api::BraveGeneratedFunctionRegistry::RegisterAll(registry);

// Instead of registering all Chrome extension functions,
// via api::ChromeGeneratedFunctionRegistry::RegisterAll(registry)
// explicitly register just the ones we want
constexpr ExtensionFunctionRegistry::FactoryEntry chromeEntries[] = {
{
&NewExtensionFunction<api::CryptotokenPrivateCanOriginAssertAppIdFunction>,
api::CryptotokenPrivateCanOriginAssertAppIdFunction::function_name(),
api::CryptotokenPrivateCanOriginAssertAppIdFunction::histogram_value(),
},
};

for (const auto& entry : chromeEntries) {
registry->Register(entry);
}
}

std::unique_ptr<RuntimeAPIDelegate>
Expand Down
4 changes: 2 additions & 2 deletions atom/browser/extensions/atom_extensions_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/chrome_component_extension_resource_manager.h"
#include "extensions/browser/extension_host_delegate.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/kiosk/kiosk_delegate.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 Expand Up @@ -139,7 +139,7 @@ class AtomExtensionsBrowserClient : public ExtensionsBrowserClient {
// Client for API implementations.
std::unique_ptr<ExtensionsAPIClient> api_client_;

std::unique_ptr<AtomComponentExtensionResourceManager> resource_manager_;
std::unique_ptr<ChromeComponentExtensionResourceManager> resource_manager_;

std::unique_ptr<ExtensionCache> extension_cache_;

Expand Down
6 changes: 5 additions & 1 deletion atom/common/api/resources/windows_bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ binding.registerCustomHook(function (bindingsAPI, extensionId) {
})

apiFunctions.setHandleRequest('get', function (windowId, getInfo, cb) {
console.warn('chrome.windows.get is not supported yet')
var responseId = ipc.guid()
ipc.once('chrome-windows-get-response-' + responseId, function (evt, win) {
cb(win)
})
ipc.send('chrome-windows-get', responseId, windowId, getInfo)
})

apiFunctions.setHandleRequest('create', function (createData, cb) {
Expand Down
1 change: 1 addition & 0 deletions brave/brave_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<include name="IDR_CHROME_CONTENT_PLUGIN_MANIFEST_OVERLAY" file="${root_gen_dir}\chrome\app\chrome_content_plugin_manifest_overlay.json" type="BINDATA" />
<include name="IDR_CHROME_CONTENT_RENDERER_MANIFEST_OVERLAY" file="${root_gen_dir}\chrome\app\chrome_content_renderer_manifest_overlay.json" type="BINDATA" />
<include name="IDR_CHROME_CONTENT_UTILITY_MANIFEST_OVERLAY" file="${root_gen_dir}\chrome\app\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>
69 changes: 60 additions & 9 deletions brave/browser/api/brave_api_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
#include <string>
#include <vector>

#include "atom/browser/extensions/atom_component_extensions.h"
#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 +31,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 @@ -37,9 +41,14 @@
#include "extensions/common/extension.h"
#include "extensions/common/disable_reason.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 @@ -98,10 +107,17 @@ 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;
if (!IsComponentExtension(path, &resource_id)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use IsComponentExtensionResource here instead? IsComponentExtension is a bit misleading because we have component extensions that are not loaded from resource paks

// 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 @@ -155,6 +171,41 @@ Extension::~Extension() {
}
}

std::unique_ptr<base::DictionaryValue> Extension::LoadManifest(
const base::FilePath& extension_root,
std::string* error) {
int resource_id;
if (IsComponentExtension(extension_root, &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 @@ -163,7 +214,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
8 changes: 8 additions & 0 deletions chromium_src/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ source_set("browser") {
"chrome/browser/background/background_mode_manager.cc",
"//chrome/browser/background/background_mode_manager.h",

"//chrome/browser/chrome_device_client.cc",
"//chrome/browser/chrome_device_client.h",

# extensions
"//chrome/browser/extensions/global_shortcut_listener.cc",
"//chrome/browser/extensions/global_shortcut_listener.h",
Expand Down Expand Up @@ -504,10 +507,14 @@ source_set("browser") {

if (enable_extensions) {
sources += [
"//chrome/browser/extensions/chrome_component_extension_resource_manager.cc",
"//chrome/browser/extensions/chrome_component_extension_resource_manager.h",
"chrome/browser/extensions/extension_util.cc",
"//chrome/browser/extensions/extension_util.h",
"chrome/browser/extensions/extension_tab_util.cc",
"//chrome/browser/extensions/extension_tab_util.h",
"//chrome/browser/extensions/api/cryptotoken_private/cryptotoken_private_api.cc",
"//chrome/browser/extensions/api/cryptotoken_private/cryptotoken_private_api.h",
"chrome/browser/extensions/api/messaging/incognito_connectability.cc",
"//chrome/browser/extensions/api/messaging/incognito_connectability.h",
"//chrome/browser/extensions/api/messaging/chrome_messaging_delegate.cc",
Expand Down Expand Up @@ -536,6 +543,7 @@ source_set("browser") {
"//chrome/browser/extensions/api/tabs/tabs_constants.h",
"//chrome/browser/extensions/chrome_extension_api_frame_id_map_helper.cc",
"//chrome/browser/extensions/chrome_extension_api_frame_id_map_helper.h",
"chrome/browser/extensions/chrome_extension_function_details.cc",
"//chrome/browser/extensions/event_router_forwarder.cc",
"//chrome/browser/extensions/event_router_forwarder.h",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 "chrome/browser/extensions/chrome_extension_function_details.h"

#include "extensions/browser/extension_function.h"

ChromeExtensionFunctionDetails::ChromeExtensionFunctionDetails(
UIThreadExtensionFunction* function)
: function_(function) {
}

ChromeExtensionFunctionDetails::~ChromeExtensionFunctionDetails() {
}
Loading