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

Side by Side Diff: chrome/browser/extensions/user_script_loader.h

Issue 822453002: 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: Change ConsumerID to class; remove usages of linked_ptr, and nits. Created 5 years, 11 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_
6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 10
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/shared_memory.h"
13 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h" 15 #include "base/scoped_observer.h"
15 #include "content/public/browser/notification_observer.h" 16 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h" 17 #include "content/public/browser/notification_registrar.h"
17 #include "extensions/browser/extension_registry_observer.h" 18 #include "extensions/common/consumer.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/extension_set.h"
20 #include "extensions/common/user_script.h" 19 #include "extensions/common/user_script.h"
21 20
22 namespace base { 21 namespace base {
23 class SharedMemory; 22 class SharedMemory;
24 } 23 }
25 24
26 namespace content { 25 namespace content {
27 class BrowserContext; 26 class BrowserContext;
28 class RenderProcessHost; 27 class RenderProcessHost;
29 } 28 }
30 29
31 class Profile; 30 class Profile;
32 31
33 namespace extensions { 32 namespace extensions {
34 33
35 class ContentVerifier; 34 class ContentVerifier;
36 class ExtensionRegistry;
37
38 typedef std::map<ExtensionId, ExtensionSet::ExtensionPathAndDefaultLocale>
39 ExtensionsInfo;
40 35
41 // Manages one "logical unit" of user scripts in shared memory by constructing a 36 // Manages one "logical unit" of user scripts in shared memory by constructing a
42 // new shared memory region when the set of scripts changes. Also notifies 37 // new shared memory region when the set of scripts changes. Also notifies
43 // renderers of new shared memory region when new renderers appear, or when 38 // renderers of new shared memory region when new renderers appear, or when
44 // script reloading completes. Script loading lives on the UI thread. Instances 39 // script reloading completes. Script loading lives on the UI thread. Instances
45 // of this class are embedded within classes with names ending in 40 // of this class are embedded within classes with names ending in
46 // UserScriptMaster. These "master" classes implement the strategy for which 41 // UserScriptMaster. These "master" classes implement the strategy for which
47 // scripts to load/unload on this logical unit of scripts. 42 // scripts to load/unload on this logical unit of scripts.
48 class UserScriptLoader : public content::NotificationObserver, 43 class UserScriptLoader : public content::NotificationObserver {
49 public ExtensionRegistryObserver {
50 public: 44 public:
45 using PathAndDefaultLocale = std::pair<base::FilePath, std::string>;
46 using ConsumersInfo = std::map<ConsumerID, PathAndDefaultLocale>;
47
48 using SubstitutionMap = std::map<std::string, std::string>;
49 using LoadUserScriptsFunctionCallback =
50 base::Callback<bool(const ConsumerID&,
51 UserScript::File*,
52 const SubstitutionMap*,
53 scoped_refptr<ContentVerifier>)>;
54
51 // Parses the includes out of |script| and returns them in |includes|. 55 // Parses the includes out of |script| and returns them in |includes|.
52 static bool ParseMetadataHeader(const base::StringPiece& script_text, 56 static bool ParseMetadataHeader(const base::StringPiece& script_text,
53 UserScript* script); 57 UserScript* script);
54 58
59 UserScriptLoader(Profile* profile,
60 const ConsumerID& consumer_id,
61 ContentVerifier* content_verifier = nullptr);
Devlin 2015/01/20 17:51:06 nit: we don't allow default values for arguments.
Xi Han 2015/01/21 21:30:17 Thank for pointing out this.
62 ~UserScriptLoader() override;
63
55 // A wrapper around the method to load user scripts, which is normally run on 64 // A wrapper around the method to load user scripts, which is normally run on
56 // the file thread. Exposed only for tests. 65 // the file thread. Exposed only for tests.
57 static void LoadScriptsForTest(UserScriptList* user_scripts); 66 void LoadScriptsForTest(UserScriptList* user_scripts);
58
59 UserScriptLoader(Profile* profile,
60 const ExtensionId& owner_extension_id,
61 bool listen_for_extension_system_loaded);
62 ~UserScriptLoader() override;
63 67
64 // Add |scripts| to the set of scripts managed by this loader. 68 // Add |scripts| to the set of scripts managed by this loader.
65 void AddScripts(const std::set<UserScript>& scripts); 69 void AddScripts(const std::set<UserScript>& scripts);
66 70
67 // Remove |scripts| from the set of scripts managed by this loader. 71 // Remove |scripts| from the set of scripts managed by this loader.
68 void RemoveScripts(const std::set<UserScript>& scripts); 72 void RemoveScripts(const std::set<UserScript>& scripts);
69 73
70 // Clears the set of scripts managed by this loader. 74 // Clears the set of scripts managed by this loader.
71 void ClearScripts(); 75 void ClearScripts();
72 76
73 // Initiates procedure to start loading scripts on the file thread. 77 // Initiates procedure to start loading scripts on the file thread.
74 void StartLoad(); 78 void StartLoad();
75 79
76 // Return true if we have any scripts ready. 80 // Returns true if we have any scripts ready.
77 bool scripts_ready() const { return shared_memory_.get() != NULL; } 81 bool scripts_ready() const { return shared_memory_.get() != NULL; }
78 82
83 protected:
84 using ConsumerIDSet = std::set<ConsumerID>;
85
86 // Updates |consumers_info_| to contain info for each element of
87 // |changed_consumers_|.
88 virtual void UpdateConsumersInfo(const ConsumerIDSet& changed_consumers) = 0;
89
90 // Returns a function pointer of a static funcion.
91 // Base and derived classes can specify their ways to load user scripts.
92 virtual LoadUserScriptsFunctionCallback GetLoadUserScriptsFunction();
93
94 // Notifies render process and convert |changed_consumers| to specified types
Devlin 2015/01/20 17:51:06 nit: Just "Notifies the render process of any upda
Xi Han 2015/01/21 21:30:17 Done.
95 // of consumers.
96 virtual void SendUpdate(content::RenderProcessHost* process,
97 base::SharedMemoryHandle handle_for_process,
98 const std::set<ConsumerID>& changed_consumers) = 0;
99
100 // Loads user scripts.
101 static bool LoadScriptContent(const ConsumerID& consumer_id,
102 UserScript::File* script_file,
103 const SubstitutionMap* localization_messages,
104 scoped_refptr<ContentVerifier> verifier);
105
106 // Adds the |consumer_id, location| to the consumers_info_ map.
107 // Only inserts the entry to the map when the given consumer_id doesn't
108 // exists.
109 void AddConsumerInfo(const ConsumerID& consumer_id,
110 const PathAndDefaultLocale& location);
111
112 // Removes the entries with the given host_id from the consumers_info_ map.
Devlin 2015/01/20 17:51:06 nit: It's common practice to wrap variable names i
Xi Han 2015/01/21 21:30:17 Updated.
113 void RemoveConsumerInfo(const std::string& host_id);
114
115 // Sets the flag if the initial set of consumers has finished loading; if it's
116 // set to be true, calls AttempLoad() to bootstrap.
117 void SetReady(bool ready);
118
119 Profile* profile() const { return profile_; }
120 const ConsumerID& consumer_id() const { return consumer_id_; }
121
79 private: 122 private:
80 // content::NotificationObserver implementation. 123 // content::NotificationObserver implementation.
81 void Observe(int type, 124 void Observe(int type,
82 const content::NotificationSource& source, 125 const content::NotificationSource& source,
83 const content::NotificationDetails& details) override; 126 const content::NotificationDetails& details) override;
84 127
85 // ExtensionRegistryObserver implementation.
86 void OnExtensionUnloaded(content::BrowserContext* browser_context,
87 const Extension* extension,
88 UnloadedExtensionInfo::Reason reason) override;
89
90 // Initiates script load when we have been waiting for the extension system
91 // to be ready.
92 void OnExtensionSystemReady();
93
94 // Returns whether or not it is possible that calls to AddScripts(), 128 // Returns whether or not it is possible that calls to AddScripts(),
95 // RemoveScripts(), and/or ClearScripts() have caused any real change in the 129 // RemoveScripts(), and/or ClearScripts() have caused any real change in the
96 // set of scripts to be loaded. 130 // set of scripts to be loaded.
97 bool ScriptsMayHaveChanged() const; 131 bool ScriptsMayHaveChanged() const;
98 132
99 // Attempt to initiate a load. 133 // Attempts to initiate a load.
100 void AttemptLoad(); 134 void AttemptLoad();
101 135
102 // Called once we have finished loading the scripts on the file thread. 136 // Called once we have finished loading the scripts on the file thread.
103 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, 137 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts,
104 scoped_ptr<base::SharedMemory> shared_memory); 138 scoped_ptr<base::SharedMemory> shared_memory);
105 139
106 // Sends the renderer process a new set of user scripts. If 140 // Sends the renderer process a new set of user scripts. If
107 // |changed_extensions| is not empty, this signals that only the scripts from 141 // |changed_consumers| is not empty, this signals that only the scripts from
108 // those extensions should be updated. Otherwise, all extensions will be 142 // those consumers should be updated. Otherwise, all consumers will be
109 // updated. 143 // updated.
110 void SendUpdate(content::RenderProcessHost* process, 144 void SendUpdate(content::RenderProcessHost* process,
111 base::SharedMemory* shared_memory, 145 base::SharedMemory* shared_memory,
112 const std::set<ExtensionId>& changed_extensions); 146 const ConsumerIDSet& changed_consumers);
113 147
114 // Add to |changed_extensions_| those extensions referred to by |scripts|. 148 // Adds to |changed_consumers_| those consumers referred to by |scripts|.
115 void ExpandChangedExtensions(const std::set<UserScript>& scripts); 149 void ExpandChangedConsumers(const std::set<UserScript>& scripts);
116
117 // Update |extensions_info_| to contain info for each element of
118 // |changed_extensions_|.
119 void UpdateExtensionsInfo();
120 150
121 bool is_loading() const { 151 bool is_loading() const {
122 // Ownership of |user_scripts_| is passed to the file thread when loading. 152 // Ownership of |user_scripts_| is passed to the file thread when loading.
123 return user_scripts_.get() == NULL; 153 return user_scripts_.get() == NULL;
124 } 154 }
125 155
126 // Manages our notification registrations. 156 // Manages our notification registrations.
127 content::NotificationRegistrar registrar_; 157 content::NotificationRegistrar registrar_;
128 158
129 // Contains the scripts that were found the last time scripts were updated. 159 // Contains the scripts that were found the last time scripts were updated.
130 scoped_ptr<base::SharedMemory> shared_memory_; 160 scoped_ptr<base::SharedMemory> shared_memory_;
131 161
132 // List of scripts from currently-installed extensions we should load. 162 // List of scripts from currently-installed extensions we should load.
133 scoped_ptr<UserScriptList> user_scripts_; 163 scoped_ptr<UserScriptList> user_scripts_;
134 164
135 // Maps extension info needed for localization to an extension ID. 165 // Maps consumer info needed for localization to a host ID.
136 ExtensionsInfo extensions_info_; 166 ConsumersInfo consumers_info_;
137 167
138 // The mutually-exclusive sets of scripts that were added or removed since the 168 // The mutually-exclusive sets of scripts that were added or removed since the
139 // last script load. 169 // last script load.
140 std::set<UserScript> added_scripts_; 170 std::set<UserScript> added_scripts_;
141 std::set<UserScript> removed_scripts_; 171 std::set<UserScript> removed_scripts_;
142 172
143 // Indicates whether the the collection of scripts should be cleared before 173 // Indicates whether the the collection of scripts should be cleared before
144 // additions and removals on the next script load. 174 // additions and removals on the next script load.
145 bool clear_scripts_; 175 bool clear_scripts_;
146 176
147 // The IDs of the extensions which changed in the last update sent to the 177 // The IDs of the extensions which changed in the last update sent to the
148 // renderer. 178 // renderer.
149 ExtensionIdSet changed_extensions_; 179 ConsumerIDSet changed_consumers_;
150 180
151 // If the extensions service has finished loading its initial set of 181 // If the initial set of consumers has finished loading.
152 // extensions. 182 bool ready_;
153 bool extension_system_ready_;
154 183
155 // If list of user scripts is modified while we're loading it, we note 184 // If list of user scripts is modified while we're loading it, we note
156 // that we're currently mid-load and then start over again once the load 185 // that we're currently mid-load and then start over again once the load
157 // finishes. This boolean tracks whether another load is pending. 186 // finishes. This boolean tracks whether another load is pending.
158 bool pending_load_; 187 bool pending_load_;
159 188
160 // Whether or not we are currently loading. 189 // Whether or not we are currently loading.
161 bool is_loading_; 190 bool is_loading_;
162 191
163 // The profile for which the scripts managed here are installed. 192 // The profile for which the scripts managed here are installed.
164 Profile* profile_; 193 Profile* profile_;
165 194
166 // ID of the extension that owns these scripts, if any. This is only set to a 195 // ID of the consumer that owns these scripts, if any. This is only set to a
167 // non-empty value for declarative user script shared memory regions. 196 // non-empty value for declarative user script shared memory regions.
168 ExtensionId owner_extension_id_; 197 ConsumerID consumer_id_;
169 198
170 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 199 // Manages content verification of the loaded user scripts.
171 extension_registry_observer_; 200 ContentVerifier* content_verifier_;
Devlin 2015/01/20 17:51:06 Please store this as a scoped_refptr.
Xi Han 2015/01/21 21:30:17 Done.
172 201
173 base::WeakPtrFactory<UserScriptLoader> weak_factory_; 202 base::WeakPtrFactory<UserScriptLoader> weak_factory_;
174 203
175 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); 204 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader);
176 }; 205 };
177 206
178 } // namespace extensions 207 } // namespace extensions
179 208
180 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ 209 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698