| 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 "apps/app_load_service.h" | |
| 6 | |
| 7 #include "apps/app_load_service_factory.h" | |
| 8 #include "apps/app_restore_service.h" | |
| 9 #include "apps/launcher.h" | |
| 10 #include "chrome/browser/extensions/extension_service.h" | |
| 11 #include "chrome/browser/extensions/unpacked_installer.h" | |
| 12 #include "content/public/browser/browser_context.h" | |
| 13 #include "content/public/browser/notification_details.h" | |
| 14 #include "content/public/browser/notification_service.h" | |
| 15 #include "content/public/browser/notification_types.h" | |
| 16 #include "extensions/browser/app_window/app_window_registry.h" | |
| 17 #include "extensions/browser/extension_host.h" | |
| 18 #include "extensions/browser/extension_prefs.h" | |
| 19 #include "extensions/browser/extension_registry.h" | |
| 20 #include "extensions/browser/extension_system.h" | |
| 21 #include "extensions/browser/notification_types.h" | |
| 22 #include "extensions/common/extension.h" | |
| 23 | |
| 24 using extensions::Extension; | |
| 25 using extensions::ExtensionPrefs; | |
| 26 using extensions::ExtensionSystem; | |
| 27 | |
| 28 namespace apps { | |
| 29 | |
| 30 AppLoadService::PostReloadAction::PostReloadAction() | |
| 31 : action_type(LAUNCH_FOR_RELOAD), | |
| 32 command_line(base::CommandLine::NO_PROGRAM) { | |
| 33 } | |
| 34 | |
| 35 AppLoadService::AppLoadService(content::BrowserContext* context) | |
| 36 : context_(context) { | |
| 37 registrar_.Add(this, | |
| 38 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD, | |
| 39 content::NotificationService::AllSources()); | |
| 40 extensions::ExtensionRegistry::Get(context_)->AddObserver(this); | |
| 41 } | |
| 42 | |
| 43 AppLoadService::~AppLoadService() = default; | |
| 44 | |
| 45 void AppLoadService::Shutdown() { | |
| 46 extensions::ExtensionRegistry::Get(context_)->RemoveObserver(this); | |
| 47 } | |
| 48 | |
| 49 void AppLoadService::RestartApplication(const std::string& extension_id) { | |
| 50 post_reload_actions_[extension_id].action_type = RESTART; | |
| 51 ExtensionService* service = | |
| 52 extensions::ExtensionSystem::Get(context_)->extension_service(); | |
| 53 DCHECK(service); | |
| 54 service->ReloadExtension(extension_id); | |
| 55 } | |
| 56 | |
| 57 void AppLoadService::RestartApplicationIfRunning( | |
| 58 const std::string& extension_id) { | |
| 59 if (apps::AppRestoreService::Get(context_)->IsAppRestorable(extension_id)) | |
| 60 RestartApplication(extension_id); | |
| 61 } | |
| 62 | |
| 63 bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path, | |
| 64 const base::CommandLine& command_line, | |
| 65 const base::FilePath& current_dir) { | |
| 66 ExtensionService* extension_service = | |
| 67 ExtensionSystem::Get(context_)->extension_service(); | |
| 68 std::string extension_id; | |
| 69 if (!extensions::UnpackedInstaller::Create(extension_service) | |
| 70 ->LoadFromCommandLine(base::FilePath(extension_path), &extension_id, | |
| 71 true /* only_allow_apps */)) { | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 // Schedule the app to be launched once loaded. | |
| 76 PostReloadAction& action = post_reload_actions_[extension_id]; | |
| 77 action.action_type = LAUNCH_FOR_LOAD_AND_LAUNCH; | |
| 78 action.command_line = command_line; | |
| 79 action.current_dir = current_dir; | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 bool AppLoadService::Load(const base::FilePath& extension_path) { | |
| 84 ExtensionService* extension_service = | |
| 85 ExtensionSystem::Get(context_)->extension_service(); | |
| 86 std::string extension_id; | |
| 87 return extensions::UnpackedInstaller::Create(extension_service) | |
| 88 ->LoadFromCommandLine(base::FilePath(extension_path), &extension_id, | |
| 89 true /* only_allow_apps */); | |
| 90 } | |
| 91 | |
| 92 // static | |
| 93 AppLoadService* AppLoadService::Get(content::BrowserContext* context) { | |
| 94 return apps::AppLoadServiceFactory::GetForBrowserContext(context); | |
| 95 } | |
| 96 | |
| 97 void AppLoadService::Observe(int type, | |
| 98 const content::NotificationSource& source, | |
| 99 const content::NotificationDetails& details) { | |
| 100 DCHECK_EQ(type, extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD); | |
| 101 extensions::ExtensionHost* host = | |
| 102 content::Details<extensions::ExtensionHost>(details).ptr(); | |
| 103 const Extension* extension = host->extension(); | |
| 104 // It is possible for an extension to be unloaded before it stops loading. | |
| 105 if (!extension) | |
| 106 return; | |
| 107 std::map<std::string, PostReloadAction>::iterator it = | |
| 108 post_reload_actions_.find(extension->id()); | |
| 109 if (it == post_reload_actions_.end()) | |
| 110 return; | |
| 111 | |
| 112 switch (it->second.action_type) { | |
| 113 case LAUNCH_FOR_RELOAD: | |
| 114 LaunchPlatformApp(context_, extension, extensions::SOURCE_RELOAD); | |
| 115 break; | |
| 116 case RESTART: | |
| 117 RestartPlatformApp(context_, extension); | |
| 118 break; | |
| 119 case LAUNCH_FOR_LOAD_AND_LAUNCH: | |
| 120 LaunchPlatformAppWithCommandLine( | |
| 121 context_, extension, it->second.command_line, it->second.current_dir, | |
| 122 extensions::SOURCE_LOAD_AND_LAUNCH); | |
| 123 break; | |
| 124 default: | |
| 125 NOTREACHED(); | |
| 126 } | |
| 127 | |
| 128 post_reload_actions_.erase(it); | |
| 129 } | |
| 130 | |
| 131 void AppLoadService::OnExtensionUnloaded( | |
| 132 content::BrowserContext* browser_context, | |
| 133 const Extension* extension, | |
| 134 extensions::UnloadedExtensionInfo::Reason reason) { | |
| 135 if (!extension->is_platform_app()) | |
| 136 return; | |
| 137 | |
| 138 extensions::ExtensionPrefs* extension_prefs = | |
| 139 extensions::ExtensionPrefs::Get(browser_context); | |
| 140 if (WasUnloadedForReload(extension->id(), reason) && | |
| 141 extension_prefs->IsActive(extension->id()) && | |
| 142 !HasPostReloadAction(extension->id())) { | |
| 143 post_reload_actions_[extension->id()].action_type = LAUNCH_FOR_RELOAD; | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 bool AppLoadService::WasUnloadedForReload( | |
| 148 const extensions::ExtensionId& extension_id, | |
| 149 const extensions::UnloadedExtensionInfo::Reason reason) { | |
| 150 if (reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) { | |
| 151 ExtensionPrefs* prefs = ExtensionPrefs::Get(context_); | |
| 152 return (prefs->GetDisableReasons(extension_id) & | |
| 153 Extension::DISABLE_RELOAD) != 0; | |
| 154 } | |
| 155 return false; | |
| 156 } | |
| 157 | |
| 158 bool AppLoadService::HasPostReloadAction(const std::string& extension_id) { | |
| 159 return post_reload_actions_.find(extension_id) != post_reload_actions_.end(); | |
| 160 } | |
| 161 | |
| 162 } // namespace apps | |
| OLD | NEW |