blob: d2423c05e7056bbffcbde11cfd1532aa9f7a86d6 [file] [log] [blame]
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001// 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"
Ben Murdochbb1529c2013-08-08 10:24:53 +01008#include "apps/launcher.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01009#include "chrome/browser/chrome_notification_types.h"
10#include "chrome/browser/extensions/extension_host.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010011#include "chrome/browser/extensions/extension_prefs.h"
12#include "chrome/browser/extensions/extension_service.h"
13#include "chrome/browser/extensions/extension_system.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010014#include "chrome/browser/extensions/shell_window_registry.h"
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010015#include "chrome/browser/extensions/unpacked_installer.h"
16#include "chrome/browser/profiles/profile.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010017#include "chrome/common/extensions/extension.h"
18#include "chrome/common/extensions/extension_constants.h"
19#include "content/public/browser/notification_details.h"
20#include "content/public/browser/notification_service.h"
21#include "content/public/browser/notification_types.h"
22
23using extensions::Extension;
24using extensions::ExtensionPrefs;
25
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010026namespace apps {
27
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010028AppLoadService::PostReloadAction::PostReloadAction()
29 : command_line(CommandLine::NO_PROGRAM) {
30}
31
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010032AppLoadService::AppLoadService(Profile* profile)
33 : profile_(profile) {
34 registrar_.Add(
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010035 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010036 content::NotificationService::AllSources());
37 registrar_.Add(
38 this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
39 content::NotificationService::AllSources());
40}
41
42AppLoadService::~AppLoadService() {}
43
44void AppLoadService::RestartApplication(const std::string& extension_id) {
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010045 post_reload_actions_[extension_id].action_type = RESTART;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010046 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
47 extension_service();
48 DCHECK(service);
49 service->ReloadExtension(extension_id);
50}
51
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010052bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path,
53 const CommandLine& command_line,
54 const base::FilePath& current_dir) {
55 std::string extension_id;
56 if (!extensions::UnpackedInstaller::Create(profile_->GetExtensionService())->
57 LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) {
58 return false;
59 }
60
61 // Schedule the app to be launched once loaded.
62 PostReloadAction& action = post_reload_actions_[extension_id];
63 action.action_type = LAUNCH_WITH_COMMAND_LINE;
64 action.command_line = command_line;
65 action.current_dir = current_dir;
66 return true;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010067}
68
69// static
70AppLoadService* AppLoadService::Get(Profile* profile) {
71 return apps::AppLoadServiceFactory::GetForProfile(profile);
72}
73
74void AppLoadService::Observe(int type,
75 const content::NotificationSource& source,
76 const content::NotificationDetails& details) {
77 switch (type) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010078 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
79 extensions::ExtensionHost* host =
80 content::Details<extensions::ExtensionHost>(details).ptr();
81 const Extension* extension = host->extension();
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010082 // It is possible for an extension to be unloaded before it stops loading.
83 if (!extension)
84 break;
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010085 std::map<std::string, PostReloadAction>::iterator it =
86 post_reload_actions_.find(extension->id());
87 if (it == post_reload_actions_.end())
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010088 break;
89
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010090 switch (it->second.action_type) {
91 case LAUNCH:
Ben Murdochbb1529c2013-08-08 10:24:53 +010092 LaunchPlatformApp(profile_, extension);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010093 break;
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010094 case RESTART:
Ben Murdochbb1529c2013-08-08 10:24:53 +010095 RestartPlatformApp(profile_, extension);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010096 break;
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010097 case LAUNCH_WITH_COMMAND_LINE:
Ben Murdochbb1529c2013-08-08 10:24:53 +010098 LaunchPlatformAppWithCommandLine(
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +010099 profile_, extension, &it->second.command_line,
100 it->second.current_dir);
101 break;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100102 default:
103 NOTREACHED();
104 }
105
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +0100106 post_reload_actions_.erase(it);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100107 break;
108 }
109 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
110 const extensions::UnloadedExtensionInfo* unload_info =
111 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
112 if (!unload_info->extension->is_platform_app())
113 break;
114
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100115 if (WasUnloadedForReload(*unload_info) &&
116 HasShellWindows(unload_info->extension->id()) &&
117 !HasPostReloadAction(unload_info->extension->id())) {
118 post_reload_actions_[unload_info->extension->id()].action_type = LAUNCH;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100119 }
120 break;
121 }
122 default:
123 NOTREACHED();
124 }
125}
126
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100127bool AppLoadService::HasShellWindows(const std::string& extension_id) {
128 return !extensions::ShellWindowRegistry::Get(profile_)->
129 GetShellWindowsForApp(extension_id).empty();
130}
131
132bool AppLoadService::WasUnloadedForReload(
133 const extensions::UnloadedExtensionInfo& unload_info) {
134 if (unload_info.reason == extension_misc::UNLOAD_REASON_DISABLE) {
135 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
136 return (prefs->GetDisableReasons(unload_info.extension->id()) &
137 Extension::DISABLE_RELOAD) != 0;
138 }
139 return false;
140}
141
142bool AppLoadService::HasPostReloadAction(const std::string& extension_id) {
143 return post_reload_actions_.find(extension_id) != post_reload_actions_.end();
144}
145
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100146} // namespace apps