Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: preliminary support for //extensions #17440

Merged
merged 62 commits into from Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
eb07c49
Add extensions deps
samuelmaddock Mar 15, 2019
ef8fb1c
Include extensions dep
samuelmaddock Mar 15, 2019
94d857f
Add AtomExtensionsRenderClient
samuelmaddock Mar 15, 2019
9e6e25a
Add AtomExtensionsClient
samuelmaddock Mar 15, 2019
4e4b49d
Dispatch scripts on document events
samuelmaddock Mar 15, 2019
60d2849
Add ExtensionsAPIProvider
samuelmaddock Mar 15, 2019
c9f4c2d
Fix compilation errors
samuelmaddock Mar 18, 2019
9636230
Add AtomExtensionsBrowserClient
samuelmaddock Mar 18, 2019
9f3d877
Add remaining browser extensions boilerplate
samuelmaddock Mar 18, 2019
7f77c0e
Add 'load-extension' command line switch to test loading extensions
samuelmaddock Mar 19, 2019
e6602e8
Fix ExtensionPrefs runtime error
samuelmaddock Mar 20, 2019
925e691
Fix content::BrowserContext::SetCorsOriginAccessListForOrigin not imp…
samuelmaddock Mar 20, 2019
7705434
Register 'content_scripts' manifest feature
samuelmaddock Mar 20, 2019
e5ab6f9
Register extension dispatcher callbacks
samuelmaddock Mar 20, 2019
71e3b21
Move extensions files into /extensions subdirectory
samuelmaddock Mar 22, 2019
89956b7
Initialize SharedUserScriptMaster
samuelmaddock Mar 22, 2019
de02b7b
Fix content script injection
samuelmaddock Mar 23, 2019
959b8d2
Add pref_registry dep for PrefRegistrySyncable
samuelmaddock Mar 28, 2019
03b4d02
Add disabled-by-default ENABLE_ELECTRON_EXTENSIONS buildflag
samuelmaddock Mar 29, 2019
164eff4
Fix preprocessor lint errors with extension additions
samuelmaddock Mar 29, 2019
3283c57
Fix build error with including extensions generated code
samuelmaddock Mar 29, 2019
db0892d
Conditionally compile extension code files
samuelmaddock Mar 29, 2019
1060e0e
Move prefs deps behind buildflag
samuelmaddock Mar 29, 2019
ee262db
Ignore gn check on extensions deps
samuelmaddock Mar 29, 2019
3c5d56d
Rename all 'shell_' files
samuelmaddock Apr 1, 2019
8b75d88
Rename Shell to Atom
samuelmaddock Apr 1, 2019
38608c1
Fix lint issues with renamed Shell code
samuelmaddock Apr 1, 2019
0e30490
chore: remove unused header
MarshallOfSound Apr 28, 2019
1610c4b
build: make some extensions targets public deps to remove nogncheck's
MarshallOfSound Apr 28, 2019
69946d1
chore: add CHECK for LoadApp to ensure it is never called
MarshallOfSound Apr 28, 2019
740cd52
chore: remove refs to CHROMEOS in extensions code
MarshallOfSound Apr 28, 2019
adbdb4c
refactor: route AtomExtensionsBrowserClient::IsShuttingDown is atom::…
MarshallOfSound Apr 28, 2019
6204ee9
chore: remove stray TODO comments from extension browser client
MarshallOfSound Apr 28, 2019
4ccea22
chore: route GetNetLog to AtomBrowserClient
MarshallOfSound Apr 28, 2019
96835dc
chore: extensions updates and refactors
MarshallOfSound Apr 28, 2019
7b173ba
chore: route some ExtensionsBrowserClient API calls to BrowserClient …
MarshallOfSound Apr 28, 2019
c6a2f4e
chore: remove unused imports in BUILD.gn
MarshallOfSound Apr 28, 2019
2bc0468
chore: fix indentation in manifest features.json
MarshallOfSound Apr 28, 2019
6fa572b
refactor: make ExtensionsBrowserClient multi context and add session.…
MarshallOfSound Apr 28, 2019
480ec26
chore: fix linting issues with TODO
MarshallOfSound Apr 28, 2019
b373539
fix build
nornagon Jun 3, 2019
3e87d31
remove redundant null assignment in destructor
nornagon Jun 3, 2019
e811818
Merge remote-tracking branch 'origin/master' into samuelmaddock-exten…
nornagon Jul 15, 2019
2270fbe
temporarily allow loading extensions synchronously
nornagon Jul 15, 2019
c794a99
make netlog test lazily access its session
nornagon Jul 15, 2019
f92dd48
beginnings of a test
nornagon Jul 15, 2019
fb16391
Merge remote-tracking branch 'origin/master' into samuelmaddock-exten…
nornagon Jul 17, 2019
86d561c
Merge remote-tracking branch 'origin/master' into samuelmaddock-exten…
nornagon Jul 17, 2019
3ce0838
fix botched merge; popup menus is orthogonal to extensions
nornagon Jul 17, 2019
04905e4
actually test a thing
nornagon Jul 17, 2019
df4d598
fetch original context for services with kServiceRedirectedInIncognit…
nornagon Jul 17, 2019
dd186b1
test that extensions get loaded in the correct session
nornagon Jul 17, 2019
c0e96a3
mark browsercontext as live earlier in initialization
nornagon Jul 22, 2019
5b841f4
add test for chrome.runtime.getManifest()
nornagon Jul 22, 2019
1b5446c
test a couple more simple chrome.runtime APIs
nornagon Jul 22, 2019
072d48c
make chrome.storage work
nornagon Jul 23, 2019
bebf359
add todo for runtime.getPlatformInfo
nornagon Jul 23, 2019
a38a935
address my own review comments
nornagon Jul 23, 2019
984d8bb
remove bad merge bits
nornagon Jul 23, 2019
5114aab
remove extra console.logs
nornagon Jul 23, 2019
8478256
disable extensions by default
nornagon Jul 24, 2019
823dcc3
make deps unconditional
nornagon Jul 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions BUILD.gn
Expand Up @@ -627,6 +627,20 @@ static_library("electron_lib") {
if (enable_pepper_flash) {
deps += [ "components/pepper_flash" ]
}

if (enable_electron_extensions) {
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",
]
sources += filenames.lib_sources_extensions
}
}

electron_paks("packed_resources") {
Expand Down
1 change: 1 addition & 0 deletions buildflags/BUILD.gn
Expand Up @@ -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",
]
}
4 changes: 4 additions & 0 deletions buildflags/buildflags.gni
Expand Up @@ -26,4 +26,8 @@ declare_args() {

# Enable flash plugin support.
enable_pepper_flash = true

# Enable Chrome extensions support
# TODO: Disable before initial merge
enable_electron_extensions = true
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
enable_electron_extensions = true
enable_electron_extensions = false

}
29 changes: 29 additions & 0 deletions filenames.gni
Expand Up @@ -602,6 +602,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",
Expand Down
15 changes: 15 additions & 0 deletions shell/browser/api/atom_api_session.cc
Expand Up @@ -66,6 +66,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;

Expand Down Expand Up @@ -637,6 +641,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());
Expand Down Expand Up @@ -740,6 +752,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)
Expand Down
5 changes: 5 additions & 0 deletions shell/browser/api/atom_api_session.h
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions shell/browser/api/atom_api_web_contents.cc
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
nornagon marked this conversation as resolved.
Show resolved Hide resolved
web_contents());
#endif

registry_.AddInterface(base::BindRepeating(&WebContents::BindElectronBrowser,
base::Unretained(this)));
Expand Down
54 changes: 53 additions & 1 deletion shell/browser/atom_browser_context.cc
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -91,6 +108,8 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,

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

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

// Initialize Pref Registry.
InitPrefs();

Expand All @@ -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() {
Expand Down Expand Up @@ -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());
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
64 changes: 44 additions & 20 deletions shell/browser/atom_browser_context.h
Expand Up @@ -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"

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

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

namespace electron {

class AtomBlobReader;
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down