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_
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/shared_memory.h"
16 #include "base/strings/string_piece.h"
17 #include "chrome/browser/extensions/extension_info_map.h"
18 #include "chrome/common/extensions/extension_messages.h"
19 #include "chrome/common/extensions/extension_set.h"
20 #include "chrome/common/extensions/user_script.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
26 class RenderProcessHost
;
31 typedef std::map
<std::string
, ExtensionSet::ExtensionPathAndDefaultLocale
>
34 namespace extensions
{
36 // Manages a segment of shared memory that contains the user scripts the user
37 // has installed. Lives on the UI thread.
38 class UserScriptMaster
: public base::RefCountedThreadSafe
<UserScriptMaster
>,
39 public content::NotificationObserver
{
41 explicit UserScriptMaster(Profile
* profile
);
43 // Kicks off a process on the file thread to reload scripts from disk
44 // into a new chunk of shared memory and notify renderers.
45 virtual void StartLoad();
47 // Gets the segment of shared memory for the scripts.
48 base::SharedMemory
* GetSharedMemory() const {
49 return shared_memory_
.get();
52 // Called by the script reloader when new scripts have been loaded.
53 void NewScriptsAvailable(base::SharedMemory
* handle
);
55 // Return true if we have any scripts ready.
56 bool ScriptsReady() const { return shared_memory_
.get() != NULL
; }
59 friend class base::RefCountedThreadSafe
<UserScriptMaster
>;
61 virtual ~UserScriptMaster();
64 // We reload user scripts on the file thread to prevent blocking the UI.
65 // ScriptReloader lives on the file thread and does the reload
66 // work, and then sends a message back to its master with a new SharedMemory*.
67 // ScriptReloader is the worker that manages running the script load
68 // on the file thread. It must be created on, and its public API must only be
69 // called from, the master's thread.
71 : public base::RefCountedThreadSafe
<UserScriptMaster::ScriptReloader
> {
73 // Parses the includes out of |script| and returns them in |includes|.
74 static bool ParseMetadataHeader(const base::StringPiece
& script_text
,
77 explicit ScriptReloader(UserScriptMaster
* master
);
79 // Start loading of scripts.
80 // Will always send a message to the master upon completion.
81 void StartLoad(const UserScriptList
& external_scripts
,
82 const ExtensionsInfo
& extension_info_
);
84 // The master is going away; don't call it back.
90 FRIEND_TEST_ALL_PREFIXES(UserScriptMasterTest
, SkipBOMAtTheBeginning
);
91 FRIEND_TEST_ALL_PREFIXES(UserScriptMasterTest
, LeaveBOMNotAtTheBeginning
);
92 friend class base::RefCountedThreadSafe
<UserScriptMaster::ScriptReloader
>;
96 // Where functions are run:
98 // StartLoad -> RunLoad
100 // NotifyMaster <- RunLoad
102 // Runs on the master thread.
103 // Notify the master that new scripts are available.
104 void NotifyMaster(base::SharedMemory
* memory
);
106 // Runs on the File thread.
107 // Load the specified user scripts, calling NotifyMaster when done.
108 // |user_scripts| is intentionally passed by value so its lifetime isn't
109 // tied to the caller.
110 void RunLoad(const UserScriptList
& user_scripts
);
112 void LoadUserScripts(UserScriptList
* user_scripts
);
114 // Uses extensions_info_ to build a map of localization messages.
115 // Returns NULL if |extension_id| is invalid.
116 SubstitutionMap
* GetLocalizationMessages(std::string extension_id
);
118 // A pointer back to our master.
119 // May be NULL if DisownMaster() is called.
120 UserScriptMaster
* master_
;
122 // Maps extension info needed for localization to an extension ID.
123 ExtensionsInfo extensions_info_
;
125 // The message loop to call our master back on.
126 // Expected to always outlive us.
127 content::BrowserThread::ID master_thread_id_
;
129 DISALLOW_COPY_AND_ASSIGN(ScriptReloader
);
133 // content::NotificationObserver implementation.
134 virtual void Observe(int type
,
135 const content::NotificationSource
& source
,
136 const content::NotificationDetails
& details
) OVERRIDE
;
138 // Sends the renderer process a new set of user scripts.
139 void SendUpdate(content::RenderProcessHost
* process
,
140 base::SharedMemory
* shared_memory
);
142 // Manages our notification registrations.
143 content::NotificationRegistrar registrar_
;
145 // We hang on to our pointer to know if we've already got one running.
146 scoped_refptr
<ScriptReloader
> script_reloader_
;
148 // Contains the scripts that were found the last time scripts were updated.
149 scoped_ptr
<base::SharedMemory
> shared_memory_
;
151 // List of scripts from currently-installed extensions we should load.
152 UserScriptList user_scripts_
;
154 // Maps extension info needed for localization to an extension ID.
155 ExtensionsInfo extensions_info_
;
157 // If the extensions service has finished loading its initial set of
159 bool extensions_service_ready_
;
161 // If list of user scripts is modified while we're loading it, we note
162 // that we're currently mid-load and then start over again once the load
163 // finishes. This boolean tracks whether another load is pending.
166 // The profile for which the scripts managed here are installed.
169 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster
);
172 } // namespace extensions
174 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_