blob: 3526566f1568ecd07f52a2d04a42cc21a920a6df [file] [log] [blame]
[email protected]2a69b942013-05-31 09:37:531// 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"
[email protected]24c81d692013-08-07 14:09:488#include "apps/launcher.h"
[email protected]f6d9b282013-08-09 11:03:209#include "apps/shell_window_registry.h"
[email protected]fdf40f3e2013-07-11 23:55:4610#include "chrome/browser/chrome_notification_types.h"
[email protected]8002cab2013-07-10 09:36:4211#include "chrome/browser/extensions/extension_host.h"
[email protected]2a69b942013-05-31 09:37:5312#include "chrome/browser/extensions/extension_prefs.h"
13#include "chrome/browser/extensions/extension_service.h"
14#include "chrome/browser/extensions/extension_system.h"
[email protected]7b9faeb72013-06-11 12:20:1715#include "chrome/browser/extensions/unpacked_installer.h"
16#include "chrome/browser/profiles/profile.h"
[email protected]2a69b942013-05-31 09:37:5317#include "content/public/browser/notification_details.h"
18#include "content/public/browser/notification_service.h"
19#include "content/public/browser/notification_types.h"
[email protected]e4452d32013-11-15 23:07:4120#include "extensions/common/extension.h"
[email protected]2a69b942013-05-31 09:37:5321
22using extensions::Extension;
23using extensions::ExtensionPrefs;
[email protected]1f56ac152013-12-04 06:06:0624using extensions::ExtensionSystem;
[email protected]2a69b942013-05-31 09:37:5325
[email protected]2a69b942013-05-31 09:37:5326namespace apps {
27
[email protected]7b9faeb72013-06-11 12:20:1728AppLoadService::PostReloadAction::PostReloadAction()
[email protected]62316842013-10-23 02:40:1329 : action_type(LAUNCH),
30 command_line(CommandLine::NO_PROGRAM) {
[email protected]7b9faeb72013-06-11 12:20:1731}
32
[email protected]2a69b942013-05-31 09:37:5333AppLoadService::AppLoadService(Profile* profile)
34 : profile_(profile) {
35 registrar_.Add(
[email protected]8002cab2013-07-10 09:36:4236 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
[email protected]2a69b942013-05-31 09:37:5337 content::NotificationService::AllSources());
38 registrar_.Add(
39 this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
40 content::NotificationService::AllSources());
41}
42
43AppLoadService::~AppLoadService() {}
44
45void AppLoadService::RestartApplication(const std::string& extension_id) {
[email protected]7b9faeb72013-06-11 12:20:1746 post_reload_actions_[extension_id].action_type = RESTART;
[email protected]2a69b942013-05-31 09:37:5347 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
48 extension_service();
49 DCHECK(service);
50 service->ReloadExtension(extension_id);
51}
52
[email protected]7b9faeb72013-06-11 12:20:1753bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path,
54 const CommandLine& command_line,
55 const base::FilePath& current_dir) {
[email protected]1f56ac152013-12-04 06:06:0656 ExtensionService* extension_service =
57 ExtensionSystem::GetForBrowserContext(profile_)->extension_service();
[email protected]7b9faeb72013-06-11 12:20:1758 std::string extension_id;
[email protected]1f56ac152013-12-04 06:06:0659 if (!extensions::UnpackedInstaller::Create(extension_service)->
[email protected]7b9faeb72013-06-11 12:20:1760 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;
[email protected]2a69b942013-05-31 09:37:5370}
71
72// static
73AppLoadService* AppLoadService::Get(Profile* profile) {
74 return apps::AppLoadServiceFactory::GetForProfile(profile);
75}
76
77void AppLoadService::Observe(int type,
78 const content::NotificationSource& source,
79 const content::NotificationDetails& details) {
80 switch (type) {
[email protected]8002cab2013-07-10 09:36:4281 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();
[email protected]85aba152013-07-31 14:35:5385 // It is possible for an extension to be unloaded before it stops loading.
86 if (!extension)
87 break;
[email protected]7b9faeb72013-06-11 12:20:1788 std::map<std::string, PostReloadAction>::iterator it =
89 post_reload_actions_.find(extension->id());
90 if (it == post_reload_actions_.end())
[email protected]2a69b942013-05-31 09:37:5391 break;
92
[email protected]7b9faeb72013-06-11 12:20:1793 switch (it->second.action_type) {
94 case LAUNCH:
[email protected]24c81d692013-08-07 14:09:4895 LaunchPlatformApp(profile_, extension);
[email protected]2a69b942013-05-31 09:37:5396 break;
[email protected]7b9faeb72013-06-11 12:20:1797 case RESTART:
[email protected]24c81d692013-08-07 14:09:4898 RestartPlatformApp(profile_, extension);
[email protected]2a69b942013-05-31 09:37:5399 break;
[email protected]7b9faeb72013-06-11 12:20:17100 case LAUNCH_WITH_COMMAND_LINE:
[email protected]24c81d692013-08-07 14:09:48101 LaunchPlatformAppWithCommandLine(
[email protected]7b9faeb72013-06-11 12:20:17102 profile_, extension, &it->second.command_line,
103 it->second.current_dir);
104 break;
[email protected]2a69b942013-05-31 09:37:53105 default:
106 NOTREACHED();
107 }
108
[email protected]7b9faeb72013-06-11 12:20:17109 post_reload_actions_.erase(it);
[email protected]2a69b942013-05-31 09:37:53110 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
[email protected]8002cab2013-07-10 09:36:42118 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;
[email protected]2a69b942013-05-31 09:37:53122 }
123 break;
124 }
125 default:
126 NOTREACHED();
127 }
128}
129
[email protected]8002cab2013-07-10 09:36:42130bool AppLoadService::HasShellWindows(const std::string& extension_id) {
[email protected]f6d9b282013-08-09 11:03:20131 return !ShellWindowRegistry::Get(profile_)->
[email protected]8002cab2013-07-10 09:36:42132 GetShellWindowsForApp(extension_id).empty();
133}
134
135bool AppLoadService::WasUnloadedForReload(
136 const extensions::UnloadedExtensionInfo& unload_info) {
[email protected]b0af4792013-10-23 09:12:13137 if (unload_info.reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) {
[email protected]8002cab2013-07-10 09:36:42138 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
139 return (prefs->GetDisableReasons(unload_info.extension->id()) &
140 Extension::DISABLE_RELOAD) != 0;
141 }
142 return false;
143}
144
145bool AppLoadService::HasPostReloadAction(const std::string& extension_id) {
146 return post_reload_actions_.find(extension_id) != post_reload_actions_.end();
147}
148
[email protected]2a69b942013-05-31 09:37:53149} // namespace apps