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* unused) { | |
33 if (g_browser_process->IsShuttingDown() || | |
34 !g_browser_process->profile_manager()->IsValidProfile(profile)) | |
35 return; | |
36 ExtensionSystem* system = ExtensionSystem::Get(profile); | |
37 if (!system) | |
38 return; | |
39 | |
40 // If this is a persistent background page, we want to wait for it to load | |
41 // (it might not be ready, since this is startup). But only enqueue once. | |
42 // If it fails to load the first time, don't bother trying again. | |
43 const Extension* extension = | |
44 system->extension_service()->extensions()->GetByID(extension_id); | |
45 if (extension && extension->has_persistent_background_page() && first_call && | |
Matt Perry
2012/08/16 22:06:45
I had to add a check for has_persistent_background
| |
46 system->lazy_background_task_queue()-> | |
47 ShouldEnqueueTask(profile, extension)) { | |
48 LOG(ERROR) << "DispatchOnStartup LATER: " << extension_id; | |
49 system->lazy_background_task_queue()->AddPendingTask( | |
50 profile, extension_id, | |
51 base::Bind(&DispatchOnStartupEventImpl, | |
52 profile, extension_id, false)); | |
53 return; | |
54 } | |
55 | |
56 LOG(ERROR) << "DispatchOnStartup NOW: " << extension_id; | |
57 scoped_ptr<ListValue> event_args(new ListValue()); | |
58 system->event_router()->DispatchEventToExtension( | |
59 extension_id, kOnStartupEvent, event_args.Pass(), NULL, GURL()); | |
22 } | 60 } |
23 | 61 |
24 namespace extensions { | 62 } // namespace |
63 | |
64 // static | |
65 void RuntimeEventRouter::DispatchOnStartupEvent( | |
66 Profile* profile, const std::string& extension_id) { | |
67 DispatchOnStartupEventImpl(profile, extension_id, true, NULL); | |
68 } | |
25 | 69 |
26 // static | 70 // static |
27 void RuntimeEventRouter::DispatchOnInstalledEvent( | 71 void RuntimeEventRouter::DispatchOnInstalledEvent( |
28 Profile* profile, const std::string& extension_id) { | 72 Profile* profile, const std::string& extension_id) { |
29 ExtensionSystem* system = ExtensionSystem::Get(profile); | 73 ExtensionSystem* system = ExtensionSystem::Get(profile); |
30 if (!system) | 74 if (!system) |
31 return; | 75 return; |
32 | 76 |
77 LOG(ERROR) << "DispatchOnInstalled: " << extension_id; | |
33 // Special case: normally, extensions add their own lazy event listeners. | 78 // Special case: normally, extensions add their own lazy event listeners. |
34 // However, since the extension has just been installed, it hasn't had a | 79 // However, since the extension has just been installed, it hasn't had a |
35 // chance to register for events. So we register on its behalf. If the | 80 // chance to register for events. So we register on its behalf. If the |
36 // extension does not actually have a listener, the event will just be | 81 // extension does not actually have a listener, the event will just be |
37 // ignored. | 82 // ignored. |
38 scoped_ptr<ListValue> event_args(new ListValue()); | 83 scoped_ptr<ListValue> event_args(new ListValue()); |
39 system->event_router()->AddLazyEventListener(kOnInstalledEvent, extension_id); | 84 system->event_router()->AddLazyEventListener(kOnInstalledEvent, extension_id); |
40 system->event_router()->DispatchEventToExtension( | 85 system->event_router()->DispatchEventToExtension( |
41 extension_id, kOnInstalledEvent, event_args.Pass(), NULL, GURL()); | 86 extension_id, kOnInstalledEvent, event_args.Pass(), NULL, GURL()); |
42 } | 87 } |
(...skipping 21 matching lines...) Expand all Loading... | |
64 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) { | 109 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) { |
65 if (host) { | 110 if (host) { |
66 SendResponse(true); | 111 SendResponse(true); |
67 } else { | 112 } else { |
68 error_ = kPageLoadError; | 113 error_ = kPageLoadError; |
69 SendResponse(false); | 114 SendResponse(false); |
70 } | 115 } |
71 } | 116 } |
72 | 117 |
73 } // namespace extensions | 118 } // namespace extensions |
OLD | NEW |