Skip to content
Permalink
Browse files

feat: preliminary support for //extensions (#17440)

  • Loading branch information...
samuelmaddock authored and nornagon committed Jul 24, 2019
1 parent bd526f9 commit 95977291f7eb0ed9c3f57eba3766076a2b0b588b
Showing with 2,483 additions and 36 deletions.
  1. +14 −0 BUILD.gn
  2. +1 −0 buildflags/BUILD.gn
  3. +3 −0 buildflags/buildflags.gni
  4. +29 −0 filenames.gni
  5. +15 −0 shell/browser/api/atom_api_session.cc
  6. +5 −0 shell/browser/api/atom_api_session.h
  7. +9 −3 shell/browser/api/atom_api_web_contents.cc
  8. +53 −1 shell/browser/atom_browser_context.cc
  9. +44 −20 shell/browser/atom_browser_context.h
  10. +25 −0 shell/browser/atom_browser_main_parts.cc
  11. +13 −1 shell/browser/atom_browser_main_parts.h
  12. +55 −0 shell/browser/extensions/api/runtime/atom_runtime_api_delegate.cc
  13. +42 −0 shell/browser/extensions/api/runtime/atom_runtime_api_delegate.h
  14. +25 −0 shell/browser/extensions/atom_browser_context_keyed_service_factories.cc
  15. +18 −0 shell/browser/extensions/atom_browser_context_keyed_service_factories.h
  16. +11 −0 shell/browser/extensions/atom_display_info_provider.cc
  17. +23 −0 shell/browser/extensions/atom_display_info_provider.h
  18. +88 −0 shell/browser/extensions/atom_extension_host_delegate.cc
  19. +52 −0 shell/browser/extensions/atom_extension_host_delegate.h
  20. +158 −0 shell/browser/extensions/atom_extension_loader.cc
  21. +86 −0 shell/browser/extensions/atom_extension_loader.h
  22. +186 −0 shell/browser/extensions/atom_extension_system.cc
  23. +119 −0 shell/browser/extensions/atom_extension_system.h
  24. +51 −0 shell/browser/extensions/atom_extension_system_factory.cc
  25. +41 −0 shell/browser/extensions/atom_extension_system_factory.h
  26. +26 −0 shell/browser/extensions/atom_extension_web_contents_observer.cc
  27. +37 −0 shell/browser/extensions/atom_extension_web_contents_observer.h
  28. +300 −0 shell/browser/extensions/atom_extensions_browser_client.cc
  29. +139 −0 shell/browser/extensions/atom_extensions_browser_client.h
  30. +40 −0 shell/browser/extensions/atom_navigation_ui_data.cc
  31. +48 −0 shell/browser/extensions/atom_navigation_ui_data.h
  32. +5 −0 shell/common/api/features.cc
  33. +35 −0 shell/common/extensions/api/BUILD.gn
  34. +14 −0 shell/common/extensions/api/_manifest_features.json
  35. +67 −0 shell/common/extensions/atom_extensions_api_provider.cc
  36. +39 −0 shell/common/extensions/atom_extensions_api_provider.h
  37. +143 −0 shell/common/extensions/atom_extensions_client.cc
  38. +65 −0 shell/common/extensions/atom_extensions_client.h
  39. +2 −0 shell/renderer/atom_renderer_client.cc
  40. +2 −0 shell/renderer/atom_sandboxed_renderer_client.cc
  41. +65 −0 shell/renderer/extensions/atom_extensions_renderer_client.cc
  42. +51 −0 shell/renderer/extensions/atom_extensions_renderer_client.h
  43. +59 −0 shell/renderer/renderer_client_base.cc
  44. +26 −0 shell/renderer/renderer_client_base.h
  45. +11 −11 spec-main/api-net-log-spec.js
  46. +90 −0 spec-main/extensions-spec.ts
  47. 0 spec-main/fixtures/blank.html
  48. +5 −0 spec-main/fixtures/extensions/chrome-runtime/main.js
  49. +12 −0 spec-main/fixtures/extensions/chrome-runtime/manifest.json
  50. +7 −0 spec-main/fixtures/extensions/chrome-storage/main.js
  51. +15 −0 spec-main/fixtures/extensions/chrome-storage/manifest.json
  52. +1 −0 spec-main/fixtures/extensions/red-bg/main.js
  53. +12 −0 spec-main/fixtures/extensions/red-bg/manifest.json
  54. +1 −0 typings/internal-ambient.d.ts
@@ -627,6 +627,20 @@ source_set("electron_lib") {
if (enable_pepper_flash) {
deps += [ "components/pepper_flash" ]
}

public_deps += [ "shell/common/extensions/api:extensions_features" ]
deps += [
"//components/pref_registry",
"//components/user_prefs",
"//extensions/browser",
"//extensions/browser:core_api_provider",
"//extensions/common",
"//extensions/common:core_api_provider",
"//extensions/renderer",
]
if (enable_electron_extensions) {
sources += filenames.lib_sources_extensions
}
}

electron_paks("packed_resources") {
@@ -17,6 +17,7 @@ buildflag_header("buildflags") {
"ENABLE_PDF_VIEWER=$enable_pdf_viewer",
"ENABLE_TTS=$enable_tts",
"ENABLE_COLOR_CHOOSER=$enable_color_chooser",
"ENABLE_ELECTRON_EXTENSIONS=$enable_electron_extensions",
"OVERRIDE_LOCATION_PROVIDER=$enable_fake_location_provider",
]
}
@@ -26,4 +26,7 @@ declare_args() {

# Enable flash plugin support.
enable_pepper_flash = true

# Enable Chrome extensions support.
enable_electron_extensions = false
}
@@ -604,6 +604,35 @@ filenames = {
"chromium_src/chrome/browser/certificate_manager_model.h",
]

lib_sources_extensions = [
"shell/browser/extensions/api/runtime/atom_runtime_api_delegate.cc",
"shell/browser/extensions/api/runtime/atom_runtime_api_delegate.h",
"shell/browser/extensions/atom_extensions_browser_client.cc",
"shell/browser/extensions/atom_extensions_browser_client.h",
"shell/browser/extensions/atom_browser_context_keyed_service_factories.cc",
"shell/browser/extensions/atom_browser_context_keyed_service_factories.h",
"shell/browser/extensions/atom_display_info_provider.cc",
"shell/browser/extensions/atom_display_info_provider.h",
"shell/browser/extensions/atom_extension_host_delegate.cc",
"shell/browser/extensions/atom_extension_host_delegate.h",
"shell/browser/extensions/atom_extension_loader.cc",
"shell/browser/extensions/atom_extension_loader.h",
"shell/browser/extensions/atom_extension_system.cc",
"shell/browser/extensions/atom_extension_system.h",
"shell/browser/extensions/atom_extension_system_factory.cc",
"shell/browser/extensions/atom_extension_system_factory.h",
"shell/browser/extensions/atom_extension_web_contents_observer.cc",
"shell/browser/extensions/atom_extension_web_contents_observer.h",
"shell/browser/extensions/atom_navigation_ui_data.cc",
"shell/browser/extensions/atom_navigation_ui_data.h",
"shell/common/extensions/atom_extensions_api_provider.cc",
"shell/common/extensions/atom_extensions_api_provider.h",
"shell/common/extensions/atom_extensions_client.cc",
"shell/common/extensions/atom_extensions_client.h",
"shell/renderer/extensions/atom_extensions_renderer_client.cc",
"shell/renderer/extensions/atom_extensions_renderer_client.h",
]

app_sources = [
"shell/app/atom_main.cc",
"shell/app/atom_main.h",
@@ -67,6 +67,10 @@
#include "shell/common/options_switches.h"
#include "ui/base/l10n/l10n_util.h"

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
#include "shell/browser/extensions/atom_extension_system.h"
#endif

using content::BrowserThread;
using content::StoragePartition;

@@ -638,6 +642,14 @@ std::vector<base::FilePath::StringType> Session::GetPreloads() const {
return prefs->preloads();
}

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
void Session::LoadChromeExtension(const base::FilePath extension_path) {
auto* extension_system = static_cast<extensions::AtomExtensionSystem*>(
extensions::ExtensionSystem::Get(browser_context()));
extension_system->LoadExtension(extension_path);
}
#endif

v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
if (cookies_.IsEmpty()) {
auto handle = Cookies::Create(isolate, browser_context());
@@ -745,6 +757,9 @@ void Session::BuildPrototype(v8::Isolate* isolate,
&Session::CreateInterruptedDownload)
.SetMethod("setPreloads", &Session::SetPreloads)
.SetMethod("getPreloads", &Session::GetPreloads)
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
.SetMethod("loadChromeExtension", &Session::LoadChromeExtension)
#endif
.SetProperty("cookies", &Session::Cookies)
.SetProperty("netLog", &Session::NetLog)
.SetProperty("protocol", &Session::Protocol)
@@ -10,6 +10,7 @@

#include "base/values.h"
#include "content/public/browser/download_manager.h"
#include "electron/buildflags/buildflags.h"
#include "native_mate/handle.h"
#include "shell/browser/api/trackable_object.h"
#include "shell/browser/atom_blob_reader.h"
@@ -86,6 +87,10 @@ class Session : public mate::TrackableObject<Session>,
v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
v8::Local<v8::Value> NetLog(v8::Isolate* isolate);

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
void LoadChromeExtension(const base::FilePath extension_path);
#endif

protected:
Session(v8::Isolate* isolate, AtomBrowserContext* browser_context);
~Session() override;
@@ -41,6 +41,7 @@
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/context_menu_params.h"
#include "electron/buildflags/buildflags.h"
#include "electron/shell/common/api/api.mojom.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "native_mate/converter.h"
@@ -112,6 +113,10 @@
#include "components/printing/common/print_messages.h"
#endif

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
#include "shell/browser/extensions/atom_extension_web_contents_observer.h"
#endif

namespace mate {

#if BUILDFLAG(ENABLE_PRINTING)
@@ -456,12 +461,13 @@ void WebContents::InitWithSessionAndOptions(
// Save the preferences in C++.
new WebContentsPreferences(web_contents(), options);

// Initialize permission helper.
WebContentsPermissionHelper::CreateForWebContents(web_contents());
// Initialize security state client.
SecurityStateTabHelper::CreateForWebContents(web_contents());
// Initialize zoom controller.
InitZoomController(web_contents(), options);
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
extensions::AtomExtensionWebContentsObserver::CreateForWebContents(
web_contents());
#endif

registry_.AddInterface(base::BindRepeating(&WebContents::BindElectronBrowser,
base::Unretained(this)));
@@ -10,6 +10,7 @@
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/task/post_task.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/common/chrome_paths.h"
@@ -43,6 +44,22 @@
#include "shell/common/application_info.h"
#include "shell/common/options_switches.h"

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/user_prefs/user_prefs.h"
#include "extensions/browser/browser_context_keyed_service_factories.h"
#include "extensions/browser/extension_pref_store.h"
#include "extensions/browser/extension_pref_value_map_factory.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension_api.h"
#include "shell/browser/extensions/atom_browser_context_keyed_service_factories.h"
#include "shell/browser/extensions/atom_extension_system.h"
#include "shell/browser/extensions/atom_extension_system_factory.h"
#include "shell/browser/extensions/atom_extensions_browser_client.h"
#include "shell/common/extensions/atom_extensions_client.h"
#endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)

using content::BrowserThread;

namespace electron {
@@ -91,6 +108,8 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,

content::BrowserContext::Initialize(this, path_);

BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);

// Initialize Pref Registry.
InitPrefs();

@@ -102,7 +121,15 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,

cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);

BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
this);

extension_system_ = static_cast<extensions::AtomExtensionSystem*>(
extensions::ExtensionSystem::Get(this));
extension_system_->InitForRegularProfile(true /* extensions_enabled */);
extension_system_->FinishInitialization();
#endif
}

AtomBrowserContext::~AtomBrowserContext() {
@@ -131,7 +158,16 @@ void AtomBrowserContext::InitPrefs() {
pref_store->ReadPrefs(); // Synchronous.
prefs_factory.set_user_prefs(pref_store);

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
auto* ext_pref_store = new ExtensionPrefStore(
ExtensionPrefValueMapFactory::GetForBrowserContext(this),
IsOffTheRecord());
prefs_factory.set_extension_prefs(ext_pref_store);

auto registry = WrapRefCounted(new user_prefs::PrefRegistrySyncable);
#else
auto registry = WrapRefCounted(new PrefRegistrySimple);
#endif

registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
base::FilePath());
@@ -144,11 +180,17 @@ void AtomBrowserContext::InitPrefs() {
MediaDeviceIDSalt::RegisterPrefs(registry.get());
ZoomLevelDelegate::RegisterPrefs(registry.get());
PrefProxyConfigTrackerImpl::RegisterPrefs(registry.get());
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
extensions::ExtensionPrefs::RegisterProfilePrefs(registry.get());
#endif

prefs_ = prefs_factory.Create(
registry.get(),
std::make_unique<PrefStoreDelegate>(weak_factory_.GetWeakPtr()));
prefs_->UpdateCommandLinePrefStore(new ValueMapPrefStore);
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
user_prefs::UserPrefs::Set(this, prefs_.get());
#endif
}

void AtomBrowserContext::SetUserAgent(const std::string& user_agent) {
@@ -305,6 +347,16 @@ AtomBrowserContext::GetClientHintsControllerDelegate() {
return nullptr;
}

void AtomBrowserContext::SetCorsOriginAccessListForOrigin(
const url::Origin& source_origin,
std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
base::OnceClosure closure) {
// TODO(nornagon): actually set the CORS access lists. This is called from
// extensions/browser/renderer_startup_helper.cc.
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, std::move(closure));
}

ResolveProxyHelper* AtomBrowserContext::GetResolveProxyHelper() {
if (!resolve_proxy_helper_) {
resolve_proxy_helper_ = base::MakeRefCounted<ResolveProxyHelper>(this);
@@ -15,6 +15,7 @@
#include "chrome/browser/net/proxy_config_monitor.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/resource_context.h"
#include "electron/buildflags/buildflags.h"
#include "shell/browser/media/media_device_id_salt.h"
#include "shell/browser/net/url_request_context_getter.h"

@@ -26,6 +27,12 @@ namespace storage {
class SpecialStoragePolicy;
}

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
namespace extensions {
class AtomExtensionSystem;
}
#endif

namespace electron {

class AtomBlobReader;
@@ -41,6 +48,27 @@ class AtomBrowserContext
: public base::RefCountedDeleteOnSequence<AtomBrowserContext>,
public content::BrowserContext {
public:
// partition_id => browser_context
struct PartitionKey {
std::string partition;
bool in_memory;

PartitionKey(const std::string& partition, bool in_memory)
: partition(partition), in_memory(in_memory) {}

bool operator<(const PartitionKey& other) const {
if (partition == other.partition)
return in_memory < other.in_memory;
return partition < other.partition;
}

bool operator==(const PartitionKey& other) const {
return (partition == other.partition) && (in_memory == other.in_memory);
}
};
using BrowserContextMap =
std::map<PartitionKey, base::WeakPtr<AtomBrowserContext>>;

// Get or create the BrowserContext according to its |partition| and
// |in_memory|. The |options| will be passed to constructor when there is no
// existing BrowserContext.
@@ -49,6 +77,10 @@ class AtomBrowserContext
bool in_memory,
const base::DictionaryValue& options = base::DictionaryValue());

static BrowserContextMap browser_context_map() {
return browser_context_map_;
}

void SetUserAgent(const std::string& user_agent);
std::string GetUserAgent() const;
bool CanUseHttpCache() const;
@@ -84,6 +116,13 @@ class AtomBrowserContext
content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate()
override;

// extensions deps
void SetCorsOriginAccessListForOrigin(
const url::Origin& source_origin,
std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
base::OnceClosure closure) override;

CookieChangeNotifier* cookie_change_notifier() const {
return cookie_change_notifier_.get();
}
@@ -114,26 +153,6 @@ class AtomBrowserContext
// Initialize pref registry.
void InitPrefs();

// partition_id => browser_context
struct PartitionKey {
std::string partition;
bool in_memory;

PartitionKey(const std::string& partition, bool in_memory)
: partition(partition), in_memory(in_memory) {}

bool operator<(const PartitionKey& other) const {
if (partition == other.partition)
return in_memory < other.in_memory;
return partition < other.partition;
}

bool operator==(const PartitionKey& other) const {
return (partition == other.partition) && (in_memory == other.in_memory);
}
};
using BrowserContextMap =
std::map<PartitionKey, base::WeakPtr<AtomBrowserContext>>;
static BrowserContextMap browser_context_map_;

// Self-destructing class responsible for creating URLRequestContextGetter
@@ -162,6 +181,11 @@ class AtomBrowserContext
bool use_cache_ = true;
int max_cache_size_ = 0;

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
// Owned by the KeyedService system.
extensions::AtomExtensionSystem* extension_system_;
#endif

base::WeakPtrFactory<AtomBrowserContext> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);

0 comments on commit 9597729

Please sign in to comment.
You can’t perform that action at this time.