[rAc Android dialog] Logo, banner, text, alignment fixes.
[chromium-blink-merge.git] / apps / app_restore_service.cc
blob66f2a73bf90754aff41a71fa2da85c1891c1cb05
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"
21 #if defined(OS_WIN)
22 #include "win8/util/win8_util.h"
23 #endif
25 using extensions::AppEventRouter;
26 using extensions::Extension;
27 using extensions::ExtensionHost;
28 using extensions::ExtensionPrefs;
29 using extensions::ExtensionSystem;
31 namespace apps {
33 // static
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;
39 #elif defined(OS_WIN)
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;
43 #endif
44 return should_restore_apps;
47 AppRestoreService::AppRestoreService(Profile* profile)
48 : profile_(profile) {
49 registrar_.Add(
50 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
51 content::NotificationService::AllSources());
52 registrar_.Add(
53 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
54 content::NotificationService::AllSources());
55 registrar_.Add(
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) {
82 switch (type) {
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());
88 break;
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());
96 break;
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();
103 break;
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_,
126 extension,
127 file_entries);
130 } // namespace apps