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

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

Issue 420543002: Declarative content scripts: Browser-side: per-extension shared memory regions (lazily loaded) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Yet another round of review comments addressed Created 6 years, 4 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 (c) 2012 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_MASTER_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_
6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 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/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h" 14 #include "base/scoped_observer.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_observer.h" 15 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h" 16 #include "content/public/browser/notification_registrar.h"
18 #include "extensions/browser/extension_registry_observer.h" 17 #include "extensions/browser/extension_registry_observer.h"
19 #include "extensions/common/extension_messages.h" 18 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_set.h" 19 #include "extensions/common/extension_set.h"
21 #include "extensions/common/user_script.h" 20 #include "extensions/common/user_script.h"
22 21
23 namespace base { 22 namespace base {
24 class SharedMemory; 23 class SharedMemory;
25 } 24 }
26 25
27 namespace content { 26 namespace content {
27 class BrowserContext;
28 class RenderProcessHost; 28 class RenderProcessHost;
29 } 29 }
30 30
31 class Profile; 31 class Profile;
32 32
33 namespace extensions { 33 namespace extensions {
34 34
35 class ContentVerifier; 35 class ContentVerifier;
36 class ExtensionRegistry; 36 class ExtensionRegistry;
37 37
38 typedef std::map<std::string, ExtensionSet::ExtensionPathAndDefaultLocale> 38 typedef std::map<ExtensionId, ExtensionSet::ExtensionPathAndDefaultLocale>
39 ExtensionsInfo; 39 ExtensionsInfo;
40 40
41 // Manages a segment of shared memory that contains the user scripts the user 41 // Manages one "logical unit" of user scripts in shared memory by constructing a
42 // has installed. Lives on the UI thread. 42 // new shared memory region when the set of scripts changes. Also notifies
43 class UserScriptMaster : public content::NotificationObserver, 43 // renderers of new shared memory region when new renderers appear, or when
44 // script reloading completes. Script loading lives on the UI thread. Instances
45 // of this class are embedded within classes with names ending in
46 // UserScriptMaster. These "master" classes implement the strategy for which
47 // scripts to load/unload on this logical unit of scripts.
48 class UserScriptLoader : public content::NotificationObserver,
44 public ExtensionRegistryObserver { 49 public ExtensionRegistryObserver {
45 public: 50 public:
46 // Parses the includes out of |script| and returns them in |includes|. 51 // Parses the includes out of |script| and returns them in |includes|.
47 static bool ParseMetadataHeader(const base::StringPiece& script_text, 52 static bool ParseMetadataHeader(const base::StringPiece& script_text,
48 UserScript* script); 53 UserScript* script);
49 54
50 // A wrapper around the method to load user scripts, which is normally run on 55 // A wrapper around the method to load user scripts, which is normally run on
51 // the file thread. Exposed only for tests. 56 // the file thread. Exposed only for tests.
52 static void LoadScriptsForTest(UserScriptList* user_scripts); 57 static void LoadScriptsForTest(UserScriptList* user_scripts);
53 58
54 explicit UserScriptMaster(Profile* profile); 59 UserScriptLoader(Profile* profile,
55 virtual ~UserScriptMaster(); 60 const ExtensionId& owner_extension_id,
61 bool listen_for_extension_system_loaded);
62 virtual ~UserScriptLoader();
56 63
57 // Kicks off a process on the file thread to reload scripts from disk 64 // Add |scripts| to the set of scripts managed by this loader.
58 // into a new chunk of shared memory and notify renderers. 65 void AddScripts(const std::set<UserScript>& scripts);
59 virtual void StartLoad();
60 66
61 // Gets the segment of shared memory for the scripts. 67 // Remove |scripts| from the set of scripts managed by this loader.
62 base::SharedMemory* GetSharedMemory() const { 68 void RemoveScripts(const std::set<UserScript>& scripts);
63 return shared_memory_.get(); 69
64 } 70 // Clears the set of scripts managed by this loader.
71 void ClearScripts();
72
73 // Initiates procedure to start loading scripts on the file thread.
74 void StartLoad();
65 75
66 // Return true if we have any scripts ready. 76 // Return true if we have any scripts ready.
67 bool ScriptsReady() const { return shared_memory_.get() != NULL; } 77 bool scripts_ready() const { return shared_memory_.get() != NULL; }
68 78
69 private: 79 private:
80 // Initiates script load when we have been waiting for the extension system
Devlin 2014/08/07 15:46:32 nit: Move this below the inherited methods.
81 // to be ready.
82 void OnExtensionSystemReady();
83
70 // content::NotificationObserver implementation. 84 // content::NotificationObserver implementation.
71 virtual void Observe(int type, 85 virtual void Observe(int type,
72 const content::NotificationSource& source, 86 const content::NotificationSource& source,
73 const content::NotificationDetails& details) OVERRIDE; 87 const content::NotificationDetails& details) OVERRIDE;
74 88
75 // ExtensionRegistryObserver implementation. 89 // ExtensionRegistryObserver implementation.
76 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
77 const Extension* extension) OVERRIDE;
78 virtual void OnExtensionUnloaded( 90 virtual void OnExtensionUnloaded(
79 content::BrowserContext* browser_context, 91 content::BrowserContext* browser_context,
80 const Extension* extension, 92 const Extension* extension,
81 UnloadedExtensionInfo::Reason reason) OVERRIDE; 93 UnloadedExtensionInfo::Reason reason) OVERRIDE;
82 94
95 // Attempt to initiate a load.
96 void AttemptLoad();
97
83 // Called once we have finished loading the scripts on the file thread. 98 // Called once we have finished loading the scripts on the file thread.
84 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, 99 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts,
85 scoped_ptr<base::SharedMemory> shared_memory); 100 scoped_ptr<base::SharedMemory> shared_memory);
86 101
87 // Sends the renderer process a new set of user scripts. If 102 // Sends the renderer process a new set of user scripts. If
88 // |changed_extensions| is not empty, this signals that only the scripts from 103 // |changed_extensions| is not empty, this signals that only the scripts from
89 // those extensions should be updated. Otherwise, all extensions will be 104 // those extensions should be updated. Otherwise, all extensions will be
90 // updated. 105 // updated.
91 void SendUpdate(content::RenderProcessHost* process, 106 void SendUpdate(content::RenderProcessHost* process,
92 base::SharedMemory* shared_memory, 107 base::SharedMemory* shared_memory,
93 const std::set<std::string>& changed_extensions); 108 const std::set<ExtensionId>& changed_extensions);
109
110 // Update |changed_extensions_| to be the extensions referred to in
111 // |added_scripts_| and |removed_scripts_|.
112 void ComputeChangedExtensions();
113
114 // Update |extensions_info_| to contain info for each element of
115 // |changed_extensions_|.
116 void UpdateExtensionsInfo();
94 117
95 bool is_loading() const { 118 bool is_loading() const {
96 // Ownership of |user_scripts_| is passed to the file thread when loading. 119 // Ownership of |user_scripts_| is passed to the file thread when loading.
97 return user_scripts_.get() == NULL; 120 return user_scripts_.get() == NULL;
98 } 121 }
99 122
100 // Manages our notification registrations. 123 // Manages our notification registrations.
101 content::NotificationRegistrar registrar_; 124 content::NotificationRegistrar registrar_;
102 125
103 // Contains the scripts that were found the last time scripts were updated. 126 // Contains the scripts that were found the last time scripts were updated.
104 scoped_ptr<base::SharedMemory> shared_memory_; 127 scoped_ptr<base::SharedMemory> shared_memory_;
105 128
106 // List of scripts from currently-installed extensions we should load. 129 // Set of scripts from currently-installed extensions we should load.
Devlin 2014/08/07 15:46:32 back to a list. :)
Mark Dittmer 2014/08/11 14:08:04 Done.
107 scoped_ptr<UserScriptList> user_scripts_; 130 scoped_ptr<UserScriptList> user_scripts_;
108 131
109 // Maps extension info needed for localization to an extension ID. 132 // Maps extension info needed for localization to an extension ID.
110 ExtensionsInfo extensions_info_; 133 ExtensionsInfo extensions_info_;
111 134
112 // The IDs of the extensions which have changed since the last update sent to 135 // The mutually-exclusive sets of scripts that were added or removed since the
113 // the renderer. 136 // last script load.
114 std::set<std::string> changed_extensions_; 137 std::set<UserScript> added_scripts_;
138 std::set<UserScript> removed_scripts_;
115 139
116 // The mutually-exclusive sets of extensions that were added or removed since 140 // Indicates whether the the collection of scripts should be cleared before
117 // the last script load. 141 // additions and removals on the next script load.
118 std::set<std::string> added_extensions_; 142 bool clear_scripts_;
119 std::set<std::string> removed_extensions_; 143
144 // The IDs of the extensions which changed in the last update sent to the
145 // renderer.
146 std::set<ExtensionId> changed_extensions_;
Devlin 2014/08/07 15:46:32 We actually have an ExtensionIdSet in extension.h.
Mark Dittmer 2014/08/11 14:08:04 Done.
120 147
121 // If the extensions service has finished loading its initial set of 148 // If the extensions service has finished loading its initial set of
122 // extensions. 149 // extensions.
123 bool extensions_service_ready_; 150 bool extension_system_ready_;
124 151
125 // If list of user scripts is modified while we're loading it, we note 152 // If list of user scripts is modified while we're loading it, we note
126 // that we're currently mid-load and then start over again once the load 153 // that we're currently mid-load and then start over again once the load
127 // finishes. This boolean tracks whether another load is pending. 154 // finishes. This boolean tracks whether another load is pending.
128 bool pending_load_; 155 bool pending_load_;
129 156
130 // Whether or not we are currently loading. 157 // Whether or not we are currently loading.
131 bool is_loading_; 158 bool is_loading_;
132 159
133 // The profile for which the scripts managed here are installed. 160 // The profile for which the scripts managed here are installed.
134 Profile* profile_; 161 Profile* profile_;
135 162
136 // Listen to extension load, unloaded notifications. 163 // ID of the extension that owns these scripts, if any. This is only set to a
164 // non-empty value for declarative user script shared memory regions.
165 ExtensionId owner_extension_id_;
166
167 base::WeakPtrFactory<UserScriptLoader> weak_factory_;
168
137 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 169 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
138 extension_registry_observer_; 170 extension_registry_observer_;
139 171
140 base::WeakPtrFactory<UserScriptMaster> weak_factory_; 172 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader);
141
142 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster);
143 }; 173 };
144 174
145 } // namespace extensions 175 } // namespace extensions
146 176
147 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ 177 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698