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