Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/api/runtime/runtime_api.h" | 5 #include "chrome/browser/extensions/api/runtime/runtime_api.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser_process.h" | |
| 7 #include "chrome/browser/extensions/event_router.h" | 8 #include "chrome/browser/extensions/event_router.h" |
| 8 #include "chrome/browser/extensions/extension_host.h" | 9 #include "chrome/browser/extensions/extension_host.h" |
| 9 #include "chrome/browser/extensions/extension_process_manager.h" | 10 #include "chrome/browser/extensions/extension_process_manager.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 10 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/extensions/lazy_background_task_queue.h" | 13 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | |
| 13 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 14 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 15 | 18 |
| 19 namespace extensions { | |
| 20 | |
| 16 namespace { | 21 namespace { |
| 17 | 22 |
| 23 const char kOnStartupEvent[] = "runtime.onStartup"; | |
| 18 const char kOnInstalledEvent[] = "runtime.onInstalled"; | 24 const char kOnInstalledEvent[] = "runtime.onInstalled"; |
| 19 const char kNoBackgroundPageError[] = "You do not have a background page."; | 25 const char kNoBackgroundPageError[] = "You do not have a background page."; |
| 20 const char kPageLoadError[] = "Background page failed to load."; | 26 const char kPageLoadError[] = "Background page failed to load."; |
| 21 | 27 |
| 28 static void DispatchOnStartupEventImpl( | |
| 29 Profile* profile, | |
| 30 const std::string& extension_id, | |
| 31 bool first_call, | |
| 32 ExtensionHost* host) { | |
| 33 // A NULL host from the LazyBackgroundTaskQueue means the page failed to | |
| 34 // load. Give up. | |
| 35 if (!host && !first_call) | |
| 36 return; | |
|
Matt Perry
2012/08/17 23:37:30
This should fix the crash. We were crashing when t
| |
| 37 | |
| 38 if (g_browser_process->IsShuttingDown() || | |
| 39 !g_browser_process->profile_manager()->IsValidProfile(profile)) | |
| 40 return; | |
| 41 ExtensionSystem* system = ExtensionSystem::Get(profile); | |
| 42 if (!system) | |
| 43 return; | |
| 44 | |
| 45 // If this is a persistent background page, we want to wait for it to load | |
| 46 // (it might not be ready, since this is startup). But only enqueue once. | |
| 47 // If it fails to load the first time, don't bother trying again. | |
| 48 const Extension* extension = | |
| 49 system->extension_service()->extensions()->GetByID(extension_id); | |
| 50 if (extension && extension->has_persistent_background_page() && first_call && | |
| 51 system->lazy_background_task_queue()-> | |
| 52 ShouldEnqueueTask(profile, extension)) { | |
| 53 system->lazy_background_task_queue()->AddPendingTask( | |
| 54 profile, extension_id, | |
| 55 base::Bind(&DispatchOnStartupEventImpl, | |
| 56 profile, extension_id, false)); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 scoped_ptr<ListValue> event_args(new ListValue()); | |
| 61 system->event_router()->DispatchEventToExtension( | |
| 62 extension_id, kOnStartupEvent, event_args.Pass(), NULL, GURL()); | |
| 22 } | 63 } |
| 23 | 64 |
| 24 namespace extensions { | 65 } // namespace |
| 66 | |
| 67 // static | |
| 68 void RuntimeEventRouter::DispatchOnStartupEvent( | |
| 69 Profile* profile, const std::string& extension_id) { | |
| 70 DispatchOnStartupEventImpl(profile, extension_id, true, NULL); | |
| 71 } | |
| 25 | 72 |
| 26 // static | 73 // static |
| 27 void RuntimeEventRouter::DispatchOnInstalledEvent( | 74 void RuntimeEventRouter::DispatchOnInstalledEvent( |
| 28 Profile* profile, const std::string& extension_id) { | 75 Profile* profile, const std::string& extension_id) { |
| 29 ExtensionSystem* system = ExtensionSystem::Get(profile); | 76 ExtensionSystem* system = ExtensionSystem::Get(profile); |
| 30 if (!system) | 77 if (!system) |
| 31 return; | 78 return; |
| 32 | 79 |
| 33 // Special case: normally, extensions add their own lazy event listeners. | 80 // Special case: normally, extensions add their own lazy event listeners. |
| 34 // However, since the extension has just been installed, it hasn't had a | 81 // However, since the extension has just been installed, it hasn't had a |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 64 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) { | 111 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) { |
| 65 if (host) { | 112 if (host) { |
| 66 SendResponse(true); | 113 SendResponse(true); |
| 67 } else { | 114 } else { |
| 68 error_ = kPageLoadError; | 115 error_ = kPageLoadError; |
| 69 SendResponse(false); | 116 SendResponse(false); |
| 70 } | 117 } |
| 71 } | 118 } |
| 72 | 119 |
| 73 } // namespace extensions | 120 } // namespace extensions |
| OLD | NEW |