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/common/chrome_notification_types.h"
14 #include "chrome/common/extensions/extension.h"
15 #include "chrome/common/extensions/extension_set.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_types.h"
20 using extensions::AppEventRouter
;
21 using extensions::Extension
;
22 using extensions::ExtensionHost
;
23 using extensions::ExtensionPrefs
;
24 using extensions::ExtensionSystem
;
28 AppRestoreService::AppRestoreService(Profile
* profile
)
31 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
,
32 content::NotificationService::AllSources());
34 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
35 content::NotificationService::AllSources());
37 this, chrome::NOTIFICATION_APP_TERMINATING
,
38 content::NotificationService::AllSources());
41 void AppRestoreService::HandleStartup(bool should_restore_apps
) {
42 ExtensionService
* extension_service
=
43 ExtensionSystem::Get(profile_
)->extension_service();
44 const ExtensionSet
* extensions
= extension_service
->extensions();
45 ExtensionPrefs
* extension_prefs
= extension_service
->extension_prefs();
47 for (ExtensionSet::const_iterator it
= extensions
->begin();
48 it
!= extensions
->end(); ++it
) {
49 const Extension
* extension
= *it
;
50 if (extension_prefs
->IsExtensionRunning(extension
->id())) {
51 std::vector
<SavedFileEntry
> file_entries
;
52 extension_prefs
->GetSavedFileEntries(extension
->id(), &file_entries
);
53 RecordAppStop(extension
->id());
54 if (should_restore_apps
)
55 RestoreApp(*it
, file_entries
);
60 void AppRestoreService::Observe(int type
,
61 const content::NotificationSource
& source
,
62 const content::NotificationDetails
& details
) {
64 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
: {
65 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
66 const Extension
* extension
= host
->extension();
67 if (extension
&& extension
->is_platform_app())
68 RecordAppStart(extension
->id());
72 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
73 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
74 const Extension
* extension
= host
->extension();
75 if (extension
&& extension
->is_platform_app())
76 RecordAppStop(extension
->id());
80 case chrome::NOTIFICATION_APP_TERMINATING
: {
81 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
82 // as all extension hosts will be destroyed as a result of shutdown.
83 registrar_
.RemoveAll();
90 void AppRestoreService::RecordAppStart(const std::string
& extension_id
) {
91 ExtensionPrefs
* extension_prefs
=
92 ExtensionSystem::Get(profile_
)->extension_service()->extension_prefs();
93 extension_prefs
->SetExtensionRunning(extension_id
, true);
96 void AppRestoreService::RecordAppStop(const std::string
& extension_id
) {
97 ExtensionPrefs
* extension_prefs
=
98 ExtensionSystem::Get(profile_
)->extension_service()->extension_prefs();
99 extension_prefs
->SetExtensionRunning(extension_id
, false);
100 extension_prefs
->ClearSavedFileEntries(extension_id
);
103 void AppRestoreService::RestoreApp(
104 const Extension
* extension
,
105 const std::vector
<SavedFileEntry
>& file_entries
) {
106 // TODO(koz): Make |file_entries| available to the newly restarted app.
107 AppEventRouter::DispatchOnRestartedEvent(profile_
, extension
);