OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include <map> |
| 10 #include <string> |
| 11 |
9 #include "base/file_path.h" | 12 #include "base/file_path.h" |
10 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
12 #include "base/shared_memory.h" | 15 #include "base/shared_memory.h" |
| 16 #include "chrome/browser/extensions/extension_info_map.h" |
| 17 #include "chrome/common/extensions/extension_messages.h" |
| 18 #include "chrome/common/extensions/extension_set.h" |
13 #include "chrome/common/extensions/user_script.h" | 19 #include "chrome/common/extensions/user_script.h" |
14 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
15 #include "content/common/notification_observer.h" | 21 #include "content/common/notification_observer.h" |
16 #include "content/common/notification_registrar.h" | 22 #include "content/common/notification_registrar.h" |
17 | 23 |
18 namespace base { | 24 namespace base { |
19 class StringPiece; | 25 class StringPiece; |
20 } | 26 } |
21 | 27 |
22 class Profile; | 28 class Profile; |
23 class RenderProcessHost; | 29 class RenderProcessHost; |
24 | 30 |
| 31 typedef std::map<std::string, ExtensionSet::ExtensionPathAndDefaultLocale> |
| 32 ExtensionsInfo; |
| 33 |
25 // Manages a segment of shared memory that contains the user scripts the user | 34 // Manages a segment of shared memory that contains the user scripts the user |
26 // has installed. Lives on the UI thread. | 35 // has installed. Lives on the UI thread. |
27 class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>, | 36 class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>, |
28 public NotificationObserver { | 37 public NotificationObserver { |
29 public: | 38 public: |
30 explicit UserScriptMaster(Profile* profile); | 39 explicit UserScriptMaster(Profile* profile); |
31 | 40 |
32 // Kicks off a process on the file thread to reload scripts from disk | 41 // Kicks off a process on the file thread to reload scripts from disk |
33 // into a new chunk of shared memory and notify renderers. | 42 // into a new chunk of shared memory and notify renderers. |
34 virtual void StartLoad(); | 43 virtual void StartLoad(); |
(...skipping 25 matching lines...) Expand all Loading... |
60 : public base::RefCountedThreadSafe<UserScriptMaster::ScriptReloader> { | 69 : public base::RefCountedThreadSafe<UserScriptMaster::ScriptReloader> { |
61 public: | 70 public: |
62 // Parses the includes out of |script| and returns them in |includes|. | 71 // Parses the includes out of |script| and returns them in |includes|. |
63 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 72 static bool ParseMetadataHeader(const base::StringPiece& script_text, |
64 UserScript* script); | 73 UserScript* script); |
65 | 74 |
66 explicit ScriptReloader(UserScriptMaster* master); | 75 explicit ScriptReloader(UserScriptMaster* master); |
67 | 76 |
68 // Start loading of scripts. | 77 // Start loading of scripts. |
69 // Will always send a message to the master upon completion. | 78 // Will always send a message to the master upon completion. |
70 void StartLoad(const UserScriptList& external_scripts); | 79 void StartLoad(const UserScriptList& external_scripts, |
| 80 const ExtensionsInfo& extension_info_); |
71 | 81 |
72 // The master is going away; don't call it back. | 82 // The master is going away; don't call it back. |
73 void DisownMaster() { | 83 void DisownMaster() { |
74 master_ = NULL; | 84 master_ = NULL; |
75 } | 85 } |
76 | 86 |
77 private: | 87 private: |
78 private: | |
79 FRIEND_TEST_ALL_PREFIXES(UserScriptMasterTest, SkipBOMAtTheBeginning); | 88 FRIEND_TEST_ALL_PREFIXES(UserScriptMasterTest, SkipBOMAtTheBeginning); |
80 FRIEND_TEST_ALL_PREFIXES(UserScriptMasterTest, LeaveBOMNotAtTheBeginning); | 89 FRIEND_TEST_ALL_PREFIXES(UserScriptMasterTest, LeaveBOMNotAtTheBeginning); |
81 friend class base::RefCountedThreadSafe<UserScriptMaster::ScriptReloader>; | 90 friend class base::RefCountedThreadSafe<UserScriptMaster::ScriptReloader>; |
82 | 91 |
83 ~ScriptReloader() {} | 92 ~ScriptReloader() {} |
84 | 93 |
85 // Where functions are run: | 94 // Where functions are run: |
86 // master file | 95 // master file |
87 // StartLoad -> RunLoad | 96 // StartLoad -> RunLoad |
88 // LoadUserScripts() | 97 // LoadUserScripts() |
89 // NotifyMaster <- RunLoad | 98 // NotifyMaster <- RunLoad |
90 | 99 |
91 // Runs on the master thread. | 100 // Runs on the master thread. |
92 // Notify the master that new scripts are available. | 101 // Notify the master that new scripts are available. |
93 void NotifyMaster(base::SharedMemory* memory); | 102 void NotifyMaster(base::SharedMemory* memory); |
94 | 103 |
95 // Runs on the File thread. | 104 // Runs on the File thread. |
96 // Load the specified user scripts, calling NotifyMaster when done. | 105 // Load the specified user scripts, calling NotifyMaster when done. |
97 // |user_scripts| is intentionally passed by value so its lifetime isn't | 106 // |user_scripts| is intentionally passed by value so its lifetime isn't |
98 // tied to the caller. | 107 // tied to the caller. |
99 void RunLoad(const UserScriptList& user_scripts); | 108 void RunLoad(const UserScriptList& user_scripts); |
100 | 109 |
101 static void LoadUserScripts(UserScriptList* user_scripts); | 110 void LoadUserScripts(UserScriptList* user_scripts); |
| 111 |
| 112 // Uses extensions_info_ to build a map of localization messages. |
| 113 // Returns NULL if |extension_id| is invalid. |
| 114 SubstitutionMap* GetLocalizationMessages(std::string extension_id); |
102 | 115 |
103 // A pointer back to our master. | 116 // A pointer back to our master. |
104 // May be NULL if DisownMaster() is called. | 117 // May be NULL if DisownMaster() is called. |
105 UserScriptMaster* master_; | 118 UserScriptMaster* master_; |
106 | 119 |
| 120 // Maps extension info needed for localization to an extension ID. |
| 121 ExtensionsInfo extensions_info_; |
| 122 |
107 // The message loop to call our master back on. | 123 // The message loop to call our master back on. |
108 // Expected to always outlive us. | 124 // Expected to always outlive us. |
109 BrowserThread::ID master_thread_id_; | 125 BrowserThread::ID master_thread_id_; |
110 | 126 |
111 DISALLOW_COPY_AND_ASSIGN(ScriptReloader); | 127 DISALLOW_COPY_AND_ASSIGN(ScriptReloader); |
112 }; | 128 }; |
113 | 129 |
114 private: | 130 private: |
115 // NotificationObserver implementation. | 131 // NotificationObserver implementation. |
116 virtual void Observe(int type, | 132 virtual void Observe(int type, |
117 const NotificationSource& source, | 133 const NotificationSource& source, |
118 const NotificationDetails& details); | 134 const NotificationDetails& details); |
119 | 135 |
120 // Sends the renderer process a new set of user scripts. | 136 // Sends the renderer process a new set of user scripts. |
121 void SendUpdate(RenderProcessHost* process, | 137 void SendUpdate(RenderProcessHost* process, |
122 base::SharedMemory* shared_memory); | 138 base::SharedMemory* shared_memory); |
123 | 139 |
124 // Manages our notification registrations. | 140 // Manages our notification registrations. |
125 NotificationRegistrar registrar_; | 141 NotificationRegistrar registrar_; |
126 | 142 |
127 // We hang on to our pointer to know if we've already got one running. | 143 // We hang on to our pointer to know if we've already got one running. |
128 scoped_refptr<ScriptReloader> script_reloader_; | 144 scoped_refptr<ScriptReloader> script_reloader_; |
129 | 145 |
130 // Contains the scripts that were found the last time scripts were updated. | 146 // Contains the scripts that were found the last time scripts were updated. |
131 scoped_ptr<base::SharedMemory> shared_memory_; | 147 scoped_ptr<base::SharedMemory> shared_memory_; |
132 | 148 |
133 // List of scripts from currently-installed extensions we should load. | 149 // List of scripts from currently-installed extensions we should load. |
134 UserScriptList user_scripts_; | 150 UserScriptList user_scripts_; |
135 | 151 |
| 152 // Maps extension info needed for localization to an extension ID. |
| 153 ExtensionsInfo extensions_info_; |
| 154 |
136 // If the extensions service has finished loading its initial set of | 155 // If the extensions service has finished loading its initial set of |
137 // extensions. | 156 // extensions. |
138 bool extensions_service_ready_; | 157 bool extensions_service_ready_; |
139 | 158 |
140 // If list of user scripts is modified while we're loading it, we note | 159 // If list of user scripts is modified while we're loading it, we note |
141 // that we're currently mid-load and then start over again once the load | 160 // that we're currently mid-load and then start over again once the load |
142 // finishes. This boolean tracks whether another load is pending. | 161 // finishes. This boolean tracks whether another load is pending. |
143 bool pending_load_; | 162 bool pending_load_; |
144 | 163 |
145 // The profile for which the scripts managed here are installed. | 164 // The profile for which the scripts managed here are installed. |
146 Profile* profile_; | 165 Profile* profile_; |
147 | 166 |
148 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); | 167 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); |
149 }; | 168 }; |
150 | 169 |
151 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | 170 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ |
OLD | NEW |