Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | |
| 15 #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" | |
| 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<ExtensionId, 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. |
|
Devlin
2014/08/04 18:33:26
Update this comment to document the purpose of thi
Mark Dittmer
2014/08/05 20:33:19
Done.
| |
| 43 class UserScriptMaster : public content::NotificationObserver, | 39 class UserScriptLoader : public content::NotificationObserver { |
| 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(std::set<UserScript>* user_scripts); |
| 53 | 48 |
| 54 explicit UserScriptMaster(Profile* profile); | 49 explicit UserScriptLoader(Profile* profile, |
|
Devlin
2014/08/04 18:33:25
This no longer needs explicit :)
Mark Dittmer
2014/08/05 20:33:19
Done.
| |
| 55 virtual ~UserScriptMaster(); | 50 ExtensionId owner_extension_id, |
|
Devlin
2014/08/04 18:33:26
const ExtensionId&
Mark Dittmer
2014/08/05 20:33:20
Done.
| |
| 56 | 51 bool listen_for_extension_system_loaded); |
| 57 // Kicks off a process on the file thread to reload scripts from disk | 52 virtual ~UserScriptLoader(); |
| 58 // into a new chunk of shared memory and notify renderers. | |
| 59 virtual void StartLoad(); | |
| 60 | 53 |
| 61 // Gets the segment of shared memory for the scripts. | 54 // Gets the segment of shared memory for the scripts. |
| 62 base::SharedMemory* GetSharedMemory() const { | 55 base::SharedMemory* GetSharedMemory() const { return shared_memory_.get(); } |
| 63 return shared_memory_.get(); | |
| 64 } | |
| 65 | 56 |
| 66 // Return true if we have any scripts ready. | 57 // Return true if we have any scripts ready. |
| 67 bool ScriptsReady() const { return shared_memory_.get() != NULL; } | 58 bool ScriptsReady() const { return shared_memory_.get() != NULL; } |
|
Devlin
2014/08/04 18:33:26
Since all the callers of this are being updated an
Mark Dittmer
2014/08/05 20:33:19
Done.
| |
| 68 | 59 |
| 60 void AddScripts(const std::set<UserScript>& scripts); | |
|
Devlin
2014/08/04 18:33:25
comments
Mark Dittmer
2014/08/05 20:33:20
Done.
| |
| 61 void RemoveScripts(const std::set<UserScript>& scripts); | |
| 62 void ClearScripts(); | |
| 63 | |
| 64 // Initiates procedure to start loading scripts on the file thread. | |
| 65 void StartLoad(); | |
| 66 | |
| 69 private: | 67 private: |
| 68 // Initiates script load when we have been waiting for the extension system | |
| 69 // to be ready. | |
| 70 void OnExtensionSystemReady(); | |
| 71 | |
| 70 // content::NotificationObserver implementation. | 72 // content::NotificationObserver implementation. |
| 71 virtual void Observe(int type, | 73 virtual void Observe(int type, |
| 72 const content::NotificationSource& source, | 74 const content::NotificationSource& source, |
| 73 const content::NotificationDetails& details) OVERRIDE; | 75 const content::NotificationDetails& details) OVERRIDE; |
| 74 | 76 |
| 75 // ExtensionRegistryObserver implementation. | 77 // Attempt to initiate a load. |
| 76 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, | 78 void AttemptLoad(); |
| 77 const Extension* extension) OVERRIDE; | |
| 78 virtual void OnExtensionUnloaded( | |
| 79 content::BrowserContext* browser_context, | |
| 80 const Extension* extension, | |
| 81 UnloadedExtensionInfo::Reason reason) OVERRIDE; | |
| 82 | 79 |
| 83 // Called once we have finished loading the scripts on the file thread. | 80 // Called once we have finished loading the scripts on the file thread. |
| 84 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, | 81 void OnScriptsLoaded(scoped_ptr<std::set<UserScript> > user_scripts, |
| 85 scoped_ptr<base::SharedMemory> shared_memory); | 82 scoped_ptr<base::SharedMemory> shared_memory); |
| 86 | |
| 87 // Sends the renderer process a new set of user scripts. If | 83 // Sends the renderer process a new set of user scripts. If |
| 88 // |changed_extensions| is not empty, this signals that only the scripts from | 84 // |changed_extensions| is not empty, this signals that only the scripts from |
| 89 // those extensions should be updated. Otherwise, all extensions will be | 85 // those extensions should be updated. Otherwise, all extensions will be |
| 90 // updated. | 86 // updated. |
| 91 void SendUpdate(content::RenderProcessHost* process, | 87 void SendUpdate(content::RenderProcessHost* process, |
| 92 base::SharedMemory* shared_memory, | 88 base::SharedMemory* shared_memory, |
| 93 const std::set<std::string>& changed_extensions); | 89 const std::set<ExtensionId>& changed_extensions); |
| 90 | |
| 91 // Update |changed_extensions_| to be the extensions referred to in | |
| 92 // |added_scripts_| and |removed_scripts_|. | |
| 93 void ComputeChangedExtensions(); | |
| 94 | |
| 95 // Update |extensions_info_| to contain info for each element of | |
| 96 // |changed_extensions_|. | |
| 97 void UpdateExtensionsInfo(); | |
| 94 | 98 |
| 95 bool is_loading() const { | 99 bool is_loading() const { |
| 96 // Ownership of |user_scripts_| is passed to the file thread when loading. | 100 // Ownership of |user_scripts_| is passed to the file thread when loading. |
| 97 return user_scripts_.get() == NULL; | 101 return user_scripts_.get() == NULL; |
| 98 } | 102 } |
| 99 | 103 |
| 100 // Manages our notification registrations. | 104 // Manages our notification registrations. |
| 101 content::NotificationRegistrar registrar_; | 105 content::NotificationRegistrar registrar_; |
| 102 | 106 |
| 103 // Contains the scripts that were found the last time scripts were updated. | 107 // Contains the scripts that were found the last time scripts were updated. |
| 104 scoped_ptr<base::SharedMemory> shared_memory_; | 108 scoped_ptr<base::SharedMemory> shared_memory_; |
| 105 | 109 |
| 106 // List of scripts from currently-installed extensions we should load. | 110 // Set of scripts from currently-installed extensions we should load. |
| 107 scoped_ptr<UserScriptList> user_scripts_; | 111 scoped_ptr<std::set<UserScript> > user_scripts_; |
| 108 | 112 |
| 109 // Maps extension info needed for localization to an extension ID. | 113 // Maps extension info needed for localization to an extension ID. |
| 110 ExtensionsInfo extensions_info_; | 114 ExtensionsInfo extensions_info_; |
| 111 | 115 |
| 112 // The IDs of the extensions which have changed since the last update sent to | 116 // The mutually-exclusive sets of scripts that were added or removed since the |
| 113 // the renderer. | 117 // last script load. |
| 114 std::set<std::string> changed_extensions_; | 118 std::set<UserScript> added_scripts_; |
| 119 std::set<UserScript> removed_scripts_; | |
| 115 | 120 |
| 116 // The mutually-exclusive sets of extensions that were added or removed since | 121 // Boolean that indicates whether the the collection of scripts should be |
|
Devlin
2014/08/04 18:33:25
Since this has type bool, I think we can just say
Mark Dittmer
2014/08/05 20:33:19
Done.
| |
| 117 // the last script load. | 122 // cleared before additions+removals on the next script load. |
| 118 std::set<std::string> added_extensions_; | 123 bool clear_scripts_; |
| 119 std::set<std::string> removed_extensions_; | 124 |
| 125 // The IDs of the extensions which changed in the last update sent to the | |
| 126 // renderer. | |
| 127 std::set<ExtensionId> changed_extensions_; | |
| 120 | 128 |
| 121 // If the extensions service has finished loading its initial set of | 129 // If the extensions service has finished loading its initial set of |
| 122 // extensions. | 130 // extensions. |
| 123 bool extensions_service_ready_; | 131 bool extension_system_ready_; |
| 124 | 132 |
| 125 // If list of user scripts is modified while we're loading it, we note | 133 // 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 | 134 // that we're currently mid-load and then start over again once the load |
| 127 // finishes. This boolean tracks whether another load is pending. | 135 // finishes. This boolean tracks whether another load is pending. |
| 128 bool pending_load_; | 136 bool pending_load_; |
| 129 | 137 |
| 130 // Whether or not we are currently loading. | 138 // Whether or not we are currently loading. |
| 131 bool is_loading_; | 139 bool is_loading_; |
| 132 | 140 |
| 133 // The profile for which the scripts managed here are installed. | 141 // The profile for which the scripts managed here are installed. |
| 134 Profile* profile_; | 142 Profile* profile_; |
| 135 | 143 |
| 136 // Listen to extension load, unloaded notifications. | 144 // ID of the extension that owns these scripts, if any. This is only set to a |
| 137 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 145 // non-empty value for declarative user script shared memory regions. |
| 138 extension_registry_observer_; | 146 ExtensionId owner_extension_id_; |
| 139 | 147 |
| 140 base::WeakPtrFactory<UserScriptMaster> weak_factory_; | 148 base::WeakPtrFactory<UserScriptLoader> weak_factory_; |
| 141 | 149 |
| 142 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); | 150 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); |
| 143 }; | 151 }; |
| 144 | 152 |
| 145 } // namespace extensions | 153 } // namespace extensions |
| 146 | 154 |
| 147 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | 155 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ |
| OLD | NEW |