Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Unified Diff: chrome/browser/extensions/user_script_loader.h

Issue 899983002: Revert of Introduce HostID and de-couple Extensions from "script injection System" [browser side] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/user_script_loader.h
diff --git a/chrome/browser/extensions/user_script_loader.h b/chrome/browser/extensions/user_script_loader.h
index abe2d90bcf7173eef52d1d547c07717d0c37eec2..687bd9c3144aea81f1bc20cca7531a2fb0cdc7fb 100644
--- a/chrome/browser/extensions/user_script_loader.h
+++ b/chrome/browser/extensions/user_script_loader.h
@@ -10,12 +10,13 @@
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
-#include "base/memory/shared_memory.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
-#include "extensions/common/host_id.h"
+#include "extensions/browser/extension_registry_observer.h"
+#include "extensions/common/extension.h"
+#include "extensions/common/extension_set.h"
#include "extensions/common/user_script.h"
namespace base {
@@ -32,6 +33,10 @@ class Profile;
namespace extensions {
class ContentVerifier;
+class ExtensionRegistry;
+
+typedef std::map<ExtensionId, ExtensionSet::ExtensionPathAndDefaultLocale>
+ ExtensionsInfo;
// Manages one "logical unit" of user scripts in shared memory by constructing a
// new shared memory region when the set of scripts changes. Also notifies
@@ -40,30 +45,21 @@ class ContentVerifier;
// of this class are embedded within classes with names ending in
// UserScriptMaster. These "master" classes implement the strategy for which
// scripts to load/unload on this logical unit of scripts.
-class UserScriptLoader : public content::NotificationObserver {
+class UserScriptLoader : public content::NotificationObserver,
+ public ExtensionRegistryObserver {
public:
- using PathAndDefaultLocale = std::pair<base::FilePath, std::string>;
- using HostsInfo = std::map<HostID, PathAndDefaultLocale>;
-
- using SubstitutionMap = std::map<std::string, std::string>;
- using LoadUserScriptsContentFunction =
- base::Callback<bool(const HostID&,
- UserScript::File*,
- const SubstitutionMap*,
- const scoped_refptr<ContentVerifier>&)>;
-
// Parses the includes out of |script| and returns them in |includes|.
static bool ParseMetadataHeader(const base::StringPiece& script_text,
UserScript* script);
- UserScriptLoader(Profile* profile,
- const HostID& host_id,
- const scoped_refptr<ContentVerifier>& content_verifier);
- ~UserScriptLoader() override;
-
// A wrapper around the method to load user scripts, which is normally run on
// the file thread. Exposed only for tests.
- void LoadScriptsForTest(UserScriptList* user_scripts);
+ static void LoadScriptsForTest(UserScriptList* user_scripts);
+
+ UserScriptLoader(Profile* profile,
+ const ExtensionId& owner_extension_id,
+ bool listen_for_extension_system_loaded);
+ ~UserScriptLoader() override;
// Add |scripts| to the set of scripts managed by this loader.
void AddScripts(const std::set<UserScript>& scripts);
@@ -77,47 +73,30 @@ class UserScriptLoader : public content::NotificationObserver {
// Initiates procedure to start loading scripts on the file thread.
void StartLoad();
- // Returns true if we have any scripts ready.
+ // Return true if we have any scripts ready.
bool scripts_ready() const { return shared_memory_.get() != NULL; }
- protected:
- // Updates |hosts_info_| to contain info for each element of
- // |changed_hosts_|.
- virtual void UpdateHostsInfo(const std::set<HostID>& changed_hosts) = 0;
-
- // Returns a function pointer of a static funcion to load user scripts.
- // Derived classes can specify their ways to load scripts in the static
- // function they return.
- // Note: It has to be safe to call multiple times.
- virtual LoadUserScriptsContentFunction GetLoadUserScriptsFunction() = 0;
-
- // Adds the |host_id, location| to the |hosts_info_| map.
- // Only inserts the entry to the map when the given host_id doesn't
- // exists.
- void AddHostInfo(const HostID& host_id, const PathAndDefaultLocale& location);
-
- // Removes the entries with the given host_id from the |hosts_info_| map.
- void RemoveHostInfo(const HostID& host_id);
-
- // Sets the flag if the initial set of hosts has finished loading; if it's
- // set to be true, calls AttempLoad() to bootstrap.
- void SetReady(bool ready);
-
- Profile* profile() const { return profile_; }
- const HostID& host_id() const { return host_id_; }
-
private:
// content::NotificationObserver implementation.
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
+ // ExtensionRegistryObserver implementation.
+ void OnExtensionUnloaded(content::BrowserContext* browser_context,
+ const Extension* extension,
+ UnloadedExtensionInfo::Reason reason) override;
+
+ // Initiates script load when we have been waiting for the extension system
+ // to be ready.
+ void OnExtensionSystemReady();
+
// Returns whether or not it is possible that calls to AddScripts(),
// RemoveScripts(), and/or ClearScripts() have caused any real change in the
// set of scripts to be loaded.
bool ScriptsMayHaveChanged() const;
- // Attempts to initiate a load.
+ // Attempt to initiate a load.
void AttemptLoad();
// Called once we have finished loading the scripts on the file thread.
@@ -125,12 +104,19 @@ class UserScriptLoader : public content::NotificationObserver {
scoped_ptr<base::SharedMemory> shared_memory);
// Sends the renderer process a new set of user scripts. If
- // |changed_hosts| is not empty, this signals that only the scripts from
- // those hosts should be updated. Otherwise, all hosts will be
+ // |changed_extensions| is not empty, this signals that only the scripts from
+ // those extensions should be updated. Otherwise, all extensions will be
// updated.
void SendUpdate(content::RenderProcessHost* process,
base::SharedMemory* shared_memory,
- const std::set<HostID>& changed_hosts);
+ const std::set<ExtensionId>& changed_extensions);
+
+ // Add to |changed_extensions_| those extensions referred to by |scripts|.
+ void ExpandChangedExtensions(const std::set<UserScript>& scripts);
+
+ // Update |extensions_info_| to contain info for each element of
+ // |changed_extensions_|.
+ void UpdateExtensionsInfo();
bool is_loading() const {
// Ownership of |user_scripts_| is passed to the file thread when loading.
@@ -146,8 +132,8 @@ class UserScriptLoader : public content::NotificationObserver {
// List of scripts from currently-installed extensions we should load.
scoped_ptr<UserScriptList> user_scripts_;
- // Maps host info needed for localization to a host ID.
- HostsInfo hosts_info_;
+ // Maps extension info needed for localization to an extension ID.
+ ExtensionsInfo extensions_info_;
// The mutually-exclusive sets of scripts that were added or removed since the
// last script load.
@@ -160,10 +146,11 @@ class UserScriptLoader : public content::NotificationObserver {
// The IDs of the extensions which changed in the last update sent to the
// renderer.
- std::set<HostID> changed_hosts_;
+ ExtensionIdSet changed_extensions_;
- // If the initial set of hosts has finished loading.
- bool ready_;
+ // If the extensions service has finished loading its initial set of
+ // extensions.
+ bool extension_system_ready_;
// If list of user scripts is modified while we're loading it, we note
// that we're currently mid-load and then start over again once the load
@@ -176,12 +163,12 @@ class UserScriptLoader : public content::NotificationObserver {
// The profile for which the scripts managed here are installed.
Profile* profile_;
- // ID of the host that owns these scripts, if any. This is only set to a
+ // ID of the extension that owns these scripts, if any. This is only set to a
// non-empty value for declarative user script shared memory regions.
- HostID host_id_;
+ ExtensionId owner_extension_id_;
- // Manages content verification of the loaded user scripts.
- scoped_refptr<ContentVerifier> content_verifier_;
+ ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
+ extension_registry_observer_;
base::WeakPtrFactory<UserScriptLoader> weak_factory_;
« no previous file with comments | « chrome/browser/extensions/shared_user_script_master.cc ('k') | chrome/browser/extensions/user_script_loader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698