OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/pickle.h" | 12 #include "base/pickle.h" |
13 #include "base/stl_util-inl.h" | 13 #include "base/stl_util-inl.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "base/version.h" | 16 #include "base/version.h" |
17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/extensions/extension.h" | 20 #include "chrome/common/extensions/extension.h" |
20 #include "chrome/common/extensions/extension_messages.h" | 21 #include "chrome/common/extensions/extension_messages.h" |
21 #include "chrome/common/extensions/extension_resource.h" | 22 #include "chrome/common/extensions/extension_resource.h" |
22 #include "content/browser/renderer_host/render_process_host.h" | 23 #include "content/browser/renderer_host/render_process_host.h" |
23 #include "content/common/notification_service.h" | 24 #include "content/common/notification_service.h" |
24 | 25 |
25 // Helper function to parse greasesmonkey headers | 26 // Helper function to parse greasesmonkey headers |
26 static bool GetDeclarationValue(const base::StringPiece& line, | 27 static bool GetDeclarationValue(const base::StringPiece& line, |
27 const base::StringPiece& prefix, | 28 const base::StringPiece& prefix, |
28 std::string* value) { | 29 std::string* value) { |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 master_thread_id_, FROM_HERE, | 244 master_thread_id_, FROM_HERE, |
244 NewRunnableMethod( | 245 NewRunnableMethod( |
245 this, &ScriptReloader::NotifyMaster, Serialize(user_scripts))); | 246 this, &ScriptReloader::NotifyMaster, Serialize(user_scripts))); |
246 } | 247 } |
247 | 248 |
248 | 249 |
249 UserScriptMaster::UserScriptMaster(Profile* profile) | 250 UserScriptMaster::UserScriptMaster(Profile* profile) |
250 : extensions_service_ready_(false), | 251 : extensions_service_ready_(false), |
251 pending_load_(false), | 252 pending_load_(false), |
252 profile_(profile) { | 253 profile_(profile) { |
253 registrar_.Add(this, NotificationType::EXTENSIONS_READY, | 254 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
254 Source<Profile>(profile_)); | 255 Source<Profile>(profile_)); |
255 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 256 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
256 Source<Profile>(profile_)); | 257 Source<Profile>(profile_)); |
257 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 258 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
258 Source<Profile>(profile_)); | 259 Source<Profile>(profile_)); |
259 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED, | 260 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
260 NotificationService::AllSources()); | 261 NotificationService::AllSources()); |
261 } | 262 } |
262 | 263 |
263 UserScriptMaster::~UserScriptMaster() { | 264 UserScriptMaster::~UserScriptMaster() { |
264 if (script_reloader_) | 265 if (script_reloader_) |
265 script_reloader_->DisownMaster(); | 266 script_reloader_->DisownMaster(); |
266 } | 267 } |
267 | 268 |
268 void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) { | 269 void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) { |
269 // Ensure handle is deleted or released. | 270 // Ensure handle is deleted or released. |
270 scoped_ptr<base::SharedMemory> handle_deleter(handle); | 271 scoped_ptr<base::SharedMemory> handle_deleter(handle); |
271 | 272 |
272 if (pending_load_) { | 273 if (pending_load_) { |
273 // While we were loading, there were further changes. Don't bother | 274 // While we were loading, there were further changes. Don't bother |
274 // notifying about these scripts and instead just immediately reload. | 275 // notifying about these scripts and instead just immediately reload. |
275 pending_load_ = false; | 276 pending_load_ = false; |
276 StartLoad(); | 277 StartLoad(); |
277 } else { | 278 } else { |
278 // We're no longer loading. | 279 // We're no longer loading. |
279 script_reloader_ = NULL; | 280 script_reloader_ = NULL; |
280 // We've got scripts ready to go. | 281 // We've got scripts ready to go. |
281 shared_memory_.swap(handle_deleter); | 282 shared_memory_.swap(handle_deleter); |
282 | 283 |
283 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | 284 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
284 !i.IsAtEnd(); i.Advance()) { | 285 !i.IsAtEnd(); i.Advance()) { |
285 SendUpdate(i.GetCurrentValue(), handle); | 286 SendUpdate(i.GetCurrentValue(), handle); |
286 } | 287 } |
287 | 288 |
288 NotificationService::current()->Notify( | 289 NotificationService::current()->Notify( |
289 NotificationType::USER_SCRIPTS_UPDATED, | 290 chrome::NOTIFICATION_USER_SCRIPTS_UPDATED, |
290 Source<Profile>(profile_), | 291 Source<Profile>(profile_), |
291 Details<base::SharedMemory>(handle)); | 292 Details<base::SharedMemory>(handle)); |
292 } | 293 } |
293 } | 294 } |
294 | 295 |
295 void UserScriptMaster::Observe(NotificationType type, | 296 void UserScriptMaster::Observe(int type, |
296 const NotificationSource& source, | 297 const NotificationSource& source, |
297 const NotificationDetails& details) { | 298 const NotificationDetails& details) { |
298 bool should_start_load = false; | 299 bool should_start_load = false; |
299 switch (type.value) { | 300 switch (type) { |
300 case NotificationType::EXTENSIONS_READY: | 301 case chrome::NOTIFICATION_EXTENSIONS_READY: |
301 extensions_service_ready_ = true; | 302 extensions_service_ready_ = true; |
302 should_start_load = true; | 303 should_start_load = true; |
303 break; | 304 break; |
304 case NotificationType::EXTENSION_LOADED: { | 305 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
305 // Add any content scripts inside the extension. | 306 // Add any content scripts inside the extension. |
306 const Extension* extension = Details<const Extension>(details).ptr(); | 307 const Extension* extension = Details<const Extension>(details).ptr(); |
307 bool incognito_enabled = profile_->GetExtensionService()-> | 308 bool incognito_enabled = profile_->GetExtensionService()-> |
308 IsIncognitoEnabled(extension->id()); | 309 IsIncognitoEnabled(extension->id()); |
309 const UserScriptList& scripts = extension->content_scripts(); | 310 const UserScriptList& scripts = extension->content_scripts(); |
310 for (UserScriptList::const_iterator iter = scripts.begin(); | 311 for (UserScriptList::const_iterator iter = scripts.begin(); |
311 iter != scripts.end(); ++iter) { | 312 iter != scripts.end(); ++iter) { |
312 user_scripts_.push_back(*iter); | 313 user_scripts_.push_back(*iter); |
313 user_scripts_.back().set_incognito_enabled(incognito_enabled); | 314 user_scripts_.back().set_incognito_enabled(incognito_enabled); |
314 } | 315 } |
315 if (extensions_service_ready_) | 316 if (extensions_service_ready_) |
316 should_start_load = true; | 317 should_start_load = true; |
317 break; | 318 break; |
318 } | 319 } |
319 case NotificationType::EXTENSION_UNLOADED: { | 320 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
320 // Remove any content scripts. | 321 // Remove any content scripts. |
321 const Extension* extension = | 322 const Extension* extension = |
322 Details<UnloadedExtensionInfo>(details)->extension; | 323 Details<UnloadedExtensionInfo>(details)->extension; |
323 UserScriptList new_user_scripts; | 324 UserScriptList new_user_scripts; |
324 for (UserScriptList::iterator iter = user_scripts_.begin(); | 325 for (UserScriptList::iterator iter = user_scripts_.begin(); |
325 iter != user_scripts_.end(); ++iter) { | 326 iter != user_scripts_.end(); ++iter) { |
326 if (iter->extension_id() != extension->id()) | 327 if (iter->extension_id() != extension->id()) |
327 new_user_scripts.push_back(*iter); | 328 new_user_scripts.push_back(*iter); |
328 } | 329 } |
329 user_scripts_ = new_user_scripts; | 330 user_scripts_ = new_user_scripts; |
330 should_start_load = true; | 331 should_start_load = true; |
331 | 332 |
332 // TODO(aa): Do we want to do something smarter for the scripts that have | 333 // TODO(aa): Do we want to do something smarter for the scripts that have |
333 // already been injected? | 334 // already been injected? |
334 | 335 |
335 break; | 336 break; |
336 } | 337 } |
337 case NotificationType::RENDERER_PROCESS_CREATED: { | 338 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { |
338 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr(); | 339 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr(); |
339 if (ScriptsReady()) | 340 if (ScriptsReady()) |
340 SendUpdate(process, GetSharedMemory()); | 341 SendUpdate(process, GetSharedMemory()); |
341 break; | 342 break; |
342 } | 343 } |
343 default: | 344 default: |
344 DCHECK(false); | 345 DCHECK(false); |
345 } | 346 } |
346 | 347 |
347 if (should_start_load) { | 348 if (should_start_load) { |
(...skipping 24 matching lines...) Expand all Loading... |
372 if (!handle) | 373 if (!handle) |
373 return; | 374 return; |
374 | 375 |
375 base::SharedMemoryHandle handle_for_process; | 376 base::SharedMemoryHandle handle_for_process; |
376 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) | 377 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) |
377 return; // This can legitimately fail if the renderer asserts at startup. | 378 return; // This can legitimately fail if the renderer asserts at startup. |
378 | 379 |
379 if (base::SharedMemory::IsHandleValid(handle_for_process)) | 380 if (base::SharedMemory::IsHandleValid(handle_for_process)) |
380 process->Send(new ExtensionMsg_UpdateUserScripts(handle_for_process)); | 381 process->Send(new ExtensionMsg_UpdateUserScripts(handle_for_process)); |
381 } | 382 } |
OLD | NEW |