Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "content/public/browser/notification_observer.h" | |
| 15 #include "content/public/browser/notification_registrar.h" | |
| 16 #include "extensions/common/extension_set.h" | |
| 17 #include "extensions/common/user_script.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class SharedMemory; | |
| 21 } | |
| 22 | |
| 23 namespace content { | |
| 24 class RenderProcessHost; | |
| 25 } | |
| 26 | |
| 27 class Profile; | |
| 28 | |
| 29 namespace extensions { | |
| 30 | |
| 31 class ContentVerifier; | |
| 32 class ExtensionRegistry; | |
| 33 | |
| 34 typedef std::map<ExtensionId, ExtensionSet::ExtensionPathAndDefaultLocale> | |
| 35 ExtensionsInfo; | |
| 36 | |
| 37 // Manages one "logical unit" of user scripts in shared memory by constructing a | |
| 38 // new shared memory region when the set of scripts changes. Also notifies | |
| 39 // renderers of new shared memory region when new renderers appear, or when | |
| 40 // script reloading completes. Script loading lives on the UI thread. Instances | |
| 41 // of this class are embedded within classes with names ending in | |
| 42 // UserScriptMaster. These "master" classes implement the strategy for which | |
| 43 // scripts to load/unload on this logical unit of scripts. | |
| 44 class UserScriptLoader : public content::NotificationObserver { | |
| 45 public: | |
| 46 // Parses the includes out of |script| and returns them in |includes|. | |
| 47 static bool ParseMetadataHeader(const base::StringPiece& script_text, | |
| 48 UserScript* script); | |
| 49 | |
| 50 // A wrapper around the method to load user scripts, which is normally run on | |
| 51 // the file thread. Exposed only for tests. | |
| 52 static void LoadScriptsForTest(UserScriptList* user_scripts); | |
| 53 | |
| 54 UserScriptLoader(Profile* profile, | |
| 55 const ExtensionId& owner_extension_id, | |
| 56 bool listen_for_extension_system_loaded); | |
| 57 virtual ~UserScriptLoader(); | |
| 58 | |
| 59 // Add |scripts| to the set of scripts managed by this loader. | |
| 60 void AddScripts(const std::set<UserScript>& scripts); | |
| 61 | |
| 62 // Remove |scripts| from the set of scripts managed by this loader. | |
| 63 void RemoveScripts(const std::set<UserScript>& scripts); | |
| 64 | |
| 65 // Clears the set of scripts managed by this loader. | |
| 66 void ClearScripts(); | |
| 67 | |
| 68 // Clear extension info for a particular extension that may have scripts | |
| 69 // stored in this loader. | |
| 70 void ClearExtensionInfo(const ExtensionId& extension_id); | |
|
Devlin
2014/08/06 15:59:25
Per other comment, remove this.
Mark Dittmer
2014/08/06 22:08:18
Done.
| |
| 71 | |
| 72 // Initiates procedure to start loading scripts on the file thread. | |
| 73 void StartLoad(); | |
| 74 | |
| 75 // Return true if we have any scripts ready. | |
| 76 bool scripts_ready() const { return shared_memory_.get() != NULL; } | |
| 77 | |
| 78 private: | |
| 79 // Initiates script load when we have been waiting for the extension system | |
| 80 // to be ready. | |
| 81 void OnExtensionSystemReady(); | |
| 82 | |
| 83 // content::NotificationObserver implementation. | |
| 84 virtual void Observe(int type, | |
| 85 const content::NotificationSource& source, | |
| 86 const content::NotificationDetails& details) OVERRIDE; | |
| 87 | |
| 88 // Attempt to initiate a load. | |
| 89 void AttemptLoad(); | |
| 90 | |
| 91 // Called once we have finished loading the scripts on the file thread. | |
| 92 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, | |
| 93 scoped_ptr<base::SharedMemory> shared_memory); | |
| 94 | |
| 95 // Sends the renderer process a new set of user scripts. If | |
| 96 // |changed_extensions| is not empty, this signals that only the scripts from | |
| 97 // those extensions should be updated. Otherwise, all extensions will be | |
| 98 // updated. | |
| 99 void SendUpdate(content::RenderProcessHost* process, | |
| 100 base::SharedMemory* shared_memory, | |
| 101 const std::set<ExtensionId>& changed_extensions); | |
| 102 | |
| 103 // Update |changed_extensions_| to be the extensions referred to in | |
| 104 // |added_scripts_| and |removed_scripts_|. | |
| 105 void ComputeChangedExtensions(); | |
| 106 | |
| 107 // Update |extensions_info_| to contain info for each element of | |
| 108 // |changed_extensions_|. | |
| 109 void UpdateExtensionsInfo(); | |
| 110 | |
| 111 bool is_loading() const { | |
| 112 // Ownership of |user_scripts_| is passed to the file thread when loading. | |
| 113 return user_scripts_.get() == NULL; | |
| 114 } | |
| 115 | |
| 116 // Manages our notification registrations. | |
| 117 content::NotificationRegistrar registrar_; | |
| 118 | |
| 119 // Contains the scripts that were found the last time scripts were updated. | |
| 120 scoped_ptr<base::SharedMemory> shared_memory_; | |
| 121 | |
| 122 // Set of scripts from currently-installed extensions we should load. | |
| 123 scoped_ptr<UserScriptList> user_scripts_; | |
| 124 | |
| 125 // Maps extension info needed for localization to an extension ID. | |
| 126 ExtensionsInfo extensions_info_; | |
| 127 | |
| 128 // The mutually-exclusive sets of scripts that were added or removed since the | |
| 129 // last script load. | |
| 130 std::set<UserScript> added_scripts_; | |
| 131 std::set<UserScript> removed_scripts_; | |
| 132 | |
| 133 // Indicates whether the the collection of scripts should be cleared before | |
| 134 // additions and removals on the next script load. | |
| 135 bool clear_scripts_; | |
| 136 | |
| 137 // The IDs of the extensions which changed in the last update sent to the | |
| 138 // renderer. | |
| 139 std::set<ExtensionId> changed_extensions_; | |
| 140 | |
| 141 // If the extensions service has finished loading its initial set of | |
| 142 // extensions. | |
| 143 bool extension_system_ready_; | |
| 144 | |
| 145 // If list of user scripts is modified while we're loading it, we note | |
| 146 // that we're currently mid-load and then start over again once the load | |
| 147 // finishes. This boolean tracks whether another load is pending. | |
| 148 bool pending_load_; | |
| 149 | |
| 150 // Whether or not we are currently loading. | |
| 151 bool is_loading_; | |
| 152 | |
| 153 // The profile for which the scripts managed here are installed. | |
| 154 Profile* profile_; | |
| 155 | |
| 156 // ID of the extension that owns these scripts, if any. This is only set to a | |
| 157 // non-empty value for declarative user script shared memory regions. | |
| 158 ExtensionId owner_extension_id_; | |
| 159 | |
| 160 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | |
| 161 | |
| 162 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | |
| 163 }; | |
| 164 | |
| 165 } // namespace extensions | |
| 166 | |
| 167 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | |
| OLD | NEW |