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

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

Issue 69883007: Extract chrome.runtime API code from extensions::EventRouter (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix chromeos Created 7 years, 1 month 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 <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 10 matching lines...) Expand all
21 #include "chrome/browser/ui/browser_finder.h" 21 #include "chrome/browser/ui/browser_finder.h"
22 #include "chrome/browser/ui/browser_navigator.h" 22 #include "chrome/browser/ui/browser_navigator.h"
23 #include "chrome/browser/ui/browser_window.h" 23 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/common/extensions/api/runtime.h" 24 #include "chrome/common/extensions/api/runtime.h"
25 #include "chrome/common/extensions/extension.h" 25 #include "chrome/common/extensions/extension.h"
26 #include "chrome/common/omaha_query_params/omaha_query_params.h" 26 #include "chrome/common/omaha_query_params/omaha_query_params.h"
27 #include "content/public/browser/child_process_security_policy.h" 27 #include "content/public/browser/child_process_security_policy.h"
28 #include "content/public/browser/notification_service.h" 28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/render_process_host.h" 29 #include "content/public/browser/render_process_host.h"
30 #include "content/public/browser/render_view_host.h" 30 #include "content/public/browser/render_view_host.h"
31 #include "extensions/browser/extensions_browser_client.h"
31 #include "extensions/browser/lazy_background_task_queue.h" 32 #include "extensions/browser/lazy_background_task_queue.h"
32 #include "extensions/browser/process_manager.h" 33 #include "extensions/browser/process_manager.h"
33 #include "extensions/common/error_utils.h" 34 #include "extensions/common/error_utils.h"
34 #include "extensions/common/manifest_handlers/background_info.h" 35 #include "extensions/common/manifest_handlers/background_info.h"
35 #include "url/gurl.h" 36 #include "url/gurl.h"
36 #include "webkit/browser/fileapi/isolated_context.h" 37 #include "webkit/browser/fileapi/isolated_context.h"
37 38
38 #if defined(OS_CHROMEOS) 39 #if defined(OS_CHROMEOS)
39 #include "chrome/browser/chromeos/login/user_manager.h" 40 #include "chrome/browser/chromeos/login/user_manager.h"
40 #include "chromeos/dbus/dbus_thread_manager.h" 41 #include "chromeos/dbus/dbus_thread_manager.h"
41 #include "chromeos/dbus/power_manager_client.h" 42 #include "chromeos/dbus/power_manager_client.h"
42 #endif 43 #endif
43 44
45 using content::BrowserContext;
46
44 namespace GetPlatformInfo = extensions::api::runtime::GetPlatformInfo; 47 namespace GetPlatformInfo = extensions::api::runtime::GetPlatformInfo;
45 48
46 namespace extensions { 49 namespace extensions {
47 50
48 namespace runtime = api::runtime; 51 namespace runtime = api::runtime;
49 52
50 namespace { 53 namespace {
51 54
52 const char kNoBackgroundPageError[] = "You do not have a background page."; 55 const char kNoBackgroundPageError[] = "You do not have a background page.";
53 const char kPageLoadError[] = "Background page failed to load."; 56 const char kPageLoadError[] = "Background page failed to load.";
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 std::string GetUninstallUrl(ExtensionPrefs* prefs, 128 std::string GetUninstallUrl(ExtensionPrefs* prefs,
126 const std::string& extension_id) { 129 const std::string& extension_id) {
127 std::string url_string; 130 std::string url_string;
128 prefs->ReadPrefAsString(extension_id, kUninstallUrl, &url_string); 131 prefs->ReadPrefAsString(extension_id, kUninstallUrl, &url_string);
129 return url_string; 132 return url_string;
130 } 133 }
131 #endif // defined(ENABLE_EXTENSIONS) 134 #endif // defined(ENABLE_EXTENSIONS)
132 135
133 } // namespace 136 } // namespace
134 137
138 ///////////////////////////////////////////////////////////////////////////////
139
140 RuntimeAPI::RuntimeAPI(content::BrowserContext* context)
141 : browser_context_(context),
142 dispatch_chrome_updated_event_(false) {
143 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
144 content::Source<BrowserContext>(context));
145 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
146 content::Source<BrowserContext>(context));
147 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
148 content::Source<BrowserContext>(context));
149
150 // Check if registered events are up-to-date. We can only do this once
151 // per browser context, since it updates internal state when called.
152 dispatch_chrome_updated_event_ =
153 ExtensionsBrowserClient::Get()->DidVersionUpdate(browser_context_);
154 }
155
156 RuntimeAPI::~RuntimeAPI() {}
157
158 void RuntimeAPI::Observe(int type,
159 const content::NotificationSource& source,
160 const content::NotificationDetails& details) {
161 switch (type) {
162 case chrome::NOTIFICATION_EXTENSIONS_READY: {
163 OnExtensionsReady();
164 break;
165 }
166 case chrome::NOTIFICATION_EXTENSION_LOADED: {
167 const Extension* extension =
168 content::Details<const Extension>(details).ptr();
169 OnExtensionLoaded(extension);
170 break;
171 }
172 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
173 const Extension* extension =
174 content::Details<const InstalledExtensionInfo>(details)->extension;
175 OnExtensionInstalled(extension);
176 break;
177 }
178 default:
179 NOTREACHED();
180 break;
181 }
182 }
183
184 void RuntimeAPI::OnExtensionsReady() {
185 // We're done restarting Chrome after an update.
186 dispatch_chrome_updated_event_ = false;
187 }
188
189 void RuntimeAPI::OnExtensionLoaded(const Extension* extension) {
190 if (!dispatch_chrome_updated_event_)
191 return;
192
193 // Dispatch the onInstalled event with reason "chrome_update".
194 base::MessageLoop::current()->PostTask(
195 FROM_HERE,
196 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
197 browser_context_,
198 extension->id(),
199 Version(),
200 true));
201 }
202
203 void RuntimeAPI::OnExtensionInstalled(const Extension* extension) {
204 // Get the previous version to check if this is an upgrade.
205 ExtensionService* service = ExtensionSystem::GetForBrowserContext(
206 browser_context_)->extension_service();
207 const Extension* old = service->GetExtensionById(extension->id(), true);
208 Version old_version;
209 if (old)
210 old_version = *old->version();
211
212 // Dispatch the onInstalled event.
213 base::MessageLoop::current()->PostTask(
214 FROM_HERE,
215 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
216 browser_context_,
217 extension->id(),
218 old_version,
219 false));
220
221 }
222
223 ///////////////////////////////////////////////////////////////////////////////
224
135 // static 225 // static
136 void RuntimeEventRouter::DispatchOnStartupEvent( 226 void RuntimeEventRouter::DispatchOnStartupEvent(
137 content::BrowserContext* context, const std::string& extension_id) { 227 content::BrowserContext* context, const std::string& extension_id) {
138 // TODO(jamescook): Convert to BrowserContext all the way down. 228 // TODO(jamescook): Convert to BrowserContext all the way down.
139 Profile* profile = static_cast<Profile*>(context); 229 Profile* profile = static_cast<Profile*>(context);
140 DispatchOnStartupEventImpl(profile, extension_id, true, NULL); 230 DispatchOnStartupEventImpl(profile, extension_id, true, NULL);
141 } 231 }
142 232
143 // static 233 // static
144 void RuntimeEventRouter::DispatchOnInstalledEvent( 234 void RuntimeEventRouter::DispatchOnInstalledEvent(
145 content::BrowserContext* context, 235 content::BrowserContext* context,
146 const std::string& extension_id, 236 const std::string& extension_id,
147 const Version& old_version, 237 const Version& old_version,
148 bool chrome_updated) { 238 bool chrome_updated) {
239 if (!ExtensionsBrowserClient::Get()->IsValidContext(context))
240 return;
149 ExtensionSystem* system = ExtensionSystem::GetForBrowserContext(context); 241 ExtensionSystem* system = ExtensionSystem::GetForBrowserContext(context);
150 if (!system) 242 if (!system)
151 return; 243 return;
152 244
153 scoped_ptr<base::ListValue> event_args(new base::ListValue()); 245 scoped_ptr<base::ListValue> event_args(new base::ListValue());
154 base::DictionaryValue* info = new base::DictionaryValue(); 246 base::DictionaryValue* info = new base::DictionaryValue();
155 event_args->Append(info); 247 event_args->Append(info);
156 if (old_version.IsValid()) { 248 if (old_version.IsValid()) {
157 info->SetString(kInstallReason, kInstallReasonUpdate); 249 info->SetString(kInstallReason, kInstallReasonUpdate);
158 info->SetString(kInstallPreviousVersion, old_version.GetString()); 250 info->SetString(kInstallPreviousVersion, old_version.GetString());
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 content::ChildProcessSecurityPolicy::GetInstance(); 538 content::ChildProcessSecurityPolicy::GetInstance();
447 policy->GrantReadFileSystem(renderer_id, filesystem_id); 539 policy->GrantReadFileSystem(renderer_id, filesystem_id);
448 base::DictionaryValue* dict = new base::DictionaryValue(); 540 base::DictionaryValue* dict = new base::DictionaryValue();
449 SetResult(dict); 541 SetResult(dict);
450 dict->SetString("fileSystemId", filesystem_id); 542 dict->SetString("fileSystemId", filesystem_id);
451 dict->SetString("baseName", relative_path); 543 dict->SetString("baseName", relative_path);
452 return true; 544 return true;
453 } 545 }
454 546
455 } // namespace extensions 547 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/runtime/runtime_api.h ('k') | chrome/browser/extensions/api/runtime/runtime_api_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698