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

Unified Diff: extensions/browser/user_script_loader.h

Issue 1056533002: Implement <webview>.addContentScript/removeContentScript API [2] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webview_addremove_contentscripts_2
Patch Set: Fix a test. Created 5 years, 8 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: extensions/browser/user_script_loader.h
diff --git a/extensions/browser/user_script_loader.h b/extensions/browser/user_script_loader.h
index c800933cf775d64962eef73abf50b5593788ce83..5c8699ef0af9c2890a9225352c688f5154ef261e 100644
--- a/extensions/browser/user_script_loader.h
+++ b/extensions/browser/user_script_loader.h
@@ -44,33 +44,32 @@ class UserScriptLoader : public content::NotificationObserver {
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(content::BrowserContext* browser_context,
- const HostID& host_id,
- const scoped_refptr<ContentVerifier>& content_verifier);
+ const HostID& host_id);
~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);
-
// Add |scripts| to the set of scripts managed by this loader.
void AddScripts(const std::set<UserScript>& scripts);
+ // Add |scripts| to the set of scripts managed by this loader.
+ // The fetch of the content of the script starts URL request
+ // to the associated render specified by
+ // |render_process_id, render_view_id|.
+ void AddScripts(const std::set<UserScript>& scripts,
+ int render_process_id,
+ int render_view_id);
+
// Remove |scripts| from the set of scripts managed by this loader.
void RemoveScripts(const std::set<UserScript>& scripts);
// Clears the set of scripts managed by this loader.
- void ClearScripts();
+ // If |is_clear| is true, will attempt to load scripts.
+ void ClearScripts(bool is_clear);
// Initiates procedure to start loading scripts on the file thread.
void StartLoad();
@@ -78,32 +77,59 @@ class UserScriptLoader : public content::NotificationObserver {
// Returns true if we have any scripts ready.
bool scripts_ready() const { return shared_memory_.get() != NULL; }
+ // Pickle user scripts and return pointer to the shared memory.
+ static scoped_ptr<base::SharedMemory> Serialize(
+ const extensions::UserScriptList& scripts);
+
protected:
- // Updates |hosts_info_| to contain info for each element of
- // |changed_hosts_|.
- virtual void UpdateHostsInfo(const std::set<HostID>& changed_hosts) = 0;
+ struct UserScriptRenderInfo {
Devlin 2015/04/16 20:19:28 (Almost all of) this is a layering violation. The
Xi Han 2015/04/17 21:44:20 You are right. Refactoring is done to move all the
+ int render_process_id;
+ int render_view_id;
+
+ UserScriptRenderInfo() {
+ render_process_id = -1;
+ render_view_id = -1;
+ }
+
+ UserScriptRenderInfo(int render_process_id, int render_view_id) {
+ this->render_process_id = render_process_id;
+ this->render_view_id = render_view_id;
+ }
- // 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;
+ bool operator<(const UserScriptRenderInfo& other) {
+ if (render_process_id != other.render_process_id)
+ return render_process_id < other.render_process_id;
- // 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);
+ if (render_view_id != other.render_view_id)
+ return render_view_id < other.render_view_id;
- // Removes the entries with the given host_id from the |hosts_info_| map.
- void RemoveHostInfo(const HostID& host_id);
+ return false;
+ }
+ };
+
+ using UserScriptRenderInfoMap = std::map<int, UserScriptRenderInfo>;
+
+ // Allows the derived classes have different ways to load user scripts.
+ virtual void LoadScripts(const std::set<HostID>& changed_hosts,
+ const std::set<int>& added_script_ids) = 0;
+
+ bool GetScriptRenderInfo(int script_id, UserScriptRenderInfo* info);
+ void RemoveScriptRenderInfo(int script_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);
+ // Called once we have finished loading the scripts on the file thread.
+ void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts,
+ scoped_ptr<base::SharedMemory> shared_memory);
+
content::BrowserContext* browser_context() const { return browser_context_; }
const HostID& host_id() const { return host_id_; }
+ // List of scripts from currently-installed extensions we should load.
+ scoped_ptr<UserScriptList> user_scripts_;
+
private:
// content::NotificationObserver implementation.
void Observe(int type,
@@ -118,10 +144,6 @@ class UserScriptLoader : public content::NotificationObserver {
// Attempts to initiate a load.
void AttemptLoad();
- // Called once we have finished loading the scripts on the file thread.
- void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts,
- 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
@@ -141,12 +163,6 @@ class UserScriptLoader : public content::NotificationObserver {
// Contains the scripts that were found the last time scripts were updated.
scoped_ptr<base::SharedMemory> shared_memory_;
- // 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_;
-
// The mutually-exclusive sets of scripts that were added or removed since the
// last script load.
std::set<UserScript> added_scripts_;
@@ -178,8 +194,11 @@ class UserScriptLoader : public content::NotificationObserver {
// non-empty value for declarative user script shared memory regions.
HostID host_id_;
- // Manages content verification of the loaded user scripts.
- scoped_refptr<ContentVerifier> content_verifier_;
+ // Caches the render info of script from WebUI when AddScripts is called.
+ // When starting to load the script, we look up this map to retrieve the
+ // render info. It is used for the script from WebUI only, since the fetch
+ // of script content requires the info of associated render.
+ UserScriptRenderInfoMap script_render_info_map_;
base::WeakPtrFactory<UserScriptLoader> weak_factory_;

Powered by Google App Engine
This is Rietveld 408576698