Home | History | Annotate | Download | only in apps
      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 "apps/app_restore_service.h"
      6 
      7 #include "apps/app_lifetime_monitor_factory.h"
      8 #include "apps/app_restore_service_factory.h"
      9 #include "apps/app_window.h"
     10 #include "apps/browser/api/app_runtime/app_runtime_api.h"
     11 #include "apps/launcher.h"
     12 #include "apps/saved_files_service.h"
     13 #include "chrome/browser/chrome_notification_types.h"
     14 #include "chrome/browser/profiles/profile.h"
     15 #include "extensions/browser/extension_host.h"
     16 #include "extensions/browser/extension_prefs.h"
     17 #include "extensions/browser/extension_registry.h"
     18 #include "extensions/common/extension.h"
     19 #include "extensions/common/extension_set.h"
     20 
     21 using extensions::Extension;
     22 using extensions::ExtensionHost;
     23 using extensions::ExtensionPrefs;
     24 using extensions::ExtensionRegistry;
     25 
     26 namespace apps {
     27 
     28 // static
     29 bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) {
     30   bool should_restore_apps = is_browser_restart;
     31 #if defined(OS_CHROMEOS)
     32   // Chromeos always restarts apps, even if it was a regular shutdown.
     33   should_restore_apps = true;
     34 #endif
     35   return should_restore_apps;
     36 }
     37 
     38 AppRestoreService::AppRestoreService(Profile* profile)
     39     : profile_(profile) {
     40   StartObservingAppLifetime();
     41 }
     42 
     43 void AppRestoreService::HandleStartup(bool should_restore_apps) {
     44   const extensions::ExtensionSet& extensions =
     45       ExtensionRegistry::Get(profile_)->enabled_extensions();
     46   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
     47 
     48   for (extensions::ExtensionSet::const_iterator it = extensions.begin();
     49       it != extensions.end(); ++it) {
     50     const Extension* extension = it->get();
     51     if (extension_prefs->IsExtensionRunning(extension->id())) {
     52       RecordAppStop(extension->id());
     53       // If we are not restoring apps (e.g., because it is a clean restart), and
     54       // the app does not have retain permission, explicitly clear the retained
     55       // entries queue.
     56       if (should_restore_apps) {
     57         RestoreApp(it->get());
     58       } else {
     59         SavedFilesService::Get(profile_)->ClearQueueIfNoRetainPermission(
     60             extension);
     61       }
     62     }
     63   }
     64 }
     65 
     66 bool AppRestoreService::IsAppRestorable(const std::string& extension_id) {
     67   return ExtensionPrefs::Get(profile_)->IsExtensionRunning(extension_id);
     68 }
     69 
     70 // static
     71 AppRestoreService* AppRestoreService::Get(Profile* profile) {
     72   return apps::AppRestoreServiceFactory::GetForProfile(profile);
     73 }
     74 
     75 void AppRestoreService::OnAppStart(Profile* profile,
     76                                    const std::string& app_id) {
     77   RecordAppStart(app_id);
     78 }
     79 
     80 void AppRestoreService::OnAppActivated(Profile* profile,
     81                                        const std::string& app_id) {
     82   RecordAppActiveState(app_id, true);
     83 }
     84 
     85 void AppRestoreService::OnAppDeactivated(Profile* profile,
     86                                          const std::string& app_id) {
     87   RecordAppActiveState(app_id, false);
     88 }
     89 
     90 void AppRestoreService::OnAppStop(Profile* profile, const std::string& app_id) {
     91   RecordAppStop(app_id);
     92 }
     93 
     94 void AppRestoreService::OnChromeTerminating() {
     95   // We want to preserve the state when the app begins terminating, so stop
     96   // listening to app lifetime events.
     97   StopObservingAppLifetime();
     98 }
     99 
    100 void AppRestoreService::Shutdown() {
    101   StopObservingAppLifetime();
    102 }
    103 
    104 void AppRestoreService::RecordAppStart(const std::string& extension_id) {
    105   ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, true);
    106 }
    107 
    108 void AppRestoreService::RecordAppStop(const std::string& extension_id) {
    109   ExtensionPrefs::Get(profile_)->SetExtensionRunning(extension_id, false);
    110 }
    111 
    112 void AppRestoreService::RecordAppActiveState(const std::string& id,
    113                                              bool is_active) {
    114   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
    115 
    116   // If the extension isn't running then we will already have recorded whether
    117   // it is active or not.
    118   if (!extension_prefs->IsExtensionRunning(id))
    119     return;
    120 
    121   extension_prefs->SetIsActive(id, is_active);
    122 }
    123 
    124 void AppRestoreService::RestoreApp(const Extension* extension) {
    125   RestartPlatformApp(profile_, extension);
    126 }
    127 
    128 void AppRestoreService::StartObservingAppLifetime() {
    129   AppLifetimeMonitor* app_lifetime_monitor =
    130       AppLifetimeMonitorFactory::GetForProfile(profile_);
    131   DCHECK(app_lifetime_monitor);
    132   app_lifetime_monitor->AddObserver(this);
    133 }
    134 
    135 void AppRestoreService::StopObservingAppLifetime() {
    136   AppLifetimeMonitor* app_lifetime_monitor =
    137       AppLifetimeMonitorFactory::GetForProfile(profile_);
    138   // This might be NULL in tests.
    139   if (app_lifetime_monitor)
    140     app_lifetime_monitor->RemoveObserver(this);
    141 }
    142 
    143 }  // namespace apps
    144