| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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_MASTER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/memory/scoped_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" | |
| 17 #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" | |
| 21 #include "extensions/common/user_script.h" | |
| 22 | |
| 23 namespace base { | |
| 24 class SharedMemory; | |
| 25 } | |
| 26 | |
| 27 namespace content { | |
| 28 class RenderProcessHost; | |
| 29 } | |
| 30 | |
| 31 class Profile; | |
| 32 | |
| 33 namespace extensions { | |
| 34 | |
| 35 class ContentVerifier; | |
| 36 class ExtensionRegistry; | |
| 37 | |
| 38 typedef std::map<std::string, ExtensionSet::ExtensionPathAndDefaultLocale> | |
| 39 ExtensionsInfo; | |
| 40 | |
| 41 // Manages a segment of shared memory that contains the user scripts the user | |
| 42 // has installed. Lives on the UI thread. | |
| 43 class UserScriptMaster : public content::NotificationObserver, | |
| 44 public ExtensionRegistryObserver { | |
| 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 explicit UserScriptMaster(Profile* profile); | |
| 55 virtual ~UserScriptMaster(); | |
| 56 | |
| 57 // Kicks off a process on the file thread to reload scripts from disk | |
| 58 // into a new chunk of shared memory and notify renderers. | |
| 59 virtual void StartLoad(); | |
| 60 | |
| 61 // Gets the segment of shared memory for the scripts. | |
| 62 base::SharedMemory* GetSharedMemory() const { | |
| 63 return shared_memory_.get(); | |
| 64 } | |
| 65 | |
| 66 // Return true if we have any scripts ready. | |
| 67 bool ScriptsReady() const { return shared_memory_.get() != NULL; } | |
| 68 | |
| 69 private: | |
| 70 // content::NotificationObserver implementation. | |
| 71 virtual void Observe(int type, | |
| 72 const content::NotificationSource& source, | |
| 73 const content::NotificationDetails& details) OVERRIDE; | |
| 74 | |
| 75 // ExtensionRegistryObserver implementation. | |
| 76 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, | |
| 77 const Extension* extension) OVERRIDE; | |
| 78 virtual void OnExtensionUnloaded( | |
| 79 content::BrowserContext* browser_context, | |
| 80 const Extension* extension, | |
| 81 UnloadedExtensionInfo::Reason reason) OVERRIDE; | |
| 82 | |
| 83 // Called when ExtensionSystem is ready. | |
| 84 void OnExtensionsReady(); | |
| 85 | |
| 86 // Called once we have finished loading the scripts on the file thread. | |
| 87 void OnScriptsLoaded(scoped_ptr<UserScriptList> user_scripts, | |
| 88 scoped_ptr<base::SharedMemory> shared_memory); | |
| 89 | |
| 90 // Sends the renderer process a new set of user scripts. If | |
| 91 // |changed_extensions| is not empty, this signals that only the scripts from | |
| 92 // those extensions should be updated. Otherwise, all extensions will be | |
| 93 // updated. | |
| 94 void SendUpdate(content::RenderProcessHost* process, | |
| 95 base::SharedMemory* shared_memory, | |
| 96 const std::set<std::string>& changed_extensions); | |
| 97 | |
| 98 bool is_loading() const { | |
| 99 // Ownership of |user_scripts_| is passed to the file thread when loading. | |
| 100 return user_scripts_.get() == NULL; | |
| 101 } | |
| 102 | |
| 103 // Manages our notification registrations. | |
| 104 content::NotificationRegistrar registrar_; | |
| 105 | |
| 106 // Contains the scripts that were found the last time scripts were updated. | |
| 107 scoped_ptr<base::SharedMemory> shared_memory_; | |
| 108 | |
| 109 // List of scripts from currently-installed extensions we should load. | |
| 110 scoped_ptr<UserScriptList> user_scripts_; | |
| 111 | |
| 112 // Maps extension info needed for localization to an extension ID. | |
| 113 ExtensionsInfo extensions_info_; | |
| 114 | |
| 115 // The IDs of the extensions which have changed since the last update sent to | |
| 116 // the renderer. | |
| 117 std::set<std::string> changed_extensions_; | |
| 118 | |
| 119 // The mutually-exclusive sets of extensions that were added or removed since | |
| 120 // the last script load. | |
| 121 std::set<std::string> added_extensions_; | |
| 122 std::set<std::string> removed_extensions_; | |
| 123 | |
| 124 // If the extensions service has finished loading its initial set of | |
| 125 // extensions. | |
| 126 bool extensions_service_ready_; | |
| 127 | |
| 128 // If list of user scripts is modified while we're loading it, we note | |
| 129 // that we're currently mid-load and then start over again once the load | |
| 130 // finishes. This boolean tracks whether another load is pending. | |
| 131 bool pending_load_; | |
| 132 | |
| 133 // Whether or not we are currently loading. | |
| 134 bool is_loading_; | |
| 135 | |
| 136 // The profile for which the scripts managed here are installed. | |
| 137 Profile* profile_; | |
| 138 | |
| 139 // Listen to extension load, unloaded notifications. | |
| 140 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | |
| 141 extension_registry_observer_; | |
| 142 | |
| 143 base::WeakPtrFactory<UserScriptMaster> weak_factory_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); | |
| 146 }; | |
| 147 | |
| 148 } // namespace extensions | |
| 149 | |
| 150 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | |
| OLD | NEW |