| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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/apps/ephemeral_app_service.h" | |
| 6 | |
| 7 #include "apps/app_lifetime_monitor_factory.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/thread_task_runner_handle.h" | |
| 11 #include "chrome/browser/apps/ephemeral_app_service_factory.h" | |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/extensions/extension_util.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/common/content_switches.h" | |
| 17 #include "extensions/browser/extension_prefs.h" | |
| 18 #include "extensions/browser/extension_registry.h" | |
| 19 #include "extensions/browser/extension_system.h" | |
| 20 #include "extensions/browser/extension_util.h" | |
| 21 #include "extensions/browser/uninstall_reason.h" | |
| 22 #include "extensions/common/extension.h" | |
| 23 #include "extensions/common/extension_set.h" | |
| 24 #include "extensions/common/one_shot_event.h" | |
| 25 | |
| 26 using extensions::Extension; | |
| 27 using extensions::ExtensionPrefs; | |
| 28 using extensions::ExtensionRegistry; | |
| 29 using extensions::ExtensionSet; | |
| 30 using extensions::ExtensionSystem; | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // The number of seconds after an app has stopped running before it will be | |
| 35 // disabled. | |
| 36 const int kDefaultDisableAppDelay = 1; | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 // static | |
| 41 EphemeralAppService* EphemeralAppService::Get(Profile* profile) { | |
| 42 return EphemeralAppServiceFactory::GetForProfile(profile); | |
| 43 } | |
| 44 | |
| 45 EphemeralAppService::EphemeralAppService(Profile* profile) | |
| 46 : profile_(profile), | |
| 47 extension_registry_observer_(this), | |
| 48 app_lifetime_monitor_observer_(this), | |
| 49 disable_idle_app_delay_(kDefaultDisableAppDelay), | |
| 50 weak_ptr_factory_(this) { | |
| 51 ExtensionSystem::Get(profile_)->ready().Post( | |
| 52 FROM_HERE, | |
| 53 base::Bind(&EphemeralAppService::Init, weak_ptr_factory_.GetWeakPtr())); | |
| 54 } | |
| 55 | |
| 56 EphemeralAppService::~EphemeralAppService() { | |
| 57 } | |
| 58 | |
| 59 void EphemeralAppService::ClearCachedApps() { | |
| 60 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); | |
| 61 DCHECK(registry); | |
| 62 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 63 DCHECK(prefs); | |
| 64 ExtensionService* service = | |
| 65 ExtensionSystem::Get(profile_)->extension_service(); | |
| 66 DCHECK(service); | |
| 67 | |
| 68 scoped_ptr<ExtensionSet> extensions = | |
| 69 registry->GenerateInstalledExtensionsSet(); | |
| 70 | |
| 71 for (ExtensionSet::const_iterator it = extensions->begin(); | |
| 72 it != extensions->end(); | |
| 73 ++it) { | |
| 74 std::string extension_id = (*it)->id(); | |
| 75 if (!prefs->IsEphemeralApp(extension_id)) | |
| 76 continue; | |
| 77 | |
| 78 DCHECK(registry->GetExtensionById(extension_id, | |
| 79 ExtensionRegistry::EVERYTHING)); | |
| 80 service->UninstallExtension( | |
| 81 extension_id, | |
| 82 extensions::UNINSTALL_REASON_ORPHANED_EPHEMERAL_EXTENSION, | |
| 83 base::Bind(&base::DoNothing), | |
| 84 NULL); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void EphemeralAppService::OnExtensionWillBeInstalled( | |
| 89 content::BrowserContext* browser_context, | |
| 90 const extensions::Extension* extension, | |
| 91 bool is_update, | |
| 92 bool from_ephemeral, | |
| 93 const std::string& old_name) { | |
| 94 if (from_ephemeral) | |
| 95 HandleEphemeralAppPromoted(extension); | |
| 96 } | |
| 97 | |
| 98 void EphemeralAppService::OnAppStop(Profile* profile, | |
| 99 const std::string& app_id) { | |
| 100 if (!extensions::util::IsEphemeralApp(app_id, profile_)) | |
| 101 return; | |
| 102 | |
| 103 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 104 FROM_HERE, base::Bind(&EphemeralAppService::DisableEphemeralApp, | |
| 105 weak_ptr_factory_.GetWeakPtr(), app_id), | |
| 106 base::TimeDelta::FromSeconds(disable_idle_app_delay_)); | |
| 107 } | |
| 108 | |
| 109 void EphemeralAppService::OnChromeTerminating() { | |
| 110 extension_registry_observer_.RemoveAll(); | |
| 111 app_lifetime_monitor_observer_.RemoveAll(); | |
| 112 } | |
| 113 | |
| 114 void EphemeralAppService::Init() { | |
| 115 // Start observing. | |
| 116 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); | |
| 117 app_lifetime_monitor_observer_.Add( | |
| 118 apps::AppLifetimeMonitorFactory::GetForProfile(profile_)); | |
| 119 | |
| 120 // Execute startup clean up tasks (except during tests). | |
| 121 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) | |
| 122 return; | |
| 123 | |
| 124 content::BrowserThread::PostAfterStartupTask( | |
| 125 FROM_HERE, content::BrowserThread::GetMessageLoopProxyForThread( | |
| 126 content::BrowserThread::UI), | |
| 127 base::Bind(&EphemeralAppService::ClearCachedApps, | |
| 128 weak_ptr_factory_.GetWeakPtr())); | |
| 129 } | |
| 130 | |
| 131 void EphemeralAppService::DisableEphemeralApp(const std::string& app_id) { | |
| 132 if (!extensions::util::IsEphemeralApp(app_id, profile_) || | |
| 133 !extensions::util::IsExtensionIdle(app_id, profile_)) { | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 // After an ephemeral app has stopped running, unload it from extension | |
| 138 // system and disable it to prevent all background activity. | |
| 139 ExtensionService* service = | |
| 140 ExtensionSystem::Get(profile_)->extension_service(); | |
| 141 DCHECK(service); | |
| 142 service->DisableExtension(app_id, Extension::DISABLE_INACTIVE_EPHEMERAL_APP); | |
| 143 } | |
| 144 | |
| 145 void EphemeralAppService::HandleEphemeralAppPromoted(const Extension* app) { | |
| 146 // When ephemeral apps are promoted to regular install apps, remove the | |
| 147 // DISABLE_INACTIVE_EPHEMERAL_APP reason and enable the app if there are no | |
| 148 // other reasons. | |
| 149 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 150 DCHECK(prefs); | |
| 151 | |
| 152 int disable_reasons = prefs->GetDisableReasons(app->id()); | |
| 153 if (disable_reasons & Extension::DISABLE_INACTIVE_EPHEMERAL_APP) { | |
| 154 if (disable_reasons == Extension::DISABLE_INACTIVE_EPHEMERAL_APP) { | |
| 155 // This will also clear disable reasons. | |
| 156 prefs->SetExtensionEnabled(app->id()); | |
| 157 } else { | |
| 158 prefs->RemoveDisableReason(app->id(), | |
| 159 Extension::DISABLE_INACTIVE_EPHEMERAL_APP); | |
| 160 } | |
| 161 } | |
| 162 } | |
| OLD | NEW |