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(std::set<UserScript>* 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 // Gets the segment of shared memory for the scripts. | |
| 60 base::SharedMemory* GetSharedMemory() const { return shared_memory_.get(); } | |
|
Devlin
2014/08/05 21:47:27
Do we need this?
Mark Dittmer
2014/08/06 15:32:25
Not really. Removed.
| |
| 61 | |
| 62 // Return true if we have any scripts ready. | |
| 63 bool scripts_ready() const { return shared_memory_.get() != NULL; } | |
|
Devlin
2014/08/05 21:47:27
nit: prefer accessors right above private:
Mark Dittmer
2014/08/06 15:32:25
Done.
| |
| 64 | |
| 65 // Add |scripts| to the set of scripts managed by this loader. | |
| 66 void AddScripts(const std::set<UserScript>& scripts); | |
| 67 | |
| 68 // Remove |scripts| from the set of scripts managed by this loader. | |
| 69 void RemoveScripts(const std::set<UserScript>& scripts); | |
| 70 | |
| 71 // Clear the set of scripts managed by this loader; i.e., make this set be | |
|
Devlin
2014/08/05 21:47:27
lose the i.e.. I think "clear the set" is suitabl
Mark Dittmer
2014/08/06 15:32:25
Done.
| |
| 72 // the empty set. | |
| 73 void ClearScripts(); | |
| 74 | |
| 75 // Initiates procedure to start loading scripts on the file thread. | |
| 76 void StartLoad(); | |
| 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<std::set<UserScript> > user_scripts, | |
| 93 scoped_ptr<base::SharedMemory> shared_memory); | |
|
Devlin
2014/08/05 21:47:27
newline
Mark Dittmer
2014/08/06 15:32:25
Done.
| |
| 94 // Sends the renderer process a new set of user scripts. If | |
| 95 // |changed_extensions| is not empty, this signals that only the scripts from | |
| 96 // those extensions should be updated. Otherwise, all extensions will be | |
| 97 // updated. | |
| 98 void SendUpdate(content::RenderProcessHost* process, | |
| 99 base::SharedMemory* shared_memory, | |
| 100 const std::set<ExtensionId>& changed_extensions); | |
| 101 | |
| 102 // Update |changed_extensions_| to be the extensions referred to in | |
| 103 // |added_scripts_| and |removed_scripts_|. | |
| 104 void ComputeChangedExtensions(); | |
| 105 | |
| 106 // Update |extensions_info_| to contain info for each element of | |
| 107 // |changed_extensions_|. | |
| 108 void UpdateExtensionsInfo(); | |
| 109 | |
| 110 bool is_loading() const { | |
| 111 // Ownership of |user_scripts_| is passed to the file thread when loading. | |
| 112 return user_scripts_.get() == NULL; | |
| 113 } | |
| 114 | |
| 115 // Manages our notification registrations. | |
| 116 content::NotificationRegistrar registrar_; | |
| 117 | |
| 118 // Contains the scripts that were found the last time scripts were updated. | |
| 119 scoped_ptr<base::SharedMemory> shared_memory_; | |
| 120 | |
| 121 // Set of scripts from currently-installed extensions we should load. | |
| 122 scoped_ptr<std::set<UserScript> > user_scripts_; | |
| 123 | |
| 124 // Maps extension info needed for localization to an extension ID. | |
| 125 ExtensionsInfo extensions_info_; | |
| 126 | |
| 127 // The mutually-exclusive sets of scripts that were added or removed since the | |
| 128 // last script load. | |
| 129 std::set<UserScript> added_scripts_; | |
| 130 std::set<UserScript> removed_scripts_; | |
| 131 | |
| 132 // Indicates whether the the collection of scripts should be cleared before | |
| 133 // additions and removals on the next script load. | |
| 134 bool clear_scripts_; | |
| 135 | |
| 136 // The IDs of the extensions which changed in the last update sent to the | |
| 137 // renderer. | |
| 138 std::set<ExtensionId> changed_extensions_; | |
| 139 | |
| 140 // If the extensions service has finished loading its initial set of | |
| 141 // extensions. | |
| 142 bool extension_system_ready_; | |
| 143 | |
| 144 // If list of user scripts is modified while we're loading it, we note | |
| 145 // that we're currently mid-load and then start over again once the load | |
| 146 // finishes. This boolean tracks whether another load is pending. | |
| 147 bool pending_load_; | |
| 148 | |
| 149 // Whether or not we are currently loading. | |
| 150 bool is_loading_; | |
| 151 | |
| 152 // The profile for which the scripts managed here are installed. | |
| 153 Profile* profile_; | |
| 154 | |
| 155 // ID of the extension that owns these scripts, if any. This is only set to a | |
| 156 // non-empty value for declarative user script shared memory regions. | |
| 157 ExtensionId owner_extension_id_; | |
| 158 | |
| 159 base::WeakPtrFactory<UserScriptLoader> weak_factory_; | |
| 160 | |
| 161 DISALLOW_COPY_AND_ASSIGN(UserScriptLoader); | |
| 162 }; | |
| 163 | |
| 164 } // namespace extensions | |
| 165 | |
| 166 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_LOADER_H_ | |
| OLD | NEW |