blob: 4a7a8ddce7057fbd4ba1c6f00afb4e69fc2fb939 [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]8002cab2013-07-10 09:36:428#include "chrome/browser/extensions/extension_host.h"
[email protected]2a69b942013-05-31 09:37:539#include "chrome/browser/extensions/extension_prefs.h"
10#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/extensions/extension_system.h"
12#include "chrome/browser/extensions/platform_app_launcher.h"
13#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]7b9faeb72013-06-11 12:20:1714#include "chrome/browser/extensions/unpacked_installer.h"
15#include "chrome/browser/profiles/profile.h"
[email protected]2a69b942013-05-31 09:37:5316#include "chrome/common/chrome_notification_types.h"
17#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
[email protected]2a69b942013-05-31 09:37:5326namespace apps {
27
[email protected]7b9faeb72013-06-11 12:20:1728AppLoadService::PostReloadAction::PostReloadAction()
29 : command_line(CommandLine::NO_PROGRAM) {
30}
31
[email protected]2a69b942013-05-31 09:37:5332AppLoadService::AppLoadService(Profile* profile)
33 : profile_(profile) {
34 registrar_.Add(
[email protected]8002cab2013-07-10 09:36:4235 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
[email protected]2a69b942013-05-31 09:37:5336 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) {
[email protected]7b9faeb72013-06-11 12:20:1745 post_reload_actions_[extension_id].action_type = RESTART;
[email protected]2a69b942013-05-31 09:37:5346 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
47 extension_service();
48 DCHECK(service);
49 service->ReloadExtension(extension_id);
50}
51
[email protected]7b9faeb72013-06-11 12:20:1752bool 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;
[email protected]2a69b942013-05-31 09:37:5367}
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) {
[email protected]8002cab2013-07-10 09:36:4278 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();
[email protected]7b9faeb72013-06-11 12:20:1782 std::map<std::string, PostReloadAction>::iterator it =
83 post_reload_actions_.find(extension->id());
84 if (it == post_reload_actions_.end())
[email protected]2a69b942013-05-31 09:37:5385 break;
86
[email protected]7b9faeb72013-06-11 12:20:1787 switch (it->second.action_type) {
88 case LAUNCH:
[email protected]2a69b942013-05-31 09:37:5389 extensions::LaunchPlatformApp(profile_, extension);
90 break;
[email protected]7b9faeb72013-06-11 12:20:1791 case RESTART:
[email protected]2a69b942013-05-31 09:37:5392 extensions::RestartPlatformApp(profile_, extension);
93 break;
[email protected]7b9faeb72013-06-11 12:20:1794 case LAUNCH_WITH_COMMAND_LINE:
95 extensions::LaunchPlatformAppWithCommandLine(
96 profile_, extension, &it->second.command_line,
97 it->second.current_dir);
98 break;
[email protected]2a69b942013-05-31 09:37:5399 default:
100 NOTREACHED();
101 }
102
[email protected]7b9faeb72013-06-11 12:20:17103 post_reload_actions_.erase(it);
[email protected]2a69b942013-05-31 09:37:53104 break;
105 }
106 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
107 const extensions::UnloadedExtensionInfo* unload_info =
108 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
109 if (!unload_info->extension->is_platform_app())
110 break;
111
[email protected]8002cab2013-07-10 09:36:42112 if (WasUnloadedForReload(*unload_info) &&
113 HasShellWindows(unload_info->extension->id()) &&
114 !HasPostReloadAction(unload_info->extension->id())) {
115 post_reload_actions_[unload_info->extension->id()].action_type = LAUNCH;
[email protected]2a69b942013-05-31 09:37:53116 }
117 break;
118 }
119 default:
120 NOTREACHED();
121 }
122}
123
[email protected]8002cab2013-07-10 09:36:42124bool AppLoadService::HasShellWindows(const std::string& extension_id) {
125 return !extensions::ShellWindowRegistry::Get(profile_)->
126 GetShellWindowsForApp(extension_id).empty();
127}
128
129bool AppLoadService::WasUnloadedForReload(
130 const extensions::UnloadedExtensionInfo& unload_info) {
131 if (unload_info.reason == extension_misc::UNLOAD_REASON_DISABLE) {
132 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
133 return (prefs->GetDisableReasons(unload_info.extension->id()) &
134 Extension::DISABLE_RELOAD) != 0;
135 }
136 return false;
137}
138
139bool AppLoadService::HasPostReloadAction(const std::string& extension_id) {
140 return post_reload_actions_.find(extension_id) != post_reload_actions_.end();
141}
142
[email protected]2a69b942013-05-31 09:37:53143} // namespace apps