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

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: Fix bugs: 1. Erroneous AttemptLoad() in UserScriptLoader::Observe(); 2. StartLoad()'ing when there … 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:
70 // content::NotificationObserver implementation. 80 // content::NotificationObserver implementation.
71 virtual void Observe(int type, 81 virtual void Observe(int type,
72 const content::NotificationSource& source, 82 const content::NotificationSource& source,
73 const content::NotificationDetails& details) OVERRIDE; 83 const content::NotificationDetails& details) OVERRIDE;
74 84
75 // ExtensionRegistryObserver implementation. 85 // ExtensionRegistryObserver implementation.
76 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
77 const Extension* extension) OVERRIDE;
78 virtual void OnExtensionUnloaded( 86 virtual void OnExtensionUnloaded(
79 content::BrowserContext* browser_context, 87 content::BrowserContext* browser_context,
80 const Extension* extension, 88 const Extension* extension,
81 UnloadedExtensionInfo::Reason reason) OVERRIDE; 89 UnloadedExtensionInfo::Reason reason) OVERRIDE;
82 90
83 // Called when ExtensionSystem is ready. 91 // Initiates script load when we have been waiting for the extension system
84 void OnExtensionsReady(); 92 // to be ready.
93 void OnExtensionSystemReady();
94
95 // Return whether or not it is possible that calls to AddScripts(),
Devlin 2014/08/14 22:07:08 nit: Returns
Mark Dittmer 2014/08/14 22:28:21 Done.
96 // RemoveScripts(), and/or ClearScripts() have caused any real change in the
97 // set of scripts to be loaded.
98 bool ScriptsMayHaveChanged() const;
99
100 // Attempt to initiate a load.
101 void AttemptLoad();
85 102
86 // Called once we have finished loading the scripts on the file thread. 103 // Called once we have finished loading the scripts on the file thread.
87 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, 104 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts,
88 scoped_ptr<base::SharedMemory> shared_memory); 105 scoped_ptr<base::SharedMemory> shared_memory);
89 106
90 // Sends the renderer process a new set of user scripts. If 107 // Sends the renderer process a new set of user scripts. If
91 // |changed_extensions| is not empty, this signals that only the scripts from 108 // |changed_extensions| is not empty, this signals that only the scripts from
92 // those extensions should be updated. Otherwise, all extensions will be 109 // those extensions should be updated. Otherwise, all extensions will be
93 // updated. 110 // updated.
94 void SendUpdate(content::RenderProcessHost* process, 111 void SendUpdate(content::RenderProcessHost* process,
95 base::SharedMemory* shared_memory, 112 base::SharedMemory* shared_memory,
96 const std::set<std::string>& changed_extensions); 113 const std::set<ExtensionId>& changed_extensions);
114
115 // Add to |changed_extensions_| those extensions referred to by |scripts|.
116 void ExpandChangedExtensions(const std::set<UserScript>& scripts);
117
118 // Update |extensions_info_| to contain info for each element of
119 // |changed_extensions_|.
120 void UpdateExtensionsInfo();
97 121
98 bool is_loading() const { 122 bool is_loading() const {
99 // Ownership of |user_scripts_| is passed to the file thread when loading. 123 // Ownership of |user_scripts_| is passed to the file thread when loading.
100 return user_scripts_.get() == NULL; 124 return user_scripts_.get() == NULL;
101 } 125 }
102 126
103 // Manages our notification registrations. 127 // Manages our notification registrations.
104 content::NotificationRegistrar registrar_; 128 content::NotificationRegistrar registrar_;
105 129
106 // Contains the scripts that were found the last time scripts were updated. 130 // Contains the scripts that were found the last time scripts were updated.
107 scoped_ptr<base::SharedMemory> shared_memory_; 131 scoped_ptr<base::SharedMemory> shared_memory_;
108 132
109 // List of scripts from currently-installed extensions we should load. 133 // List of scripts from currently-installed extensions we should load.
110 scoped_ptr<UserScriptList> user_scripts_; 134 scoped_ptr<UserScriptList> user_scripts_;
111 135
112 // Maps extension info needed for localization to an extension ID. 136 // Maps extension info needed for localization to an extension ID.
113 ExtensionsInfo extensions_info_; 137 ExtensionsInfo extensions_info_;
114 138
115 // The IDs of the extensions which have changed since the last update sent to 139 // The mutually-exclusive sets of scripts that were added or removed since the
116 // the renderer. 140 // last script load.
117 std::set<std::string> changed_extensions_; 141 std::set<UserScript> added_scripts_;
142 std::set<UserScript> removed_scripts_;
118 143
119 // The mutually-exclusive sets of extensions that were added or removed since 144 // Indicates whether the the collection of scripts should be cleared before
120 // the last script load. 145 // additions and removals on the next script load.
121 std::set<std::string> added_extensions_; 146 bool clear_scripts_;
122 std::set<std::string> removed_extensions_; 147
148 // The IDs of the extensions which changed in the last update sent to the
149 // renderer.
150 ExtensionIdSet changed_extensions_;
123 151
124 // If the extensions service has finished loading its initial set of 152 // If the extensions service has finished loading its initial set of
125 // extensions. 153 // extensions.
126 bool extensions_service_ready_; 154 bool extension_system_ready_;
127 155
128 // If list of user scripts is modified while we're loading it, we note 156 // If list of user scripts is modified while we're loading it, we note
129 // that we're currently mid-load and then start over again once the load 157 // that we're currently mid-load and then start over again once the load
130 // finishes. This boolean tracks whether another load is pending. 158 // finishes. This boolean tracks whether another load is pending.
131 bool pending_load_; 159 bool pending_load_;
132 160
133 // Whether or not we are currently loading. 161 // Whether or not we are currently loading.
134 bool is_loading_; 162 bool is_loading_;
135 163
136 // The profile for which the scripts managed here are installed. 164 // The profile for which the scripts managed here are installed.
137 Profile* profile_; 165 Profile* profile_;
138 166
139 // Listen to extension load, unloaded notifications. 167 // ID of the extension that owns these scripts, if any. This is only set to a
168 // non-empty value for declarative user script shared memory regions.
169 ExtensionId owner_extension_id_;
170
171 base::WeakPtrFactory<UserScriptLoader> weak_factory_;
172
140 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 173 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
141 extension_registry_observer_; 174 extension_registry_observer_;
142 175
143 base::WeakPtrFactory<UserScriptMaster> weak_factory_; 176 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader);
144
145 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster);
146 }; 177 };
147 178
148 } // namespace extensions 179 } // namespace extensions
149 180
150 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ 181 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698