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

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

Powered by Google App Engine
This is Rietveld 408576698