blob: 0955e75c8759d6943c1d20191581f4d1081681c5 [file] [log] [blame]
[email protected]24c81d692013-08-07 14:09:481// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]12e540452012-05-26 07:09:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]24c81d692013-08-07 14:09:485#include "apps/launcher.h"
[email protected]12e540452012-05-26 07:09:366
mostynbecb4a22b2016-04-04 06:08:017#include <memory>
[email protected]fc243fe2014-07-18 13:06:238#include <set>
[email protected]4d390782014-08-15 09:22:589#include <utility>
[email protected]fc243fe2014-07-18 13:06:2310
[email protected]12e540452012-05-26 07:09:3611#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
thestig94712702014-09-10 07:46:5913#include "base/files/file_util.h"
[email protected]12e540452012-05-26 07:09:3614#include "base/logging.h"
15#include "base/memory/ref_counted.h"
[email protected]46acbf12013-06-10 18:43:4216#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1817#include "base/strings/utf_string_conversions.h"
Gabriel Charette44db1422018-08-06 11:19:3318#include "base/task/post_task.h"
19#include "base/task/task_traits.h"
michaelpg4d80e562017-04-04 01:48:1420#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2821#include "content/public/browser/browser_task_traits.h"
[email protected]12e540452012-05-26 07:09:3622#include "content/public/browser/browser_thread.h"
[email protected]12e540452012-05-26 07:09:3623#include "content/public/browser/render_process_host.h"
[email protected]a6db6122012-09-03 06:00:2324#include "content/public/browser/web_contents.h"
[email protected]4c35abc2014-05-14 02:13:5825#include "content/public/common/content_switches.h"
26#include "content/public/common/url_constants.h"
[email protected]ce5f1b32014-06-22 01:46:4527#include "extensions/browser/api/app_runtime/app_runtime_api.h"
michaelpgbc6e379f2017-02-13 23:17:2728#include "extensions/browser/api/file_handlers/app_file_handler_util.h"
29#include "extensions/browser/api/file_handlers/directory_util.h"
30#include "extensions/browser/api/file_handlers/mime_util.h"
cmihail20232c22016-02-25 02:15:2131#include "extensions/browser/entry_info.h"
[email protected]34423532013-11-21 18:13:1032#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5733#include "extensions/browser/extension_host.h"
[email protected]489db0842014-01-22 18:20:0334#include "extensions/browser/extension_prefs.h"
benwells8af39482014-11-20 08:32:3135#include "extensions/browser/extension_registry.h"
[email protected]ce5f1b32014-06-22 01:46:4536#include "extensions/browser/granted_file_entry.h"
[email protected]9fe42042013-10-29 21:13:3337#include "extensions/browser/lazy_background_task_queue.h"
David Bertoni8269a092018-12-19 15:55:4238#include "extensions/browser/lazy_context_id.h"
[email protected]98b6d942013-11-10 00:34:0739#include "extensions/browser/process_manager.h"
[email protected]ce5f1b32014-06-22 01:46:4540#include "extensions/common/api/app_runtime.h"
[email protected]e4452d32013-11-15 23:07:4141#include "extensions/common/extension.h"
[email protected]70c39bb2013-11-26 22:59:2842#include "extensions/common/manifest_handlers/kiosk_mode_info.h"
tbarzic8bf50fca2017-06-22 03:02:1843#include "extensions/common/permissions/api_permission.h"
44#include "extensions/common/permissions/permissions_data.h"
[email protected]f7fc72c2014-04-22 13:01:5245#include "net/base/filename_util.h"
[email protected]43197ea22013-09-10 15:31:5646#include "url/gurl.h"
[email protected]12e540452012-05-26 07:09:3647
[email protected]906ae212013-03-24 01:37:1348#if defined(OS_CHROMEOS)
[email protected]4d390782014-08-15 09:22:5849#include "components/user_manager/user_manager.h"
[email protected]906ae212013-03-24 01:37:1350#endif
51
kalmane58e62232015-07-23 18:27:2252namespace app_runtime = extensions::api::app_runtime;
[email protected]e054ea12013-08-20 00:41:5753
[email protected]12e540452012-05-26 07:09:3654using content::BrowserThread;
[email protected]ce5f1b32014-06-22 01:46:4555using extensions::AppRuntimeEventRouter;
rkc79bf63c2016-08-25 21:07:2356using extensions::api::app_runtime::PlayStoreStatus;
[email protected]fc2a40f2013-03-13 13:14:5757using extensions::app_file_handler_util::CreateFileEntry;
cmihail20232c22016-02-25 02:15:2158using extensions::app_file_handler_util::FileHandlerCanHandleEntry;
[email protected]ce5f1b32014-06-22 01:46:4559using extensions::app_file_handler_util::FileHandlerForId;
[email protected]ffb87062013-08-29 10:02:2560using extensions::app_file_handler_util::HasFileSystemWritePermission;
[email protected]ce5f1b32014-06-22 01:46:4561using extensions::app_file_handler_util::PrepareFilesForWritableApp;
[email protected]3a368a22014-03-26 19:29:1962using extensions::EventRouter;
[email protected]24c81d692013-08-07 14:09:4863using extensions::Extension;
64using extensions::ExtensionHost;
[email protected]ce5f1b32014-06-22 01:46:4565using extensions::GrantedFileEntry;
[email protected]d9ede582012-08-14 19:21:3866
[email protected]24c81d692013-08-07 14:09:4867namespace apps {
[email protected]12e540452012-05-26 07:09:3668
69namespace {
70
[email protected]9be0bad2013-04-18 05:51:3671const char kFallbackMimeType[] = "application/octet-stream";
72
[email protected]3567d142014-05-12 11:49:4373bool DoMakePathAbsolute(const base::FilePath& current_directory,
74 base::FilePath* file_path) {
[email protected]a5a0be02012-07-18 05:51:5475 DCHECK(file_path);
76 if (file_path->IsAbsolute())
77 return true;
78
[email protected]15476932013-04-12 05:17:1579 if (current_directory.empty()) {
kinaba6e0570b2014-10-21 05:43:3480 base::FilePath absolute_path = base::MakeAbsoluteFilePath(*file_path);
81 if (absolute_path.empty())
82 return false;
83 *file_path = absolute_path;
84 return true;
[email protected]15476932013-04-12 05:17:1585 }
[email protected]a5a0be02012-07-18 05:51:5486
87 if (!current_directory.IsAbsolute())
88 return false;
89
90 *file_path = current_directory.Append(*file_path);
91 return true;
92}
93
[email protected]3113a232014-06-04 09:40:2994// Class to handle launching of platform apps to open specific paths.
[email protected]4e04f1e2012-06-20 03:20:3195// An instance of this class is created for each launch. The lifetime of these
96// instances is managed by reference counted pointers. As long as an instance
97// has outstanding tasks on a message queue it will be retained; once all
98// outstanding tasks are completed it will be deleted.
[email protected]a228c842012-09-04 10:07:0599class PlatformAppPathLauncher
100 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> {
[email protected]12e540452012-05-26 07:09:36101 public:
michaelpg4d80e562017-04-04 01:48:14102 PlatformAppPathLauncher(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11103 const Extension* app,
cmihail20232c22016-02-25 02:15:21104 const std::vector<base::FilePath>& entry_paths)
michaelpg4d80e562017-04-04 01:48:14105 : context_(context),
jdufault9d3e955f2016-08-16 22:19:11106 extension_id(app->id()),
cmihail20232c22016-02-25 02:15:21107 entry_paths_(entry_paths),
michaelpg4d80e562017-04-04 01:48:14108 mime_type_collector_(context),
109 is_directory_collector_(context) {}
[email protected]3113a232014-06-04 09:40:29110
michaelpg4d80e562017-04-04 01:48:14111 PlatformAppPathLauncher(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11112 const Extension* app,
[email protected]650b2d52013-02-10 03:41:45113 const base::FilePath& file_path)
michaelpg4d80e562017-04-04 01:48:14114 : context_(context),
jdufault9d3e955f2016-08-16 22:19:11115 extension_id(app->id()),
michaelpg4d80e562017-04-04 01:48:14116 mime_type_collector_(context),
117 is_directory_collector_(context) {
[email protected]3113a232014-06-04 09:40:29118 if (!file_path.empty())
cmihail20232c22016-02-25 02:15:21119 entry_paths_.push_back(file_path);
[email protected]3113a232014-06-04 09:40:29120 }
[email protected]12e540452012-05-26 07:09:36121
jdufault9d3e955f2016-08-16 22:19:11122 void set_action_data(std::unique_ptr<app_runtime::ActionData> action_data) {
123 action_data_ = std::move(action_data);
124 }
125
126 void set_launch_source(extensions::AppLaunchSource launch_source) {
127 launch_source_ = launch_source;
128 }
129
[email protected]12e540452012-05-26 07:09:36130 void Launch() {
[email protected]3567d142014-05-12 11:49:43131 DCHECK_CURRENTLY_ON(BrowserThread::UI);
benwells8af39482014-11-20 08:32:31132
jdufault9d3e955f2016-08-16 22:19:11133 const Extension* app = GetExtension();
134 if (!app)
benwells8af39482014-11-20 08:32:31135 return;
136
cmihail20232c22016-02-25 02:15:21137 if (entry_paths_.empty()) {
Sam McNallye11acd72018-11-02 04:54:34138 LaunchWithBasicData();
[email protected]12e540452012-05-26 07:09:36139 return;
140 }
141
cmihail20232c22016-02-25 02:15:21142 for (size_t i = 0; i < entry_paths_.size(); ++i) {
143 DCHECK(entry_paths_[i].IsAbsolute());
[email protected]3113a232014-06-04 09:40:29144 }
[email protected]906ae212013-03-24 01:37:13145
cmihail20232c22016-02-25 02:15:21146 is_directory_collector_.CollectForEntriesPaths(
147 entry_paths_,
148 base::Bind(&PlatformAppPathLauncher::OnAreDirectoriesCollected, this,
jdufault9d3e955f2016-08-16 22:19:11149 HasFileSystemWritePermission(app)));
[email protected]12e540452012-05-26 07:09:36150 }
151
[email protected]af8dc08e2012-11-22 01:58:42152 void LaunchWithHandler(const std::string& handler_id) {
153 handler_id_ = handler_id;
154 Launch();
155 }
156
[email protected]3567d142014-05-12 11:49:43157 void LaunchWithRelativePath(const base::FilePath& current_directory) {
Michael Giuffridafc9947e2017-07-26 09:37:47158 base::PostTaskWithTraits(
[email protected]3567d142014-05-12 11:49:43159 FROM_HERE,
Michael Giuffridafc9947e2017-07-26 09:37:47160 {base::TaskPriority::USER_VISIBLE, base::MayBlock(),
161 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
162 base::Bind(&PlatformAppPathLauncher::MakePathAbsolute, this,
[email protected]3567d142014-05-12 11:49:43163 current_directory));
164 }
165
[email protected]12e540452012-05-26 07:09:36166 private:
[email protected]a228c842012-09-04 10:07:05167 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>;
[email protected]12e540452012-05-26 07:09:36168
Chris Watkinsee8488b2017-11-27 04:06:56169 virtual ~PlatformAppPathLauncher() = default;
[email protected]12e540452012-05-26 07:09:36170
[email protected]3567d142014-05-12 11:49:43171 void MakePathAbsolute(const base::FilePath& current_directory) {
cmihail20232c22016-02-25 02:15:21172 for (std::vector<base::FilePath>::iterator it = entry_paths_.begin();
173 it != entry_paths_.end(); ++it) {
[email protected]3113a232014-06-04 09:40:29174 if (!DoMakePathAbsolute(current_directory, &*it)) {
175 LOG(WARNING) << "Cannot make absolute path from " << it->value();
Eric Seckler8652dcd52018-09-20 10:42:28176 base::PostTaskWithTraits(
177 FROM_HERE, {BrowserThread::UI},
Sam McNallye11acd72018-11-02 04:54:34178 base::BindOnce(&PlatformAppPathLauncher::LaunchWithBasicData,
179 this));
[email protected]3113a232014-06-04 09:40:29180 return;
181 }
[email protected]3567d142014-05-12 11:49:43182 }
183
Eric Seckler8652dcd52018-09-20 10:42:28184 base::PostTaskWithTraits(
185 FROM_HERE, {BrowserThread::UI},
186 base::Bind(&PlatformAppPathLauncher::Launch, this));
[email protected]3567d142014-05-12 11:49:43187 }
188
mostynbecb4a22b2016-04-04 06:08:01189 void OnFilesValid(std::unique_ptr<std::set<base::FilePath>> directory_paths) {
cmihail20232c22016-02-25 02:15:21190 mime_type_collector_.CollectForLocalPaths(
191 entry_paths_,
192 base::Bind(
193 &PlatformAppPathLauncher::OnAreDirectoriesAndMimeTypesCollected,
194 this, base::Passed(std::move(directory_paths))));
[email protected]ffb87062013-08-29 10:02:25195 }
196
[email protected]fb6de882014-07-03 07:50:17197 void OnFilesInvalid(const base::FilePath& /* error_path */) {
Sam McNallye11acd72018-11-02 04:54:34198 LaunchWithBasicData();
[email protected]ffb87062013-08-29 10:02:25199 }
200
Sam McNallye11acd72018-11-02 04:54:34201 void LaunchWithBasicData() {
[email protected]a228c842012-09-04 10:07:05202 // This method is required as an entry point on the UI thread.
cylee988a9bb52014-11-04 16:39:16203 DCHECK_CURRENTLY_ON(BrowserThread::UI);
benwells8af39482014-11-20 08:32:31204
jdufault9d3e955f2016-08-16 22:19:11205 const Extension* app = GetExtension();
206 if (!app)
benwells8af39482014-11-20 08:32:31207 return;
208
rkc79bf63c2016-08-25 21:07:23209 std::unique_ptr<app_runtime::LaunchData> launch_data =
Gyuyoung Kim637cefb2018-02-03 03:03:21210 std::make_unique<app_runtime::LaunchData>();
rkc79bf63c2016-08-25 21:07:23211 launch_data->action_data = std::move(action_data_);
Sam McNallye11acd72018-11-02 04:54:34212 if (!handler_id_.empty())
213 launch_data->id = std::make_unique<std::string>(handler_id_);
rkc79bf63c2016-08-25 21:07:23214
cylee988a9bb52014-11-04 16:39:16215 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
michaelpg4d80e562017-04-04 01:48:14216 context_, app, launch_source_, std::move(launch_data));
[email protected]a228c842012-09-04 10:07:05217 }
218
cmihail20232c22016-02-25 02:15:21219 void OnAreDirectoriesCollected(
220 bool has_file_system_write_permission,
mostynbecb4a22b2016-04-04 06:08:01221 std::unique_ptr<std::set<base::FilePath>> directory_paths) {
cmihail20232c22016-02-25 02:15:21222 if (has_file_system_write_permission) {
223 std::set<base::FilePath>* const directory_paths_ptr =
224 directory_paths.get();
225 PrepareFilesForWritableApp(
michaelpg4d80e562017-04-04 01:48:14226 entry_paths_, context_, *directory_paths_ptr,
cmihail20232c22016-02-25 02:15:21227 base::Bind(&PlatformAppPathLauncher::OnFilesValid, this,
228 base::Passed(std::move(directory_paths))),
229 base::Bind(&PlatformAppPathLauncher::OnFilesInvalid, this));
230 return;
231 }
232
233 OnFilesValid(std::move(directory_paths));
234 }
235
236 void OnAreDirectoriesAndMimeTypesCollected(
mostynbecb4a22b2016-04-04 06:08:01237 std::unique_ptr<std::set<base::FilePath>> directory_paths,
238 std::unique_ptr<std::vector<std::string>> mime_types) {
cmihail20232c22016-02-25 02:15:21239 DCHECK(entry_paths_.size() == mime_types->size());
240 // If fetching a mime type failed, then use a fallback one.
241 for (size_t i = 0; i < entry_paths_.size(); ++i) {
242 const std::string mime_type =
243 !(*mime_types)[i].empty() ? (*mime_types)[i] : kFallbackMimeType;
244 bool is_directory =
245 directory_paths->find(entry_paths_[i]) != directory_paths->end();
246 entries_.push_back(
247 extensions::EntryInfo(entry_paths_[i], mime_type, is_directory));
248 }
[email protected]fb6de882014-07-03 07:50:17249
jdufault9d3e955f2016-08-16 22:19:11250 const Extension* app = GetExtension();
251 if (!app)
benwells8af39482014-11-20 08:32:31252 return;
253
[email protected]8427a0622013-02-11 17:00:57254 // Find file handler from the platform app for the file being opened.
[email protected]24c81d692013-08-07 14:09:48255 const extensions::FileHandlerInfo* handler = NULL;
[email protected]3113a232014-06-04 09:40:29256 if (!handler_id_.empty()) {
jdufault9d3e955f2016-08-16 22:19:11257 handler = FileHandlerForId(*app, handler_id_);
[email protected]3113a232014-06-04 09:40:29258 if (handler) {
cmihail20232c22016-02-25 02:15:21259 for (size_t i = 0; i < entry_paths_.size(); ++i) {
260 if (!FileHandlerCanHandleEntry(*handler, entries_[i])) {
[email protected]3113a232014-06-04 09:40:29261 LOG(WARNING)
262 << "Extension does not provide a valid file handler for "
cmihail20232c22016-02-25 02:15:21263 << entry_paths_[i].value();
[email protected]3113a232014-06-04 09:40:29264 handler = NULL;
265 break;
266 }
267 }
268 }
269 } else {
[email protected]3113a232014-06-04 09:40:29270 const std::vector<const extensions::FileHandlerInfo*>& handlers =
cmihail20232c22016-02-25 02:15:21271 extensions::app_file_handler_util::FindFileHandlersForEntries(
jdufault9d3e955f2016-08-16 22:19:11272 *app, entries_);
[email protected]3113a232014-06-04 09:40:29273 if (!handlers.empty())
274 handler = handlers[0];
[email protected]af8dc08e2012-11-22 01:58:42275 }
[email protected]af8dc08e2012-11-22 01:58:42276
[email protected]8427a0622013-02-11 17:00:57277 // If this app doesn't have a file handler that supports the file, launch
278 // with no launch data.
279 if (!handler) {
[email protected]3113a232014-06-04 09:40:29280 LOG(WARNING) << "Extension does not provide a valid file handler.";
Sam McNallye11acd72018-11-02 04:54:34281 LaunchWithBasicData();
[email protected]12e540452012-05-26 07:09:36282 return;
283 }
284
[email protected]56573d72013-05-09 06:36:37285 if (handler_id_.empty())
286 handler_id_ = handler->id;
287
[email protected]4e04f1e2012-06-20 03:20:31288 // Access needs to be granted to the file for the process associated with
289 // the extension. To do this the ExtensionHost is needed. This might not be
[email protected]12e540452012-05-26 07:09:36290 // available, or it might be in the process of being unloaded, in which case
[email protected]4e04f1e2012-06-20 03:20:31291 // the lazy background task queue is used to load the extension and then
[email protected]12e540452012-05-26 07:09:36292 // call back to us.
[email protected]3113a232014-06-04 09:40:29293 extensions::LazyBackgroundTaskQueue* const queue =
michaelpg4d80e562017-04-04 01:48:14294 extensions::LazyBackgroundTaskQueue::Get(context_);
295 if (queue->ShouldEnqueueTask(context_, app)) {
[email protected]3113a232014-06-04 09:40:29296 queue->AddPendingTask(
David Bertoni8269a092018-12-19 15:55:42297 extensions::LazyContextId(context_, extension_id),
[email protected]3113a232014-06-04 09:40:29298 base::Bind(&PlatformAppPathLauncher::GrantAccessToFilesAndLaunch,
299 this));
[email protected]12e540452012-05-26 07:09:36300 return;
301 }
302
[email protected]3113a232014-06-04 09:40:29303 extensions::ProcessManager* const process_manager =
michaelpg4d80e562017-04-04 01:48:14304 extensions::ProcessManager::Get(context_);
[email protected]3113a232014-06-04 09:40:29305 ExtensionHost* const host =
benwells8af39482014-11-20 08:32:31306 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]12e540452012-05-26 07:09:36307 DCHECK(host);
David Bertoni643f6a942018-12-17 16:50:07308 GrantAccessToFilesAndLaunch(
309 std::make_unique<extensions::LazyContextTaskQueue::ContextInfo>(host));
[email protected]12e540452012-05-26 07:09:36310 }
311
David Bertoni643f6a942018-12-17 16:50:07312 void GrantAccessToFilesAndLaunch(
313 std::unique_ptr<extensions::LazyContextTaskQueue::ContextInfo>
314 context_info) {
jdufault9d3e955f2016-08-16 22:19:11315 const Extension* app = GetExtension();
316 if (!app)
benwells8af39482014-11-20 08:32:31317 return;
318
David Bertoni643f6a942018-12-17 16:50:07319 // If there was an error loading the app page, |context_info| will be NULL.
320 if (!context_info) {
benwells8af39482014-11-20 08:32:31321 LOG(ERROR) << "Could not load app page for " << extension_id;
[email protected]12e540452012-05-26 07:09:36322 return;
323 }
324
cmihail20232c22016-02-25 02:15:21325 std::vector<GrantedFileEntry> granted_entries;
326 for (size_t i = 0; i < entry_paths_.size(); ++i) {
David Bertoni643f6a942018-12-17 16:50:07327 granted_entries.push_back(CreateFileEntry(
328 context_, app, context_info->render_process_host->GetID(),
329 entries_[i].path, entries_[i].is_directory));
[email protected]3113a232014-06-04 09:40:29330 }
331
[email protected]ce5f1b32014-06-22 01:46:45332 AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries(
michaelpg4d80e562017-04-04 01:48:14333 context_, app, launch_source_, handler_id_, entries_, granted_entries,
jdufault9d3e955f2016-08-16 22:19:11334 std::move(action_data_));
benwells8af39482014-11-20 08:32:31335 }
336
337 const Extension* GetExtension() const {
michaelpg4d80e562017-04-04 01:48:14338 return extensions::ExtensionRegistry::Get(context_)->GetExtensionById(
benwells8af39482014-11-20 08:32:31339 extension_id, extensions::ExtensionRegistry::EVERYTHING);
[email protected]12e540452012-05-26 07:09:36340 }
341
michaelpg4d80e562017-04-04 01:48:14342 // The browser context the app should be run in.
343 content::BrowserContext* context_;
benwells8af39482014-11-20 08:32:31344 // The id of the extension providing the app. A pointer to the extension is
345 // not kept as the extension may be unloaded and deleted during the course of
346 // the launch.
347 const std::string extension_id;
jdufault9d3e955f2016-08-16 22:19:11348 extensions::AppLaunchSource launch_source_ = extensions::SOURCE_FILE_HANDLER;
349 std::unique_ptr<app_runtime::ActionData> action_data_;
cmihail20232c22016-02-25 02:15:21350 // A list of files and directories to be passed through to the app.
351 std::vector<base::FilePath> entry_paths_;
352 // A corresponding list with EntryInfo for every base::FilePath in
353 // entry_paths_.
354 std::vector<extensions::EntryInfo> entries_;
[email protected]af8dc08e2012-11-22 01:58:42355 // The ID of the file handler used to launch the app.
356 std::string handler_id_;
cmihail20232c22016-02-25 02:15:21357 extensions::app_file_handler_util::MimeTypeCollector mime_type_collector_;
358 extensions::app_file_handler_util::IsDirectoryCollector
359 is_directory_collector_;
[email protected]12e540452012-05-26 07:09:36360
[email protected]a228c842012-09-04 10:07:05361 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher);
[email protected]4e04f1e2012-06-20 03:20:31362};
363
[email protected]12e540452012-05-26 07:09:36364} // namespace
365
michaelpg4d80e562017-04-04 01:48:14366void LaunchPlatformAppWithCommandLine(content::BrowserContext* context,
rkc79bf63c2016-08-25 21:07:23367 const extensions::Extension* app,
pgal.u-szeged214274b2014-10-28 11:59:48368 const base::CommandLine& command_line,
cylee988a9bb52014-11-04 16:39:16369 const base::FilePath& current_directory,
rkc79bf63c2016-08-25 21:07:23370 extensions::AppLaunchSource source,
371 PlayStoreStatus play_store_status) {
michaelpg4d80e562017-04-04 01:48:14372 LaunchPlatformAppWithCommandLineAndLaunchId(context, app, "", command_line,
andra.paraschiv4e4fb8bb2016-12-15 11:13:54373 current_directory, source,
374 play_store_status);
375}
376
377void LaunchPlatformAppWithCommandLineAndLaunchId(
michaelpg4d80e562017-04-04 01:48:14378 content::BrowserContext* context,
andra.paraschiv4e4fb8bb2016-12-15 11:13:54379 const extensions::Extension* app,
380 const std::string& launch_id,
381 const base::CommandLine& command_line,
382 const base::FilePath& current_directory,
383 extensions::AppLaunchSource source,
384 PlayStoreStatus play_store_status) {
[email protected]14a18bf2013-09-26 08:42:30385 // An app with "kiosk_only" should not be installed and launched
386 // outside of ChromeOS kiosk mode in the first place. This is a defensive
387 // check in case this scenario does occur.
jdufault9d3e955f2016-08-16 22:19:11388 if (extensions::KioskModeInfo::IsKioskOnly(app)) {
[email protected]14a18bf2013-09-26 08:42:30389 bool in_kiosk_mode = false;
390#if defined(OS_CHROMEOS)
[email protected]4d390782014-08-15 09:22:58391 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
[email protected]14a18bf2013-09-26 08:42:30392 in_kiosk_mode = user_manager && user_manager->IsLoggedInAsKioskApp();
393#endif
394 if (!in_kiosk_mode) {
395 LOG(ERROR) << "App with 'kiosk_only' attribute must be run in "
andra.paraschiv4e4fb8bb2016-12-15 11:13:54396 << " ChromeOS kiosk mode.";
[email protected]14a18bf2013-09-26 08:42:30397 NOTREACHED();
[email protected]8a011172013-08-09 04:29:23398 return;
399 }
[email protected]14a18bf2013-09-26 08:42:30400 }
[email protected]8a011172013-08-09 04:29:23401
[email protected]4c35abc2014-05-14 02:13:58402#if defined(OS_WIN)
403 base::CommandLine::StringType about_blank_url(
thestige5c64d92014-11-07 01:19:24404 base::ASCIIToUTF16(url::kAboutBlankURL));
[email protected]4c35abc2014-05-14 02:13:58405#else
[email protected]8e09c7af2014-06-10 11:46:17406 base::CommandLine::StringType about_blank_url(url::kAboutBlankURL);
[email protected]4c35abc2014-05-14 02:13:58407#endif
pgal.u-szeged214274b2014-10-28 11:59:48408 base::CommandLine::StringVector args = command_line.GetArgs();
[email protected]4c35abc2014-05-14 02:13:58409 // Browser tests will add about:blank to the command line. This should
410 // never be interpreted as a file to open, as doing so with an app that
411 // has write access will result in a file 'about' being created, which
412 // causes problems on the bots.
413 if (args.empty() || (command_line.HasSwitch(switches::kTestType) &&
414 args[0] == about_blank_url)) {
rkc79bf63c2016-08-25 21:07:23415 std::unique_ptr<app_runtime::LaunchData> launch_data =
Gyuyoung Kim637cefb2018-02-03 03:03:21416 std::make_unique<app_runtime::LaunchData>();
rkc79bf63c2016-08-25 21:07:23417 if (play_store_status != PlayStoreStatus::PLAY_STORE_STATUS_UNKNOWN)
418 launch_data->play_store_status = play_store_status;
andra.paraschiv4e4fb8bb2016-12-15 11:13:54419 if (!launch_id.empty())
420 launch_data->id.reset(new std::string(launch_id));
michaelpg4d80e562017-04-04 01:48:14421 AppRuntimeEventRouter::DispatchOnLaunchedEvent(context, app, source,
rkc79bf63c2016-08-25 21:07:23422 std::move(launch_data));
[email protected]a228c842012-09-04 10:07:05423 return;
424 }
425
[email protected]3567d142014-05-12 11:49:43426 base::FilePath file_path(command_line.GetArgs()[0]);
427 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14428 new PlatformAppPathLauncher(context, app, file_path);
[email protected]3567d142014-05-12 11:49:43429 launcher->LaunchWithRelativePath(current_directory);
[email protected]a228c842012-09-04 10:07:05430}
431
michaelpg4d80e562017-04-04 01:48:14432void LaunchPlatformAppWithPath(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11433 const Extension* app,
[email protected]650b2d52013-02-10 03:41:45434 const base::FilePath& file_path) {
[email protected]a228c842012-09-04 10:07:05435 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14436 new PlatformAppPathLauncher(context, app, file_path);
jdufault9d3e955f2016-08-16 22:19:11437 launcher->Launch();
438}
439
440void LaunchPlatformAppWithAction(
michaelpg4d80e562017-04-04 01:48:14441 content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11442 const extensions::Extension* app,
443 std::unique_ptr<app_runtime::ActionData> action_data,
444 const base::FilePath& file_path) {
tbarzic8bf50fca2017-06-22 03:02:18445 CHECK(!action_data || !action_data->is_lock_screen_action ||
446 !*action_data->is_lock_screen_action ||
447 app->permissions_data()->HasAPIPermission(
448 extensions::APIPermission::kLockScreen))
449 << "Launching lock screen action handler requires lockScreen permission.";
450
jdufault9d3e955f2016-08-16 22:19:11451 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14452 new PlatformAppPathLauncher(context, app, file_path);
jdufault9d3e955f2016-08-16 22:19:11453 launcher->set_action_data(std::move(action_data));
454 launcher->set_launch_source(extensions::AppLaunchSource::SOURCE_UNTRACKED);
[email protected]12e540452012-05-26 07:09:36455 launcher->Launch();
456}
457
michaelpg4d80e562017-04-04 01:48:14458void LaunchPlatformApp(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11459 const Extension* app,
cylee988a9bb52014-11-04 16:39:16460 extensions::AppLaunchSource source) {
461 LaunchPlatformAppWithCommandLine(
michaelpg4d80e562017-04-04 01:48:14462 context, app, base::CommandLine(base::CommandLine::NO_PROGRAM),
jdufault9d3e955f2016-08-16 22:19:11463 base::FilePath(), source);
[email protected]2a69b942013-05-31 09:37:53464}
465
[email protected]3113a232014-06-04 09:40:29466void LaunchPlatformAppWithFileHandler(
michaelpg4d80e562017-04-04 01:48:14467 content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11468 const Extension* app,
[email protected]3113a232014-06-04 09:40:29469 const std::string& handler_id,
cmihail20232c22016-02-25 02:15:21470 const std::vector<base::FilePath>& entry_paths) {
[email protected]af8dc08e2012-11-22 01:58:42471 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14472 new PlatformAppPathLauncher(context, app, entry_paths);
[email protected]af8dc08e2012-11-22 01:58:42473 launcher->LaunchWithHandler(handler_id);
474}
475
michaelpg4d80e562017-04-04 01:48:14476void RestartPlatformApp(content::BrowserContext* context,
477 const Extension* app) {
478 EventRouter* event_router = EventRouter::Get(context);
jdufault9d3e955f2016-08-16 22:19:11479 bool listening_to_restart = event_router->ExtensionHasEventListener(
480 app->id(), app_runtime::OnRestarted::kEventName);
[email protected]771c8d272013-05-17 09:47:40481
482 if (listening_to_restart) {
michaelpg4d80e562017-04-04 01:48:14483 AppRuntimeEventRouter::DispatchOnRestartedEvent(context, app);
[email protected]771c8d272013-05-17 09:47:40484 return;
485 }
486
[email protected]2d9f2a792014-01-24 12:44:09487 extensions::ExtensionPrefs* extension_prefs =
michaelpg4d80e562017-04-04 01:48:14488 extensions::ExtensionPrefs::Get(context);
jdufault9d3e955f2016-08-16 22:19:11489 bool had_windows = extension_prefs->IsActive(app->id());
490 extension_prefs->SetIsActive(app->id(), false);
491 bool listening_to_launch = event_router->ExtensionHasEventListener(
492 app->id(), app_runtime::OnLaunched::kEventName);
[email protected]771c8d272013-05-17 09:47:40493
cylee988a9bb52014-11-04 16:39:16494 if (listening_to_launch && had_windows) {
495 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
michaelpg4d80e562017-04-04 01:48:14496 context, app, extensions::SOURCE_RESTART, nullptr);
cylee988a9bb52014-11-04 16:39:16497 }
[email protected]fc2a40f2013-03-13 13:14:57498}
499
michaelpg4d80e562017-04-04 01:48:14500void LaunchPlatformAppWithUrl(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11501 const Extension* app,
[email protected]43197ea22013-09-10 15:31:56502 const std::string& handler_id,
503 const GURL& url,
504 const GURL& referrer_url) {
[email protected]ce5f1b32014-06-22 01:46:45505 AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
michaelpg4d80e562017-04-04 01:48:14506 context, app, handler_id, url, referrer_url);
[email protected]43197ea22013-09-10 15:31:56507}
508
[email protected]24c81d692013-08-07 14:09:48509} // namespace apps