OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sessions/app_restore_service.h" |
| 6 |
| 7 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" |
| 8 #include "chrome/browser/extensions/event_router.h" |
| 9 #include "chrome/browser/extensions/extension_host.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/extensions/extension_system.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "content/public/browser/notification_details.h" |
| 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_types.h" |
| 20 |
| 21 AppRestoreService::AppRestoreService(Profile* profile) |
| 22 : profile_(profile) { |
| 23 registrar_.Add( |
| 24 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
| 25 content::NotificationService::AllSources()); |
| 26 registrar_.Add( |
| 27 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 28 content::NotificationService::AllSources()); |
| 29 registrar_.Add( |
| 30 this, content::NOTIFICATION_APP_TERMINATING, |
| 31 content::NotificationService::AllSources()); |
| 32 } |
| 33 |
| 34 void AppRestoreService::RestoreApps() { |
| 35 const base::Value* running_apps = |
| 36 profile_->GetPrefs()->GetUserPrefValue(prefs::kAppsRunningAtShutdown); |
| 37 if (!running_apps) |
| 38 return; |
| 39 const base::ListValue* running_apps_list = NULL; |
| 40 running_apps->GetAsList(&running_apps_list); |
| 41 for (size_t i = 0; i < running_apps_list->GetSize(); i++) { |
| 42 std::string extension_id; |
| 43 running_apps_list->GetString(i, &extension_id); |
| 44 |
| 45 // TODO(koz): As event dispatch occurs asynchronously the order of windows |
| 46 // created in response to the onRestarted event is not stable, which means |
| 47 // that the alt-tab order of windows won't remain constant over restarts. |
| 48 // This can be fixed by acking each event before dispatching the next one. |
| 49 RestoreApp(extension_id); |
| 50 } |
| 51 profile_->GetPrefs()->ClearPref(prefs::kAppsRunningAtShutdown); |
| 52 } |
| 53 |
| 54 void AppRestoreService::Observe(int type, |
| 55 const content::NotificationSource& source, |
| 56 const content::NotificationDetails& details) { |
| 57 switch (type) { |
| 58 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { |
| 59 extensions::ExtensionHost* host = |
| 60 content::Details<extensions::ExtensionHost>(details).ptr(); |
| 61 if (host->extension()->is_platform_app()) { |
| 62 RecordAppStart(host->extension()->id()); |
| 63 } |
| 64 break; |
| 65 } |
| 66 |
| 67 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
| 68 extensions::ExtensionHost* host = |
| 69 content::Details<extensions::ExtensionHost>(details).ptr(); |
| 70 if (host->extension()->is_platform_app()) { |
| 71 RecordAppStop(host->extension()->id()); |
| 72 } |
| 73 break; |
| 74 } |
| 75 |
| 76 case content::NOTIFICATION_APP_TERMINATING: { |
| 77 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular |
| 78 // as all extension hosts will be destroyed as a result of shutdown. |
| 79 registrar_.RemoveAll(); |
| 80 break; |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 85 // static |
| 86 void AppRestoreService::RegisterUserPrefs(PrefService* pref_service) { |
| 87 pref_service->RegisterListPref(prefs::kAppsRunningAtShutdown, |
| 88 PrefService::UNSYNCABLE_PREF); |
| 89 } |
| 90 |
| 91 void AppRestoreService::RecordAppStart(const std::string& extension_id) { |
| 92 ListPrefUpdate pref_update(profile_->GetPrefs(), |
| 93 prefs::kAppsRunningAtShutdown); |
| 94 base::ListValue* apps = pref_update.Get(); |
| 95 apps->AppendIfNotPresent(base::Value::CreateStringValue(extension_id)); |
| 96 } |
| 97 |
| 98 void AppRestoreService::RecordAppStop(const std::string& extension_id) { |
| 99 ListPrefUpdate pref_update(profile_->GetPrefs(), |
| 100 prefs::kAppsRunningAtShutdown); |
| 101 base::StringValue to_remove(extension_id); |
| 102 pref_update.Get()->Remove(to_remove, NULL); |
| 103 } |
| 104 |
| 105 void AppRestoreService::RestoreApp(const std::string& extension_id) { |
| 106 const extensions::Extension* extension = |
| 107 extensions::ExtensionSystem::Get(profile_)->extension_service()-> |
| 108 extensions()->GetByID(extension_id); |
| 109 extensions::AppEventRouter::DispatchOnRestartEvent(profile_, extension); |
| 110 } |
OLD | NEW |