| OLD | NEW |
| 1 // Copyright 2014 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_LOADER_H_ | 5 #ifndef EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | 6 #define EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 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/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/scoped_observer.h" | 15 #include "base/scoped_observer.h" |
| 16 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
| 18 #include "extensions/common/host_id.h" | 18 #include "extensions/common/host_id.h" |
| 19 #include "extensions/common/user_script.h" | 19 #include "extensions/common/user_script.h" |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 class SharedMemory; | 22 class SharedMemory; |
| 23 } | 23 } |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 class BrowserContext; | 26 class BrowserContext; |
| 27 class RenderProcessHost; | 27 class RenderProcessHost; |
| 28 } | 28 } |
| 29 | 29 |
| 30 class Profile; | |
| 31 | |
| 32 namespace extensions { | 30 namespace extensions { |
| 33 | 31 |
| 34 class ContentVerifier; | 32 class ContentVerifier; |
| 35 | 33 |
| 36 // Manages one "logical unit" of user scripts in shared memory by constructing a | 34 // Manages one "logical unit" of user scripts in shared memory by constructing a |
| 37 // new shared memory region when the set of scripts changes. Also notifies | 35 // new shared memory region when the set of scripts changes. Also notifies |
| 38 // renderers of new shared memory region when new renderers appear, or when | 36 // renderers of new shared memory region when new renderers appear, or when |
| 39 // script reloading completes. Script loading lives on the UI thread. Instances | 37 // script reloading completes. Script loading lives on the UI thread. Instances |
| 40 // of this class are embedded within classes with names ending in | 38 // of this class are embedded within classes with names ending in |
| 41 // UserScriptMaster. These "master" classes implement the strategy for which | 39 // UserScriptMaster. These "master" classes implement the strategy for which |
| 42 // scripts to load/unload on this logical unit of scripts. | 40 // scripts to load/unload on this logical unit of scripts. |
| 43 class UserScriptLoader : public content::NotificationObserver { | 41 class UserScriptLoader : public content::NotificationObserver { |
| 44 public: | 42 public: |
| 45 using PathAndDefaultLocale = std::pair<base::FilePath, std::string>; | 43 using PathAndDefaultLocale = std::pair<base::FilePath, std::string>; |
| 46 using HostsInfo = std::map<HostID, PathAndDefaultLocale>; | 44 using HostsInfo = std::map<HostID, PathAndDefaultLocale>; |
| 47 | 45 |
| 48 using SubstitutionMap = std::map<std::string, std::string>; | 46 using SubstitutionMap = std::map<std::string, std::string>; |
| 49 using LoadUserScriptsContentFunction = | 47 using LoadUserScriptsContentFunction = |
| 50 base::Callback<bool(const HostID&, | 48 base::Callback<bool(const HostID&, |
| 51 UserScript::File*, | 49 UserScript::File*, |
| 52 const SubstitutionMap*, | 50 const SubstitutionMap*, |
| 53 const scoped_refptr<ContentVerifier>&)>; | 51 const scoped_refptr<ContentVerifier>&)>; |
| 54 | 52 |
| 55 // Parses the includes out of |script| and returns them in |includes|. | 53 // Parses the includes out of |script| and returns them in |includes|. |
| 56 static bool ParseMetadataHeader(const base::StringPiece& script_text, | 54 static bool ParseMetadataHeader(const base::StringPiece& script_text, |
| 57 UserScript* script); | 55 UserScript* script); |
| 58 | 56 |
| 59 UserScriptLoader(Profile* profile, | 57 UserScriptLoader(content::BrowserContext* browser_context, |
| 60 const HostID& host_id, | 58 const HostID& host_id, |
| 61 const scoped_refptr<ContentVerifier>& content_verifier); | 59 const scoped_refptr<ContentVerifier>& content_verifier); |
| 62 ~UserScriptLoader() override; | 60 ~UserScriptLoader() override; |
| 63 | 61 |
| 64 // A wrapper around the method to load user scripts, which is normally run on | 62 // A wrapper around the method to load user scripts, which is normally run on |
| 65 // the file thread. Exposed only for tests. | 63 // the file thread. Exposed only for tests. |
| 66 void LoadScriptsForTest(UserScriptList* user_scripts); | 64 void LoadScriptsForTest(UserScriptList* user_scripts); |
| 67 | 65 |
| 68 // Add |scripts| to the set of scripts managed by this loader. | 66 // Add |scripts| to the set of scripts managed by this loader. |
| 69 void AddScripts(const std::set<UserScript>& scripts); | 67 void AddScripts(const std::set<UserScript>& scripts); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 96 // exists. | 94 // exists. |
| 97 void AddHostInfo(const HostID& host_id, const PathAndDefaultLocale& location); | 95 void AddHostInfo(const HostID& host_id, const PathAndDefaultLocale& location); |
| 98 | 96 |
| 99 // Removes the entries with the given host_id from the |hosts_info_| map. | 97 // Removes the entries with the given host_id from the |hosts_info_| map. |
| 100 void RemoveHostInfo(const HostID& host_id); | 98 void RemoveHostInfo(const HostID& host_id); |
| 101 | 99 |
| 102 // Sets the flag if the initial set of hosts has finished loading; if it's | 100 // Sets the flag if the initial set of hosts has finished loading; if it's |
| 103 // set to be true, calls AttempLoad() to bootstrap. | 101 // set to be true, calls AttempLoad() to bootstrap. |
| 104 void SetReady(bool ready); | 102 void SetReady(bool ready); |
| 105 | 103 |
| 106 Profile* profile() const { return profile_; } | 104 content::BrowserContext* browser_context() const { return browser_context_; } |
| 107 const HostID& host_id() const { return host_id_; } | 105 const HostID& host_id() const { return host_id_; } |
| 108 | 106 |
| 109 private: | 107 private: |
| 110 // content::NotificationObserver implementation. | 108 // content::NotificationObserver implementation. |
| 111 void Observe(int type, | 109 void Observe(int type, |
| 112 const content::NotificationSource& source, | 110 const content::NotificationSource& source, |
| 113 const content::NotificationDetails& details) override; | 111 const content::NotificationDetails& details) override; |
| 114 | 112 |
| 115 // Returns whether or not it is possible that calls to AddScripts(), | 113 // Returns whether or not it is possible that calls to AddScripts(), |
| 116 // RemoveScripts(), and/or ClearScripts() have caused any real change in the | 114 // RemoveScripts(), and/or ClearScripts() have caused any real change in the |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 bool ready_; | 164 bool ready_; |
| 167 | 165 |
| 168 // If list of user scripts is modified while we're loading it, we note | 166 // If list of user scripts is modified while we're loading it, we note |
| 169 // that we're currently mid-load and then start over again once the load | 167 // that we're currently mid-load and then start over again once the load |
| 170 // finishes. This boolean tracks whether another load is pending. | 168 // finishes. This boolean tracks whether another load is pending. |
| 171 bool pending_load_; | 169 bool pending_load_; |
| 172 | 170 |
| 173 // Whether or not we are currently loading. | 171 // Whether or not we are currently loading. |
| 174 bool is_loading_; | 172 bool is_loading_; |
| 175 | 173 |
| 176 // The profile for which the scripts managed here are installed. | 174 // The browser_context for which the scripts managed here are installed. |
| 177 Profile* profile_; | 175 content::BrowserContext* browser_context_; |
| 178 | 176 |
| 179 // ID of the host that owns these scripts, if any. This is only set to a | 177 // ID of the host that owns these scripts, if any. This is only set to a |
| 180 // non-empty value for declarative user script shared memory regions. | 178 // non-empty value for declarative user script shared memory regions. |
| 181 HostID host_id_; | 179 HostID host_id_; |
| 182 | 180 |
| 183 // Manages content verification of the loaded user scripts. | 181 // Manages content verification of the loaded user scripts. |
| 184 scoped_refptr<ContentVerifier> content_verifier_; | 182 scoped_refptr<ContentVerifier> content_verifier_; |
| 185 | 183 |
| 186 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | 184 base::WeakPtrFactory<UserScriptLoader> weak_factory_; |
| 187 | 185 |
| 188 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | 186 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); |
| 189 }; | 187 }; |
| 190 | 188 |
| 191 } // namespace extensions | 189 } // namespace extensions |
| 192 | 190 |
| 193 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | 191 #endif // EXTENSIONS_BROWSER_USER_SCRIPT_LOADER_H_ |
| OLD | NEW |