Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/extensions/api/runtime/runtime_api.cc

Issue 10828218: Add chrome.runtime.onStartup, which is fired on browser start. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 should_enqueue,
32 ExtensionHost* unused) {
33 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
34 return;
35 ExtensionSystem* system = ExtensionSystem::Get(profile);
36 if (!system)
37 return;
38
39 // Only check if we should enqueue the first time. If the background page
40 // fails to load, we don't want to re-enqueue the task.
41 const Extension* extension =
42 system->extension_service()->extensions()->GetByID(extension_id);
43 if (extension && should_enqueue &&
44 system->lazy_background_task_queue()->
45 ShouldEnqueueTask(profile, extension)) {
46 system->lazy_background_task_queue()->AddPendingTask(
47 profile, extension_id,
48 base::Bind(&DispatchOnStartupEventImpl,
49 profile, extension_id, false));
50 return;
51 }
52
53 scoped_ptr<ListValue> event_args(new ListValue());
54 system->event_router()->DispatchEventToExtension(
55 extension_id, kOnStartupEvent, event_args.Pass(), NULL, GURL());
22 } 56 }
23 57
24 namespace extensions { 58 } // namespace
59
60 // static
61 void RuntimeEventRouter::DispatchOnStartupEvent(
62 Profile* profile, const std::string& extension_id) {
63 DispatchOnStartupEventImpl(profile, extension_id, true, NULL);
64 }
25 65
26 // static 66 // static
27 void RuntimeEventRouter::DispatchOnInstalledEvent( 67 void RuntimeEventRouter::DispatchOnInstalledEvent(
28 Profile* profile, const std::string& extension_id) { 68 Profile* profile, const std::string& extension_id) {
69 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
Yoyo Zhou 2012/08/09 00:49:17 What is this for?
Matt Perry 2012/08/15 20:35:45 This method is called via a PostTask, so the profi
70 return;
29 ExtensionSystem* system = ExtensionSystem::Get(profile); 71 ExtensionSystem* system = ExtensionSystem::Get(profile);
30 if (!system) 72 if (!system)
31 return; 73 return;
32 74
33 // Special case: normally, extensions add their own lazy event listeners. 75 // Special case: normally, extensions add their own lazy event listeners.
34 // However, since the extension has just been installed, it hasn't had a 76 // 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 77 // 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 78 // extension does not actually have a listener, the event will just be
37 // ignored. 79 // ignored.
38 scoped_ptr<ListValue> event_args(new ListValue()); 80 scoped_ptr<ListValue> event_args(new ListValue());
(...skipping 25 matching lines...) Expand all
64 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) { 106 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) {
65 if (host) { 107 if (host) {
66 SendResponse(true); 108 SendResponse(true);
67 } else { 109 } else {
68 error_ = kPageLoadError; 110 error_ = kPageLoadError;
69 SendResponse(false); 111 SendResponse(false);
70 } 112 }
71 } 113 }
72 114
73 } // namespace extensions 115 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698