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.
5 #include "apps/app_restore_service.h"
7 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
8 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
9 #include "chrome/browser/extensions/event_router.h"
10 #include "chrome/browser/extensions/extension_host.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_system.h"
13 #include "chrome/browser/extensions/platform_app_launcher.h"
14 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/extensions/extension.h"
16 #include "chrome/common/extensions/extension_set.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"
22 #include "win8/util/win8_util.h"
25 using extensions::AppEventRouter
;
26 using extensions::Extension
;
27 using extensions::ExtensionHost
;
28 using extensions::ExtensionPrefs
;
29 using extensions::ExtensionSystem
;
34 bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart
) {
35 bool should_restore_apps
= is_browser_restart
;
36 #if defined(OS_CHROMEOS)
37 // Chromeos always restarts apps, even if it was a regular shutdown.
38 should_restore_apps
= true;
40 // Packaged apps are not supported in Metro mode, so don't try to start them.
41 if (win8::IsSingleWindowMetroMode())
42 should_restore_apps
= false;
44 return should_restore_apps
;
47 AppRestoreService::AppRestoreService(Profile
* profile
)
50 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
,
51 content::NotificationService::AllSources());
53 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
54 content::NotificationService::AllSources());
56 this, chrome::NOTIFICATION_APP_TERMINATING
,
57 content::NotificationService::AllSources());
60 void AppRestoreService::HandleStartup(bool should_restore_apps
) {
61 ExtensionService
* extension_service
=
62 ExtensionSystem::Get(profile_
)->extension_service();
63 const ExtensionSet
* extensions
= extension_service
->extensions();
64 ExtensionPrefs
* extension_prefs
= extension_service
->extension_prefs();
66 for (ExtensionSet::const_iterator it
= extensions
->begin();
67 it
!= extensions
->end(); ++it
) {
68 const Extension
* extension
= *it
;
69 if (extension_prefs
->IsExtensionRunning(extension
->id())) {
70 std::vector
<SavedFileEntry
> file_entries
;
71 extension_prefs
->GetSavedFileEntries(extension
->id(), &file_entries
);
72 RecordAppStop(extension
->id());
73 if (should_restore_apps
)
74 RestoreApp(*it
, file_entries
);
79 void AppRestoreService::Observe(int type
,
80 const content::NotificationSource
& source
,
81 const content::NotificationDetails
& details
) {
83 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
: {
84 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
85 const Extension
* extension
= host
->extension();
86 if (extension
&& extension
->is_platform_app())
87 RecordAppStart(extension
->id());
91 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
92 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
93 const Extension
* extension
= host
->extension();
94 if (extension
&& extension
->is_platform_app())
95 RecordAppStop(extension
->id());
99 case chrome::NOTIFICATION_APP_TERMINATING
: {
100 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
101 // as all extension hosts will be destroyed as a result of shutdown.
102 registrar_
.RemoveAll();
109 void AppRestoreService::RecordAppStart(const std::string
& extension_id
) {
110 ExtensionPrefs
* extension_prefs
=
111 ExtensionSystem::Get(profile_
)->extension_service()->extension_prefs();
112 extension_prefs
->SetExtensionRunning(extension_id
, true);
115 void AppRestoreService::RecordAppStop(const std::string
& extension_id
) {
116 ExtensionPrefs
* extension_prefs
=
117 ExtensionSystem::Get(profile_
)->extension_service()->extension_prefs();
118 extension_prefs
->SetExtensionRunning(extension_id
, false);
119 extension_prefs
->ClearSavedFileEntries(extension_id
);
122 void AppRestoreService::RestoreApp(
123 const Extension
* extension
,
124 const std::vector
<SavedFileEntry
>& file_entries
) {
125 extensions::RestartPlatformAppWithFileEntries(profile_
,