OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ |
7 | 7 |
8 #include "base/directory_watcher.h" | 8 #include "base/directory_watcher.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/process.h" | 11 #include "base/process.h" |
12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
13 #include "base/shared_memory.h" | 13 #include "base/shared_memory.h" |
14 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 | 16 |
| 17 class Pickle; |
| 18 |
17 struct UserScriptInfo { | 19 struct UserScriptInfo { |
18 GURL url; | 20 GURL url; |
19 FilePath path; | 21 FilePath path; |
20 std::vector<std::string> matches; | 22 std::vector<std::string> matches; |
21 }; | 23 }; |
22 typedef std::vector<UserScriptInfo> UserScriptList; | 24 typedef std::vector<UserScriptInfo> UserScriptList; |
23 | 25 |
24 // Manages a segment of shared memory that contains the user scripts the user | 26 // Manages a segment of shared memory that contains the user scripts the user |
25 // has installed. Lives on the UI thread. | 27 // has installed. Lives on the UI thread. |
26 class UserScriptMaster : public base::RefCounted<UserScriptMaster>, | 28 class UserScriptMaster : public base::RefCounted<UserScriptMaster>, |
27 public DirectoryWatcher::Delegate { | 29 public DirectoryWatcher::Delegate { |
28 public: | 30 public: |
29 // For testability, the constructor takes the MessageLoop to run the | 31 // For testability, the constructor takes the MessageLoop to run the |
30 // script-reloading worker on as well as the path the scripts live in. | 32 // script-reloading worker on as well as the path the scripts live in. |
31 // These are normally the file thread and a directory inside the profile. | 33 // These are normally the file thread and a directory inside the profile. |
32 UserScriptMaster(MessageLoop* worker, const FilePath& script_dir); | 34 UserScriptMaster(MessageLoop* worker, const FilePath& script_dir); |
33 ~UserScriptMaster(); | 35 ~UserScriptMaster(); |
34 | 36 |
| 37 // Add a single user script that exists outside the script directory. |
| 38 void AddLoneScript(const UserScriptInfo& script) { |
| 39 lone_scripts_.push_back(script); |
| 40 } |
| 41 |
| 42 // Add a watched directory. All scripts will be reloaded when any file in |
| 43 // this directory changes. |
| 44 void AddWatchedPath(const FilePath& path); |
| 45 |
| 46 // Kicks off a process on the file thread to reload scripts from disk |
| 47 // into a new chunk of shared memory and notify renderers. |
| 48 void StartScan(); |
| 49 |
35 // Gets the segment of shared memory for the scripts. | 50 // Gets the segment of shared memory for the scripts. |
36 base::SharedMemory* GetSharedMemory() const { | 51 base::SharedMemory* GetSharedMemory() const { |
37 return shared_memory_.get(); | 52 return shared_memory_.get(); |
38 } | 53 } |
39 | 54 |
40 // Called by the script reloader when new scripts have been loaded. | 55 // Called by the script reloader when new scripts have been loaded. |
41 void NewScriptsAvailable(base::SharedMemory* handle); | 56 void NewScriptsAvailable(base::SharedMemory* handle); |
42 | 57 |
43 // Return true if we have any scripts ready. | 58 // Return true if we have any scripts ready. |
44 bool ScriptsReady() const { return shared_memory_.get() != NULL; } | 59 bool ScriptsReady() const { return shared_memory_.get() != NULL; } |
45 | 60 |
46 // Returns the path to the directory user scripts are stored in. | 61 // Returns the path to the directory user scripts are stored in. |
47 FilePath user_script_dir() const { return *user_script_dir_; } | 62 FilePath user_script_dir() const { return user_script_dir_; } |
48 | 63 |
49 private: | 64 private: |
50 FRIEND_TEST(UserScriptMasterTest, Parse1); | 65 FRIEND_TEST(UserScriptMasterTest, Parse1); |
51 FRIEND_TEST(UserScriptMasterTest, Parse2); | 66 FRIEND_TEST(UserScriptMasterTest, Parse2); |
52 FRIEND_TEST(UserScriptMasterTest, Parse3); | 67 FRIEND_TEST(UserScriptMasterTest, Parse3); |
53 | 68 |
54 // We reload user scripts on the file thread to prevent blocking the UI. | 69 // We reload user scripts on the file thread to prevent blocking the UI. |
55 // ScriptReloader lives on the file thread and does the reload | 70 // ScriptReloader lives on the file thread and does the reload |
56 // work, and then sends a message back to its master with a new SharedMemory*. | 71 // work, and then sends a message back to its master with a new SharedMemory*. |
57 // ScriptReloader is the worker that manages running the script scan | 72 // ScriptReloader is the worker that manages running the script scan |
58 // on the file thread. It must be created on, and its public API must only be | 73 // on the file thread. It must be created on, and its public API must only be |
59 // called from, the master's thread. | 74 // called from, the master's thread. |
60 class ScriptReloader | 75 class ScriptReloader |
61 : public base::RefCounted<UserScriptMaster::ScriptReloader> { | 76 : public base::RefCounted<UserScriptMaster::ScriptReloader> { |
62 public: | 77 public: |
63 // Parses the includes out of |script| and returns them in |includes|. | 78 // Parses the includes out of |script| and returns them in |includes|. |
64 static void ParseMetadataHeader(const StringPiece& script, | 79 static void ParseMetadataHeader(const StringPiece& script, |
65 std::vector<std::string>* includes); | 80 std::vector<std::string>* includes); |
66 | 81 |
67 ScriptReloader(UserScriptMaster* master) | 82 ScriptReloader(UserScriptMaster* master) |
68 : master_(master), master_message_loop_(MessageLoop::current()) {} | 83 : master_(master), master_message_loop_(MessageLoop::current()) {} |
69 | 84 |
70 // Start a scan for scripts. | 85 // Start a scan for scripts. |
71 // Will always send a message to the master upon completion. | 86 // Will always send a message to the master upon completion. |
72 void StartScan(MessageLoop* work_loop, const FilePath& script_dir); | 87 void StartScan(MessageLoop* work_loop, const FilePath& script_dir, |
| 88 const UserScriptList &external_scripts); |
73 | 89 |
74 // The master is going away; don't call it back. | 90 // The master is going away; don't call it back. |
75 void DisownMaster() { | 91 void DisownMaster() { |
76 master_ = NULL; | 92 master_ = NULL; |
77 } | 93 } |
78 | 94 |
79 private: | 95 private: |
80 // Where functions are run: | 96 // Where functions are run: |
81 // master file | 97 // master file |
82 // StartScan -> RunScan | 98 // StartScan -> RunScan |
83 // GetNewScripts() | 99 // GetNewScripts() |
84 // ParseMetadataHeader() | 100 // ParseMetadataHeader() |
85 // NotifyMaster <- RunScan | 101 // NotifyMaster <- RunScan |
86 | 102 |
87 // Runs on the master thread. | 103 // Runs on the master thread. |
88 // Notify the master that new scripts are available. | 104 // Notify the master that new scripts are available. |
89 void NotifyMaster(base::SharedMemory* memory); | 105 void NotifyMaster(base::SharedMemory* memory); |
90 | 106 |
91 // Runs on the File thread. | 107 // Runs on the File thread. |
92 // Scan the script directory for scripts, calling NotifyMaster when done. | 108 // Scan the specified directory and lone scripts, calling NotifyMaster when |
93 // The path is intentionally passed by value so its lifetime isn't tied | 109 // done. The parameters are intentionally passed by value so their lifetimes |
94 // to the caller. | 110 // aren't tied to the caller. |
95 void RunScan(const FilePath script_dir); | 111 void RunScan(const FilePath script_dir, const UserScriptList lone_scripts); |
96 | 112 |
97 // Runs on the File thread. | 113 // Runs on the File thread. |
98 // Scan the script directory for scripts, returning either a | 114 // Scan the script directory and lone scripts, returning either a new |
99 // new SharedMemory or NULL on error. | 115 // SharedMemory or NULL on error. |
100 base::SharedMemory* GetNewScripts(const FilePath& script_dir); | 116 base::SharedMemory* GetNewScripts(const FilePath& script_dir, |
| 117 const UserScriptList& lone_scripts); |
| 118 |
| 119 // Serialize script metadata and contents into the specified pickle. |
| 120 void PickleScriptData(const UserScriptInfo& script, |
| 121 const std::string& contents, Pickle* pickle); |
101 | 122 |
102 // A pointer back to our master. | 123 // A pointer back to our master. |
103 // May be NULL if DisownMaster() is called. | 124 // May be NULL if DisownMaster() is called. |
104 UserScriptMaster* master_; | 125 UserScriptMaster* master_; |
105 | 126 |
106 // The message loop to call our master back on. | 127 // The message loop to call our master back on. |
107 // Expected to always outlive us. | 128 // Expected to always outlive us. |
108 MessageLoop* master_message_loop_; | 129 MessageLoop* master_message_loop_; |
109 | 130 |
110 DISALLOW_COPY_AND_ASSIGN(ScriptReloader); | 131 DISALLOW_COPY_AND_ASSIGN(ScriptReloader); |
111 }; | 132 }; |
112 | 133 |
113 // DirectoryWatcher::Delegate implementation. | 134 // DirectoryWatcher::Delegate implementation. |
114 virtual void OnDirectoryChanged(const FilePath& path); | 135 virtual void OnDirectoryChanged(const FilePath& path); |
115 | 136 |
116 // Kicks off a process on the file thread to reload scripts from disk | 137 // The directories containing user scripts. |
117 // into a new chunk of shared memory and notify renderers. | 138 FilePath user_script_dir_; |
118 void StartScan(); | |
119 | |
120 // The directory containing user scripts. | |
121 scoped_ptr<FilePath> user_script_dir_; | |
122 | 139 |
123 // The watcher watches the profile's user scripts directory for new scripts. | 140 // The watcher watches the profile's user scripts directory for new scripts. |
124 scoped_ptr<DirectoryWatcher> dir_watcher_; | 141 std::vector<DirectoryWatcher*> dir_watchers_; |
125 | 142 |
126 // The MessageLoop that the scanner worker runs on. | 143 // The MessageLoop that the scanner worker runs on. |
127 // Typically the file thread; configurable for testing. | 144 // Typically the file thread; configurable for testing. |
128 MessageLoop* worker_loop_; | 145 MessageLoop* worker_loop_; |
129 | 146 |
130 // ScriptReloader (in another thread) reloads script off disk. | 147 // ScriptReloader (in another thread) reloads script off disk. |
131 // We hang on to our pointer to know if we've already got one running. | 148 // We hang on to our pointer to know if we've already got one running. |
132 scoped_refptr<ScriptReloader> script_reloader_; | 149 scoped_refptr<ScriptReloader> script_reloader_; |
133 | 150 |
134 // Contains the scripts that were found the last time scripts were updated. | 151 // Contains the scripts that were found the last time scripts were updated. |
135 scoped_ptr<base::SharedMemory> shared_memory_; | 152 scoped_ptr<base::SharedMemory> shared_memory_; |
136 | 153 |
| 154 // List of scripts outside of script directories we should also load. |
| 155 UserScriptList lone_scripts_; |
| 156 |
137 // If the script directory is modified while we're rescanning it, we note | 157 // If the script directory is modified while we're rescanning it, we note |
138 // that we're currently mid-scan and then start over again once the scan | 158 // that we're currently mid-scan and then start over again once the scan |
139 // finishes. This boolean tracks whether another scan is pending. | 159 // finishes. This boolean tracks whether another scan is pending. |
140 bool pending_scan_; | 160 bool pending_scan_; |
141 | 161 |
142 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); | 162 DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); |
143 }; | 163 }; |
144 | 164 |
145 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ | 165 #endif // CHROME_BROWSER_EXTENSIONS_USER_SCRIPT_MASTER_H_ |
OLD | NEW |