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

Unified Diff: chrome/browser/sessions/app_restore_service.cc

Issue 10875027: Restart running apps when chrome restarts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: typo 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/sessions/app_restore_service.cc
diff --git a/chrome/browser/sessions/app_restore_service.cc b/chrome/browser/sessions/app_restore_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..50b6d1336eda244d22be6ba65bfc2aede8b7967d
--- /dev/null
+++ b/chrome/browser/sessions/app_restore_service.cc
@@ -0,0 +1,110 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/sessions/app_restore_service.h"
+
+#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
+#include "chrome/browser/extensions/event_router.h"
+#include "chrome/browser/extensions/extension_host.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/extensions/extension_system.h"
+#include "chrome/browser/prefs/pref_service.h"
+#include "chrome/browser/prefs/scoped_user_pref_update.h"
+#include "chrome/common/extensions/extension.h"
+#include "chrome/common/chrome_notification_types.h"
+#include "chrome/common/pref_names.h"
+#include "content/public/browser/notification_details.h"
+#include "content/public/browser/notification_service.h"
+#include "content/public/browser/notification_types.h"
+
+AppRestoreService::AppRestoreService(Profile* profile)
+ : profile_(profile) {
+ registrar_.Add(
+ this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
+ content::NotificationService::AllSources());
+ registrar_.Add(
+ this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
+ content::NotificationService::AllSources());
+ registrar_.Add(
+ this, content::NOTIFICATION_APP_TERMINATING,
+ content::NotificationService::AllSources());
+}
+
+void AppRestoreService::RestoreApps() {
+ const base::Value* running_apps =
+ profile_->GetPrefs()->GetUserPrefValue(prefs::kAppsRunningAtShutdown);
+ if (!running_apps)
+ return;
+ const base::ListValue* running_apps_list = NULL;
+ running_apps->GetAsList(&running_apps_list);
+ for (size_t i = 0; i < running_apps_list->GetSize(); i++) {
+ std::string extension_id;
+ running_apps_list->GetString(i, &extension_id);
+
+ // TODO(koz): As event dispatch occurs asynchronously the order of windows
+ // created in response to the onRestarted event is not stable, which means
+ // that the alt-tab order of windows won't remain constant over restarts.
+ // This can be fixed by acking each event before dispatching the next one.
+ RestoreApp(extension_id);
+ }
+ profile_->GetPrefs()->ClearPref(prefs::kAppsRunningAtShutdown);
+}
+
+void AppRestoreService::Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) {
+ switch (type) {
+ case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
+ extensions::ExtensionHost* host =
+ content::Details<extensions::ExtensionHost>(details).ptr();
+ if (host->extension()->is_platform_app()) {
+ RecordAppStart(host->extension()->id());
+ }
+ break;
+ }
+
+ case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
+ extensions::ExtensionHost* host =
+ content::Details<extensions::ExtensionHost>(details).ptr();
+ if (host->extension()->is_platform_app()) {
+ RecordAppStop(host->extension()->id());
+ }
+ break;
+ }
+
+ case content::NOTIFICATION_APP_TERMINATING: {
+ // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
+ // as all extension hosts will be destroyed as a result of shutdown.
+ registrar_.RemoveAll();
+ break;
+ }
+ }
+}
+
+// static
+void AppRestoreService::RegisterUserPrefs(PrefService* pref_service) {
+ pref_service->RegisterListPref(prefs::kAppsRunningAtShutdown,
+ PrefService::UNSYNCABLE_PREF);
+}
+
+void AppRestoreService::RecordAppStart(const std::string& extension_id) {
+ ListPrefUpdate pref_update(profile_->GetPrefs(),
+ prefs::kAppsRunningAtShutdown);
+ base::ListValue* apps = pref_update.Get();
+ apps->AppendIfNotPresent(base::Value::CreateStringValue(extension_id));
+}
+
+void AppRestoreService::RecordAppStop(const std::string& extension_id) {
+ ListPrefUpdate pref_update(profile_->GetPrefs(),
+ prefs::kAppsRunningAtShutdown);
+ base::StringValue to_remove(extension_id);
+ pref_update.Get()->Remove(to_remove, NULL);
+}
+
+void AppRestoreService::RestoreApp(const std::string& extension_id) {
+ const extensions::Extension* extension =
+ extensions::ExtensionSystem::Get(profile_)->extension_service()->
+ extensions()->GetByID(extension_id);
+ extensions::AppEventRouter::DispatchOnRestartEvent(profile_, extension);
+}

Powered by Google App Engine
This is Rietveld 408576698