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

Side by Side Diff: chrome/browser/extensions/user_script_master.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: Refactor relationship between UserScriptMaster and its subclasses 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 (c) 2012 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_MASTER_H_
6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string>
10 9
11 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_observer.h" 14 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h" 15 #include "content/public/browser/notification_registrar.h"
18 #include "extensions/browser/extension_registry_observer.h"
19 #include "extensions/common/extension_messages.h" 16 #include "extensions/common/extension_messages.h"
20 #include "extensions/common/extension_set.h" 17 #include "extensions/common/extension_set.h"
21 #include "extensions/common/user_script.h" 18 #include "extensions/common/user_script.h"
22 19
23 namespace base { 20 namespace base {
24 class SharedMemory; 21 class SharedMemory;
25 } 22 }
26 23
27 namespace content { 24 namespace content {
28 class RenderProcessHost; 25 class RenderProcessHost;
29 } 26 }
30 27
31 class Profile; 28 class Profile;
32 29
33 namespace extensions { 30 namespace extensions {
34 31
35 class ContentVerifier; 32 class ContentVerifier;
36 class ExtensionRegistry; 33 class ExtensionRegistry;
37 34
38 typedef std::map<std::string, ExtensionSet::ExtensionPathAndDefaultLocale> 35 typedef std::map<ExtensionId, ExtensionSet::ExtensionPathAndDefaultLocale>
39 ExtensionsInfo; 36 ExtensionsInfo;
40 37
41 // Manages a segment of shared memory that contains the user scripts the user 38 // Manages a segment of shared memory that contains the user scripts the user
42 // has installed. Lives on the UI thread. 39 // has installed. Lives on the UI thread.
43 class UserScriptMaster : public content::NotificationObserver, 40 class UserScriptMaster : public content::NotificationObserver {
44 public ExtensionRegistryObserver {
45 public: 41 public:
46 // Parses the includes out of |script| and returns them in |includes|. 42 // Parses the includes out of |script| and returns them in |includes|.
47 static bool ParseMetadataHeader(const base::StringPiece& script_text, 43 static bool ParseMetadataHeader(const base::StringPiece& script_text,
48 UserScript* script); 44 UserScript* script);
49 45
50 // A wrapper around the method to load user scripts, which is normally run on 46 // A wrapper around the method to load user scripts, which is normally run on
51 // the file thread. Exposed only for tests. 47 // the file thread. Exposed only for tests.
52 static void LoadScriptsForTest(UserScriptList* user_scripts); 48 static void LoadScriptsForTest(UserScriptList* user_scripts);
53 49
54 explicit UserScriptMaster(Profile* profile); 50 explicit UserScriptMaster(Profile* profile, ExtensionId owner_extension_id);
55 virtual ~UserScriptMaster(); 51 virtual ~UserScriptMaster();
56 52
57 // Kicks off a process on the file thread to reload scripts from disk 53 // content::NotificationObserver implementation.
58 // into a new chunk of shared memory and notify renderers. 54 virtual void Observe(int type,
59 virtual void StartLoad(); 55 const content::NotificationSource& source,
56 const content::NotificationDetails& details) OVERRIDE;
60 57
61 // Gets the segment of shared memory for the scripts. 58 // Gets the segment of shared memory for the scripts.
62 base::SharedMemory* GetSharedMemory() const { 59 base::SharedMemory* GetSharedMemory() const {
63 return shared_memory_.get(); 60 return shared_memory_.get();
64 } 61 }
65 62
66 // Return true if we have any scripts ready. 63 // Return true if we have any scripts ready.
67 bool ScriptsReady() const { return shared_memory_.get() != NULL; } 64 bool ScriptsReady() const { return shared_memory_.get() != NULL; }
68 65
69 private:
70 // content::NotificationObserver implementation.
71 virtual void Observe(int type,
72 const content::NotificationSource& source,
73 const content::NotificationDetails& details) OVERRIDE;
74
75 // ExtensionRegistryObserver implementation.
76 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
77 const Extension* extension) OVERRIDE;
78 virtual void OnExtensionUnloaded(
79 content::BrowserContext* browser_context,
80 const Extension* extension,
81 UnloadedExtensionInfo::Reason reason) OVERRIDE;
82
83 // Called once we have finished loading the scripts on the file thread. 66 // Called once we have finished loading the scripts on the file thread.
84 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, 67 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts,
85 scoped_ptr<base::SharedMemory> shared_memory); 68 scoped_ptr<base::SharedMemory> shared_memory);
86 69
70 // Wrapper around LoadScripts() that that does bookkeeping and invokes
71 // LoadScripts() with the correct parameters.
72 virtual void StartLoad() = 0;
73
74 protected:
75 // Takes ownership of user script list.
76 virtual void AcquireUserScripts(scoped_ptr<UserScriptList> scripts) = 0;
77
78 // Get extension info related to extensions managed by this master.
79 virtual ExtensionsInfo GetExtensionsInfo() = 0;
80
81 // Gets a set of extensions that need to be reloaded on the file thread.
82 virtual std::set<ExtensionId> GetAddedExtensions() = 0;
83
84 // Gets a set of all extensions managed by this master. The empty set
85 // indicates "all extensions in the system".
86 virtual std::set<ExtensionId> GetAllManagedExtensions() = 0;
87
88 // Gets a set of extensions that have changed since the last
89 // ResetChangedExtensions().
90 virtual std::set<ExtensionId> GetChangedExtensions() = 0;
91
92 // Resets any bookkeeping for tracking the set of changed extensions.
93 virtual void ResetChangedExtensions() = 0;
94
95 // Kicks off a process on the file thread to reload scripts from disk
96 // into a new chunk of shared memory and notify renderers.
97 void LoadScripts(scoped_ptr<UserScriptList> scripts,
98 const ExtensionsInfo& extensions_info);
99
87 // Sends the renderer process a new set of user scripts. If 100 // Sends the renderer process a new set of user scripts. If
88 // |changed_extensions| is not empty, this signals that only the scripts from 101 // |changed_extensions| is not empty, this signals that only the scripts from
89 // those extensions should be updated. Otherwise, all extensions will be 102 // those extensions should be updated. Otherwise, all extensions will be
90 // updated. 103 // updated.
91 void SendUpdate(content::RenderProcessHost* process, 104 void SendUpdate(content::RenderProcessHost* process,
92 base::SharedMemory* shared_memory, 105 base::SharedMemory* shared_memory,
93 const std::set<std::string>& changed_extensions); 106 const std::set<ExtensionId>& changed_extensions);
94 107
95 bool is_loading() const { 108 void SignalPendingLoad() { pending_load_ = true; }
96 // Ownership of |user_scripts_| is passed to the file thread when loading.
97 return user_scripts_.get() == NULL;
98 }
99 109
100 // Manages our notification registrations. 110 Profile* profile() { return profile_; }
101 content::NotificationRegistrar registrar_;
102 111
103 // Contains the scripts that were found the last time scripts were updated. 112 const ExtensionId& owner_extension_id() const { return owner_extension_id_; }
104 scoped_ptr<base::SharedMemory> shared_memory_;
105 113
106 // List of scripts from currently-installed extensions we should load. 114 private:
107 scoped_ptr<UserScriptList> user_scripts_;
108
109 // Maps extension info needed for localization to an extension ID.
110 ExtensionsInfo extensions_info_;
111
112 // The IDs of the extensions which have changed since the last update sent to
113 // the renderer.
114 std::set<std::string> changed_extensions_;
115
116 // The mutually-exclusive sets of extensions that were added or removed since
117 // the last script load.
118 std::set<std::string> added_extensions_;
119 std::set<std::string> removed_extensions_;
120
121 // If the extensions service has finished loading its initial set of
122 // extensions.
123 bool extensions_service_ready_;
124
125 // If list of user scripts is modified while we're loading it, we note 115 // 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 116 // that we're currently mid-load and then start over again once the load
127 // finishes. This boolean tracks whether another load is pending. 117 // finishes. This boolean tracks whether another load is pending.
128 bool pending_load_; 118 bool pending_load_;
129 119
120 // The profile for which the scripts managed here are installed.
121 Profile* profile_;
122
123 base::WeakPtrFactory<UserScriptMaster> weak_factory_;
124
125 // ID of extension that owns this region, if any. This is the empty string in
126 // the case of the memory region for statically declared content scripts,
127 // which is shared between extensions.
128 ExtensionId owner_extension_id_;
129
130 // Contains the scripts that were found the last time scripts were updated.
131 scoped_ptr<base::SharedMemory> shared_memory_;
132
130 // Whether or not we are currently loading. 133 // Whether or not we are currently loading.
131 bool is_loading_; 134 bool is_loading_;
132 135
133 // The profile for which the scripts managed here are installed. 136 // Manages our notification registrations.
134 Profile* profile_; 137 content::NotificationRegistrar registrar_;
135
136 // Listen to extension load, unloaded notifications.
137 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
138 extension_registry_observer_;
139
140 base::WeakPtrFactory<UserScriptMaster> weak_factory_;
141 138
142 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); 139 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster);
143 }; 140 };
144 141
145 } // namespace extensions 142 } // namespace extensions
146 143
147 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ 144 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698