Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / user_script_master.h
blobddcc1f5bdca49c66dbb63d30a0c20c594c615cbc
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_
6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_
8 #include <map>
9 #include <string>
11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/shared_memory.h"
16 #include "base/strings/string_piece.h"
17 #include "chrome/common/extensions/extension_messages.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "extensions/common/extension_set.h"
22 #include "extensions/common/user_script.h"
24 namespace content {
25 class RenderProcessHost;
28 class Profile;
30 namespace extensions {
32 typedef std::map<std::string, ExtensionSet::ExtensionPathAndDefaultLocale>
33 ExtensionsInfo;
35 // Manages a segment of shared memory that contains the user scripts the user
36 // has installed. Lives on the UI thread.
37 class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>,
38 public content::NotificationObserver {
39 public:
40 explicit UserScriptMaster(Profile* profile);
42 // Kicks off a process on the file thread to reload scripts from disk
43 // into a new chunk of shared memory and notify renderers.
44 virtual void StartLoad();
46 // Gets the segment of shared memory for the scripts.
47 base::SharedMemory* GetSharedMemory() const {
48 return shared_memory_.get();
51 // Called by the script reloader when new scripts have been loaded.
52 void NewScriptsAvailable(base::SharedMemory* handle);
54 // Return true if we have any scripts ready.
55 bool ScriptsReady() const { return shared_memory_.get() != NULL; }
57 protected:
58 friend class base::RefCountedThreadSafe<UserScriptMaster>;
60 virtual ~UserScriptMaster();
62 public:
63 // We reload user scripts on the file thread to prevent blocking the UI.
64 // ScriptReloader lives on the file thread and does the reload
65 // work, and then sends a message back to its master with a new SharedMemory*.
66 // ScriptReloader is the worker that manages running the script load
67 // on the file thread. It must be created on, and its public API must only be
68 // called from, the master's thread.
69 class ScriptReloader
70 : public base::RefCountedThreadSafe<UserScriptMaster::ScriptReloader> {
71 public:
72 // Parses the includes out of |script| and returns them in |includes|.
73 static bool ParseMetadataHeader(const base::StringPiece& script_text,
74 UserScript* script);
76 explicit ScriptReloader(UserScriptMaster* master);
78 // Start loading of scripts.
79 // Will always send a message to the master upon completion.
80 void StartLoad(const UserScriptList& external_scripts,
81 const ExtensionsInfo& extension_info_);
83 // The master is going away; don't call it back.
84 void DisownMaster() {
85 master_ = NULL;
88 private:
89 FRIEND_TEST_ALL_PREFIXES(UserScriptMasterTest, SkipBOMAtTheBeginning);
90 FRIEND_TEST_ALL_PREFIXES(UserScriptMasterTest, LeaveBOMNotAtTheBeginning);
91 friend class base::RefCountedThreadSafe<UserScriptMaster::ScriptReloader>;
93 ~ScriptReloader();
95 // Where functions are run:
96 // master file
97 // StartLoad -> RunLoad
98 // LoadUserScripts()
99 // NotifyMaster <- RunLoad
101 // Runs on the master thread.
102 // Notify the master that new scripts are available.
103 void NotifyMaster(base::SharedMemory* memory);
105 // Runs on the File thread.
106 // Load the specified user scripts, calling NotifyMaster when done.
107 // |user_scripts| is intentionally passed by value so its lifetime isn't
108 // tied to the caller.
109 void RunLoad(const UserScriptList& user_scripts);
111 void LoadUserScripts(UserScriptList* user_scripts);
113 // Uses extensions_info_ to build a map of localization messages.
114 // Returns NULL if |extension_id| is invalid.
115 SubstitutionMap* GetLocalizationMessages(std::string extension_id);
117 // A pointer back to our master.
118 // May be NULL if DisownMaster() is called.
119 UserScriptMaster* master_;
121 // Maps extension info needed for localization to an extension ID.
122 ExtensionsInfo extensions_info_;
124 // The message loop to call our master back on.
125 // Expected to always outlive us.
126 content::BrowserThread::ID master_thread_id_;
128 DISALLOW_COPY_AND_ASSIGN(ScriptReloader);
131 private:
132 // content::NotificationObserver implementation.
133 virtual void Observe(int type,
134 const content::NotificationSource& source,
135 const content::NotificationDetails& details) OVERRIDE;
137 // Sends the renderer process a new set of user scripts.
138 void SendUpdate(content::RenderProcessHost* process,
139 base::SharedMemory* shared_memory);
141 // Manages our notification registrations.
142 content::NotificationRegistrar registrar_;
144 // We hang on to our pointer to know if we've already got one running.
145 scoped_refptr<ScriptReloader> script_reloader_;
147 // Contains the scripts that were found the last time scripts were updated.
148 scoped_ptr<base::SharedMemory> shared_memory_;
150 // List of scripts from currently-installed extensions we should load.
151 UserScriptList user_scripts_;
153 // Maps extension info needed for localization to an extension ID.
154 ExtensionsInfo extensions_info_;
156 // If the extensions service has finished loading its initial set of
157 // extensions.
158 bool extensions_service_ready_;
160 // If list of user scripts is modified while we're loading it, we note
161 // that we're currently mid-load and then start over again once the load
162 // finishes. This boolean tracks whether another load is pending.
163 bool pending_load_;
165 // The profile for which the scripts managed here are installed.
166 Profile* profile_;
168 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster);
171 } // namespace extensions
173 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_