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 #include "chrome/browser/extensions/user_script_master.h" | 5 #include "chrome/browser/extensions/user_script_master.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 if (!line.starts_with(prefix)) | 29 if (!line.starts_with(prefix)) |
30 return false; | 30 return false; |
31 | 31 |
32 std::string temp(line.data() + prefix.length(), | 32 std::string temp(line.data() + prefix.length(), |
33 line.length() - prefix.length()); | 33 line.length() - prefix.length()); |
34 TrimWhitespaceASCII(temp, TRIM_ALL, value); | 34 TrimWhitespaceASCII(temp, TRIM_ALL, value); |
35 return true; | 35 return true; |
36 } | 36 } |
37 | 37 |
38 UserScriptMaster::ScriptReloader::ScriptReloader(UserScriptMaster* master) | 38 UserScriptMaster::ScriptReloader::ScriptReloader(UserScriptMaster* master) |
39 : master_(master), | 39 : master_(master) { |
40 master_message_loop_(MessageLoop::current()) { | 40 CHECK(ChromeThread::GetCurrentThreadIdentifier(&master_thread_id_)); |
41 } | 41 } |
42 | 42 |
43 // static | 43 // static |
44 bool UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 44 bool UserScriptMaster::ScriptReloader::ParseMetadataHeader( |
45 const base::StringPiece& script_text, UserScript* script) { | 45 const base::StringPiece& script_text, UserScript* script) { |
46 // http://wiki.greasespot.net/Metadata_block | 46 // http://wiki.greasespot.net/Metadata_block |
47 base::StringPiece line; | 47 base::StringPiece line; |
48 size_t line_start = 0; | 48 size_t line_start = 0; |
49 size_t line_end = 0; | 49 size_t line_end = 0; |
50 bool in_metadata = false; | 50 bool in_metadata = false; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 103 |
104 // If no patterns were specified, default to @include *. This is what | 104 // If no patterns were specified, default to @include *. This is what |
105 // Greasemonkey does. | 105 // Greasemonkey does. |
106 if (script->globs().size() == 0 && script->url_patterns().size() == 0) | 106 if (script->globs().size() == 0 && script->url_patterns().size() == 0) |
107 script->add_glob("*"); | 107 script->add_glob("*"); |
108 | 108 |
109 return true; | 109 return true; |
110 } | 110 } |
111 | 111 |
112 void UserScriptMaster::ScriptReloader::StartScan( | 112 void UserScriptMaster::ScriptReloader::StartScan( |
113 MessageLoop* work_loop, const FilePath& script_dir, | 113 const FilePath& script_dir, const UserScriptList& lone_scripts) { |
114 const UserScriptList& lone_scripts) { | |
115 // Add a reference to ourselves to keep ourselves alive while we're running. | 114 // Add a reference to ourselves to keep ourselves alive while we're running. |
116 // Balanced by NotifyMaster(). | 115 // Balanced by NotifyMaster(). |
117 AddRef(); | 116 AddRef(); |
118 work_loop->PostTask(FROM_HERE, | 117 ChromeThread::PostTask( |
119 NewRunnableMethod(this, | 118 ChromeThread::FILE, FROM_HERE, |
120 &UserScriptMaster::ScriptReloader::RunScan, | 119 NewRunnableMethod( |
121 script_dir, lone_scripts)); | 120 this, &UserScriptMaster::ScriptReloader::RunScan, script_dir, |
| 121 lone_scripts)); |
122 } | 122 } |
123 | 123 |
124 void UserScriptMaster::ScriptReloader::NotifyMaster( | 124 void UserScriptMaster::ScriptReloader::NotifyMaster( |
125 base::SharedMemory* memory) { | 125 base::SharedMemory* memory) { |
126 // The master went away, so these new scripts aren't useful anymore. | 126 // The master went away, so these new scripts aren't useful anymore. |
127 if (!master_) | 127 if (!master_) |
128 delete memory; | 128 delete memory; |
129 else | 129 else |
130 master_->NewScriptsAvailable(memory); | 130 master_->NewScriptsAvailable(memory); |
131 | 131 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 LoadLoneScripts(&lone_script); | 245 LoadLoneScripts(&lone_script); |
246 | 246 |
247 // Merge with the explicit scripts | 247 // Merge with the explicit scripts |
248 scripts.reserve(scripts.size() + lone_script.size()); | 248 scripts.reserve(scripts.size() + lone_script.size()); |
249 scripts.insert(scripts.end(), | 249 scripts.insert(scripts.end(), |
250 lone_script.begin(), lone_script.end()); | 250 lone_script.begin(), lone_script.end()); |
251 | 251 |
252 // Scripts now contains list of up-to-date scripts. Load the content in the | 252 // Scripts now contains list of up-to-date scripts. Load the content in the |
253 // shared memory and let the master know it's ready. We need to post the task | 253 // shared memory and let the master know it's ready. We need to post the task |
254 // back even if no scripts ware found to balance the AddRef/Release calls | 254 // back even if no scripts ware found to balance the AddRef/Release calls |
255 master_message_loop_->PostTask(FROM_HERE, | 255 ChromeThread::PostTask( |
256 NewRunnableMethod(this, | 256 master_thread_id_, FROM_HERE, |
257 &ScriptReloader::NotifyMaster, | 257 NewRunnableMethod( |
258 Serialize(scripts))); | 258 this, &ScriptReloader::NotifyMaster, Serialize(scripts))); |
259 } | 259 } |
260 | 260 |
261 | 261 |
262 UserScriptMaster::UserScriptMaster(MessageLoop* worker_loop, | 262 UserScriptMaster::UserScriptMaster(const FilePath& script_dir) |
263 const FilePath& script_dir) | |
264 : user_script_dir_(script_dir), | 263 : user_script_dir_(script_dir), |
265 worker_loop_(worker_loop), | |
266 extensions_service_ready_(false), | 264 extensions_service_ready_(false), |
267 pending_scan_(false) { | 265 pending_scan_(false) { |
268 if (!user_script_dir_.value().empty()) | 266 if (!user_script_dir_.value().empty()) |
269 AddWatchedPath(script_dir); | 267 AddWatchedPath(script_dir); |
270 | 268 |
271 registrar_.Add(this, NotificationType::EXTENSIONS_READY, | 269 registrar_.Add(this, NotificationType::EXTENSIONS_READY, |
272 NotificationService::AllSources()); | 270 NotificationService::AllSources()); |
273 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 271 registrar_.Add(this, NotificationType::EXTENSION_LOADED, |
274 NotificationService::AllSources()); | 272 NotificationService::AllSources()); |
275 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 273 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 374 |
377 default: | 375 default: |
378 DCHECK(false); | 376 DCHECK(false); |
379 } | 377 } |
380 } | 378 } |
381 | 379 |
382 void UserScriptMaster::StartScan() { | 380 void UserScriptMaster::StartScan() { |
383 if (!script_reloader_) | 381 if (!script_reloader_) |
384 script_reloader_ = new ScriptReloader(this); | 382 script_reloader_ = new ScriptReloader(this); |
385 | 383 |
386 script_reloader_->StartScan(worker_loop_, user_script_dir_, lone_scripts_); | 384 script_reloader_->StartScan(user_script_dir_, lone_scripts_); |
387 } | 385 } |
OLD | NEW |