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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/runtime/runtime_api.cc
diff --git a/chrome/browser/extensions/api/runtime/runtime_api.cc b/chrome/browser/extensions/api/runtime/runtime_api.cc
index 682c1f466e6504c106c2e850fab215878971c199..ba969b84251879528b59451abee0b6c51ea48d94 100644
--- a/chrome/browser/extensions/api/runtime/runtime_api.cc
+++ b/chrome/browser/extensions/api/runtime/runtime_api.cc
@@ -28,6 +28,7 @@
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
+#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/lazy_background_task_queue.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/error_utils.h"
@@ -41,6 +42,8 @@
#include "chromeos/dbus/power_manager_client.h"
#endif
+using content::BrowserContext;
+
namespace GetPlatformInfo = extensions::api::runtime::GetPlatformInfo;
namespace extensions {
@@ -132,6 +135,93 @@ std::string GetUninstallUrl(ExtensionPrefs* prefs,
} // namespace
+///////////////////////////////////////////////////////////////////////////////
+
+RuntimeAPI::RuntimeAPI(content::BrowserContext* context)
+ : browser_context_(context),
+ dispatch_chrome_updated_event_(false) {
+ registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
+ content::Source<BrowserContext>(context));
+ registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
+ content::Source<BrowserContext>(context));
+ registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
+ content::Source<BrowserContext>(context));
+
+ // Check if registered events are up-to-date. We can only do this once
+ // per browser context, since it updates internal state when called.
+ dispatch_chrome_updated_event_ =
+ ExtensionsBrowserClient::Get()->DidVersionUpdate(browser_context_);
+}
+
+RuntimeAPI::~RuntimeAPI() {}
+
+void RuntimeAPI::Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) {
+ switch (type) {
+ case chrome::NOTIFICATION_EXTENSIONS_READY: {
+ OnExtensionsReady();
+ break;
+ }
+ case chrome::NOTIFICATION_EXTENSION_LOADED: {
+ const Extension* extension =
+ content::Details<const Extension>(details).ptr();
+ OnExtensionLoaded(extension);
+ break;
+ }
+ case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
+ const Extension* extension =
+ content::Details<const InstalledExtensionInfo>(details)->extension;
+ OnExtensionInstalled(extension);
+ break;
+ }
+ default:
+ NOTREACHED();
+ break;
+ }
+}
+
+void RuntimeAPI::OnExtensionsReady() {
+ // We're done restarting Chrome after an update.
+ dispatch_chrome_updated_event_ = false;
+}
+
+void RuntimeAPI::OnExtensionLoaded(const Extension* extension) {
+ if (!dispatch_chrome_updated_event_)
+ return;
+
+ // Dispatch the onInstalled event with reason "chrome_update".
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
+ browser_context_,
+ extension->id(),
+ Version(),
+ true));
+}
+
+void RuntimeAPI::OnExtensionInstalled(const Extension* extension) {
+ // Get the previous version to check if this is an upgrade.
+ ExtensionService* service = ExtensionSystem::GetForBrowserContext(
+ browser_context_)->extension_service();
+ const Extension* old = service->GetExtensionById(extension->id(), true);
+ Version old_version;
+ if (old)
+ old_version = *old->version();
+
+ // Dispatch the onInstalled event.
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
+ browser_context_,
+ extension->id(),
+ old_version,
+ false));
+
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
// static
void RuntimeEventRouter::DispatchOnStartupEvent(
content::BrowserContext* context, const std::string& extension_id) {
@@ -146,6 +236,8 @@ void RuntimeEventRouter::DispatchOnInstalledEvent(
const std::string& extension_id,
const Version& old_version,
bool chrome_updated) {
+ if (!ExtensionsBrowserClient::Get()->IsValidContext(context))
+ return;
ExtensionSystem* system = ExtensionSystem::GetForBrowserContext(context);
if (!system)
return;
« 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