blob: 9cf6dcdd3b78e3ccf0e8fbdb5502a801169fcbbe [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
Sebastien Marchand6d0558fd2019-01-25 16:49:3711#include "base/bind.h"
[email protected]12e540452012-05-26 07:09:3612#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
thestig94712702014-09-10 07:46:5914#include "base/files/file_util.h"
[email protected]12e540452012-05-26 07:09:3615#include "base/logging.h"
16#include "base/memory/ref_counted.h"
[email protected]46acbf12013-06-10 18:43:4217#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1818#include "base/strings/utf_string_conversions.h"
Gabriel Charette44db1422018-08-06 11:19:3319#include "base/task/post_task.h"
20#include "base/task/task_traits.h"
Jay Harrisd3191c02019-08-16 01:23:3921#include "components/services/app_service/public/cpp/file_handler_info.h"
michaelpg4d80e562017-04-04 01:48:1422#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2823#include "content/public/browser/browser_task_traits.h"
[email protected]12e540452012-05-26 07:09:3624#include "content/public/browser/browser_thread.h"
[email protected]12e540452012-05-26 07:09:3625#include "content/public/browser/render_process_host.h"
[email protected]a6db6122012-09-03 06:00:2326#include "content/public/browser/web_contents.h"
[email protected]4c35abc2014-05-14 02:13:5827#include "content/public/common/content_switches.h"
28#include "content/public/common/url_constants.h"
[email protected]ce5f1b32014-06-22 01:46:4529#include "extensions/browser/api/app_runtime/app_runtime_api.h"
michaelpgbc6e379f2017-02-13 23:17:2730#include "extensions/browser/api/file_handlers/app_file_handler_util.h"
31#include "extensions/browser/api/file_handlers/directory_util.h"
32#include "extensions/browser/api/file_handlers/mime_util.h"
cmihail20232c22016-02-25 02:15:2133#include "extensions/browser/entry_info.h"
[email protected]34423532013-11-21 18:13:1034#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5735#include "extensions/browser/extension_host.h"
[email protected]489db0842014-01-22 18:20:0336#include "extensions/browser/extension_prefs.h"
benwells8af39482014-11-20 08:32:3137#include "extensions/browser/extension_registry.h"
[email protected]ce5f1b32014-06-22 01:46:4538#include "extensions/browser/granted_file_entry.h"
David Bertoni8269a092018-12-19 15:55:4239#include "extensions/browser/lazy_context_id.h"
David Bertoni641e3ce02019-01-11 20:35:1240#include "extensions/browser/lazy_context_task_queue.h"
[email protected]98b6d942013-11-10 00:34:0741#include "extensions/browser/process_manager.h"
[email protected]ce5f1b32014-06-22 01:46:4542#include "extensions/common/api/app_runtime.h"
[email protected]e4452d32013-11-15 23:07:4143#include "extensions/common/extension.h"
[email protected]70c39bb2013-11-26 22:59:2844#include "extensions/common/manifest_handlers/kiosk_mode_info.h"
tbarzic8bf50fca2017-06-22 03:02:1845#include "extensions/common/permissions/api_permission.h"
46#include "extensions/common/permissions/permissions_data.h"
[email protected]f7fc72c2014-04-22 13:01:5247#include "net/base/filename_util.h"
[email protected]43197ea22013-09-10 15:31:5648#include "url/gurl.h"
[email protected]12e540452012-05-26 07:09:3649
[email protected]906ae212013-03-24 01:37:1350#if defined(OS_CHROMEOS)
[email protected]4d390782014-08-15 09:22:5851#include "components/user_manager/user_manager.h"
[email protected]906ae212013-03-24 01:37:1352#endif
53
kalmane58e62232015-07-23 18:27:2254namespace app_runtime = extensions::api::app_runtime;
[email protected]e054ea12013-08-20 00:41:5755
[email protected]12e540452012-05-26 07:09:3656using content::BrowserThread;
[email protected]ce5f1b32014-06-22 01:46:4557using extensions::AppRuntimeEventRouter;
[email protected]fc2a40f2013-03-13 13:14:5758using extensions::app_file_handler_util::CreateFileEntry;
cmihail20232c22016-02-25 02:15:2159using extensions::app_file_handler_util::FileHandlerCanHandleEntry;
[email protected]ce5f1b32014-06-22 01:46:4560using extensions::app_file_handler_util::FileHandlerForId;
[email protected]ffb87062013-08-29 10:02:2561using extensions::app_file_handler_util::HasFileSystemWritePermission;
[email protected]ce5f1b32014-06-22 01:46:4562using extensions::app_file_handler_util::PrepareFilesForWritableApp;
[email protected]3a368a22014-03-26 19:29:1963using extensions::EventRouter;
[email protected]24c81d692013-08-07 14:09:4864using extensions::Extension;
65using extensions::ExtensionHost;
[email protected]ce5f1b32014-06-22 01:46:4566using extensions::GrantedFileEntry;
[email protected]d9ede582012-08-14 19:21:3867
[email protected]24c81d692013-08-07 14:09:4868namespace apps {
[email protected]12e540452012-05-26 07:09:3669
70namespace {
71
[email protected]9be0bad2013-04-18 05:51:3672const char kFallbackMimeType[] = "application/octet-stream";
73
[email protected]3567d142014-05-12 11:49:4374bool DoMakePathAbsolute(const base::FilePath& current_directory,
75 base::FilePath* file_path) {
[email protected]a5a0be02012-07-18 05:51:5476 DCHECK(file_path);
77 if (file_path->IsAbsolute())
78 return true;
79
[email protected]15476932013-04-12 05:17:1580 if (current_directory.empty()) {
kinaba6e0570b2014-10-21 05:43:3481 base::FilePath absolute_path = base::MakeAbsoluteFilePath(*file_path);
82 if (absolute_path.empty())
83 return false;
84 *file_path = absolute_path;
85 return true;
[email protected]15476932013-04-12 05:17:1586 }
[email protected]a5a0be02012-07-18 05:51:5487
88 if (!current_directory.IsAbsolute())
89 return false;
90
91 *file_path = current_directory.Append(*file_path);
92 return true;
93}
94
[email protected]3113a232014-06-04 09:40:2995// Class to handle launching of platform apps to open specific paths.
[email protected]4e04f1e2012-06-20 03:20:3196// An instance of this class is created for each launch. The lifetime of these
97// instances is managed by reference counted pointers. As long as an instance
98// has outstanding tasks on a message queue it will be retained; once all
99// outstanding tasks are completed it will be deleted.
[email protected]a228c842012-09-04 10:07:05100class PlatformAppPathLauncher
101 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> {
[email protected]12e540452012-05-26 07:09:36102 public:
michaelpg4d80e562017-04-04 01:48:14103 PlatformAppPathLauncher(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11104 const Extension* app,
cmihail20232c22016-02-25 02:15:21105 const std::vector<base::FilePath>& entry_paths)
michaelpg4d80e562017-04-04 01:48:14106 : context_(context),
jdufault9d3e955f2016-08-16 22:19:11107 extension_id(app->id()),
cmihail20232c22016-02-25 02:15:21108 entry_paths_(entry_paths),
michaelpg4d80e562017-04-04 01:48:14109 mime_type_collector_(context),
110 is_directory_collector_(context) {}
[email protected]3113a232014-06-04 09:40:29111
michaelpg4d80e562017-04-04 01:48:14112 PlatformAppPathLauncher(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11113 const Extension* app,
[email protected]650b2d52013-02-10 03:41:45114 const base::FilePath& file_path)
michaelpg4d80e562017-04-04 01:48:14115 : context_(context),
jdufault9d3e955f2016-08-16 22:19:11116 extension_id(app->id()),
michaelpg4d80e562017-04-04 01:48:14117 mime_type_collector_(context),
118 is_directory_collector_(context) {
[email protected]3113a232014-06-04 09:40:29119 if (!file_path.empty())
cmihail20232c22016-02-25 02:15:21120 entry_paths_.push_back(file_path);
[email protected]3113a232014-06-04 09:40:29121 }
[email protected]12e540452012-05-26 07:09:36122
jdufault9d3e955f2016-08-16 22:19:11123 void set_action_data(std::unique_ptr<app_runtime::ActionData> action_data) {
124 action_data_ = std::move(action_data);
125 }
126
127 void set_launch_source(extensions::AppLaunchSource launch_source) {
128 launch_source_ = launch_source;
129 }
130
[email protected]12e540452012-05-26 07:09:36131 void Launch() {
[email protected]3567d142014-05-12 11:49:43132 DCHECK_CURRENTLY_ON(BrowserThread::UI);
benwells8af39482014-11-20 08:32:31133
jdufault9d3e955f2016-08-16 22:19:11134 const Extension* app = GetExtension();
135 if (!app)
benwells8af39482014-11-20 08:32:31136 return;
137
cmihail20232c22016-02-25 02:15:21138 if (entry_paths_.empty()) {
Sam McNallye11acd72018-11-02 04:54:34139 LaunchWithBasicData();
[email protected]12e540452012-05-26 07:09:36140 return;
141 }
142
cmihail20232c22016-02-25 02:15:21143 for (size_t i = 0; i < entry_paths_.size(); ++i) {
144 DCHECK(entry_paths_[i].IsAbsolute());
[email protected]3113a232014-06-04 09:40:29145 }
[email protected]906ae212013-03-24 01:37:13146
cmihail20232c22016-02-25 02:15:21147 is_directory_collector_.CollectForEntriesPaths(
148 entry_paths_,
Reilly Grante78baea12019-11-26 23:33:11149 base::BindOnce(&PlatformAppPathLauncher::OnAreDirectoriesCollected,
150 this, HasFileSystemWritePermission(app)));
[email protected]12e540452012-05-26 07:09:36151 }
152
[email protected]af8dc08e2012-11-22 01:58:42153 void LaunchWithHandler(const std::string& handler_id) {
154 handler_id_ = handler_id;
155 Launch();
156 }
157
[email protected]3567d142014-05-12 11:49:43158 void LaunchWithRelativePath(const base::FilePath& current_directory) {
Sami Kyostila13e85b32019-08-09 07:20:57159 base::PostTask(
[email protected]3567d142014-05-12 11:49:43160 FROM_HERE,
Sami Kyostila13e85b32019-08-09 07:20:57161 {base::ThreadPool(), base::TaskPriority::USER_VISIBLE, base::MayBlock(),
Michael Giuffridafc9947e2017-07-26 09:37:47162 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
kylechar6de6f022019-02-26 00:14:59163 base::BindOnce(&PlatformAppPathLauncher::MakePathAbsolute, this,
164 current_directory));
[email protected]3567d142014-05-12 11:49:43165 }
166
[email protected]12e540452012-05-26 07:09:36167 private:
[email protected]a228c842012-09-04 10:07:05168 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>;
[email protected]12e540452012-05-26 07:09:36169
Chris Watkinsee8488b2017-11-27 04:06:56170 virtual ~PlatformAppPathLauncher() = default;
[email protected]12e540452012-05-26 07:09:36171
[email protected]3567d142014-05-12 11:49:43172 void MakePathAbsolute(const base::FilePath& current_directory) {
cmihail20232c22016-02-25 02:15:21173 for (std::vector<base::FilePath>::iterator it = entry_paths_.begin();
174 it != entry_paths_.end(); ++it) {
[email protected]3113a232014-06-04 09:40:29175 if (!DoMakePathAbsolute(current_directory, &*it)) {
176 LOG(WARNING) << "Cannot make absolute path from " << it->value();
Sami Kyostila13e85b32019-08-09 07:20:57177 base::PostTask(
Eric Seckler8652dcd52018-09-20 10:42:28178 FROM_HERE, {BrowserThread::UI},
Sam McNallye11acd72018-11-02 04:54:34179 base::BindOnce(&PlatformAppPathLauncher::LaunchWithBasicData,
180 this));
[email protected]3113a232014-06-04 09:40:29181 return;
182 }
[email protected]3567d142014-05-12 11:49:43183 }
184
Sami Kyostila13e85b32019-08-09 07:20:57185 base::PostTask(FROM_HERE, {BrowserThread::UI},
186 base::BindOnce(&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_,
Reilly Grante78baea12019-11-26 23:33:11192 base::BindOnce(
cmihail20232c22016-02-25 02:15:21193 &PlatformAppPathLauncher::OnAreDirectoriesAndMimeTypesCollected,
Reilly Grante78baea12019-11-26 23:33:11194 this, 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,
Reilly Grante78baea12019-11-26 23:33:11227 base::BindOnce(&PlatformAppPathLauncher::OnFilesValid, this,
228 std::move(directory_paths)),
229 base::BindOnce(&PlatformAppPathLauncher::OnFilesInvalid, this));
cmihail20232c22016-02-25 02:15:21230 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.
Jay Harrisd3191c02019-08-16 01:23:39255 const FileHandlerInfo* handler = nullptr;
[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();
Jay Harrisd3191c02019-08-16 01:23:39264 handler = nullptr;
[email protected]3113a232014-06-04 09:40:29265 break;
266 }
267 }
268 }
269 } else {
Alex Danilo1ef3e572019-02-27 08:48:12270 const std::vector<extensions::FileHandlerMatch> handlers =
271 extensions::app_file_handler_util::FindFileHandlerMatchesForEntries(
jdufault9d3e955f2016-08-16 22:19:11272 *app, entries_);
[email protected]3113a232014-06-04 09:40:29273 if (!handlers.empty())
Alex Danilo1ef3e572019-02-27 08:48:12274 handler = handlers[0].handler;
[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.
David Bertoni641e3ce02019-01-11 20:35:12293 const extensions::LazyContextId context_id(context_, extension_id);
294 extensions::LazyContextTaskQueue* const queue = context_id.GetTaskQueue();
michaelpg4d80e562017-04-04 01:48:14295 if (queue->ShouldEnqueueTask(context_, app)) {
[email protected]3113a232014-06-04 09:40:29296 queue->AddPendingTask(
David Bertoni641e3ce02019-01-11 20:35:12297 context_id,
Reilly Grante78baea12019-11-26 23:33:11298 base::BindOnce(&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;
Eric Willigers618e1302019-06-23 23:03:47348 extensions::AppLaunchSource launch_source_ =
349 extensions::AppLaunchSource::kSourceFileHandler;
jdufault9d3e955f2016-08-16 22:19:11350 std::unique_ptr<app_runtime::ActionData> action_data_;
cmihail20232c22016-02-25 02:15:21351 // A list of files and directories to be passed through to the app.
352 std::vector<base::FilePath> entry_paths_;
353 // A corresponding list with EntryInfo for every base::FilePath in
354 // entry_paths_.
355 std::vector<extensions::EntryInfo> entries_;
[email protected]af8dc08e2012-11-22 01:58:42356 // The ID of the file handler used to launch the app.
357 std::string handler_id_;
cmihail20232c22016-02-25 02:15:21358 extensions::app_file_handler_util::MimeTypeCollector mime_type_collector_;
359 extensions::app_file_handler_util::IsDirectoryCollector
360 is_directory_collector_;
[email protected]12e540452012-05-26 07:09:36361
[email protected]a228c842012-09-04 10:07:05362 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher);
[email protected]4e04f1e2012-06-20 03:20:31363};
364
[email protected]12e540452012-05-26 07:09:36365} // namespace
366
michaelpg4d80e562017-04-04 01:48:14367void LaunchPlatformAppWithCommandLine(content::BrowserContext* context,
rkc79bf63c2016-08-25 21:07:23368 const extensions::Extension* app,
pgal.u-szeged214274b2014-10-28 11:59:48369 const base::CommandLine& command_line,
cylee988a9bb52014-11-04 16:39:16370 const base::FilePath& current_directory,
Eric Willigers24c32282019-06-11 05:59:14371 extensions::AppLaunchSource source) {
michaelpg4d80e562017-04-04 01:48:14372 LaunchPlatformAppWithCommandLineAndLaunchId(context, app, "", command_line,
Eric Willigers24c32282019-06-11 05:59:14373 current_directory, source);
andra.paraschiv4e4fb8bb2016-12-15 11:13:54374}
375
376void LaunchPlatformAppWithCommandLineAndLaunchId(
michaelpg4d80e562017-04-04 01:48:14377 content::BrowserContext* context,
andra.paraschiv4e4fb8bb2016-12-15 11:13:54378 const extensions::Extension* app,
379 const std::string& launch_id,
380 const base::CommandLine& command_line,
381 const base::FilePath& current_directory,
Eric Willigers24c32282019-06-11 05:59:14382 extensions::AppLaunchSource source) {
[email protected]14a18bf2013-09-26 08:42:30383 // An app with "kiosk_only" should not be installed and launched
384 // outside of ChromeOS kiosk mode in the first place. This is a defensive
385 // check in case this scenario does occur.
jdufault9d3e955f2016-08-16 22:19:11386 if (extensions::KioskModeInfo::IsKioskOnly(app)) {
[email protected]14a18bf2013-09-26 08:42:30387 bool in_kiosk_mode = false;
388#if defined(OS_CHROMEOS)
[email protected]4d390782014-08-15 09:22:58389 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
[email protected]14a18bf2013-09-26 08:42:30390 in_kiosk_mode = user_manager && user_manager->IsLoggedInAsKioskApp();
391#endif
392 if (!in_kiosk_mode) {
393 LOG(ERROR) << "App with 'kiosk_only' attribute must be run in "
andra.paraschiv4e4fb8bb2016-12-15 11:13:54394 << " ChromeOS kiosk mode.";
[email protected]14a18bf2013-09-26 08:42:30395 NOTREACHED();
[email protected]8a011172013-08-09 04:29:23396 return;
397 }
[email protected]14a18bf2013-09-26 08:42:30398 }
[email protected]8a011172013-08-09 04:29:23399
[email protected]4c35abc2014-05-14 02:13:58400#if defined(OS_WIN)
401 base::CommandLine::StringType about_blank_url(
thestige5c64d92014-11-07 01:19:24402 base::ASCIIToUTF16(url::kAboutBlankURL));
[email protected]4c35abc2014-05-14 02:13:58403#else
[email protected]8e09c7af2014-06-10 11:46:17404 base::CommandLine::StringType about_blank_url(url::kAboutBlankURL);
[email protected]4c35abc2014-05-14 02:13:58405#endif
pgal.u-szeged214274b2014-10-28 11:59:48406 base::CommandLine::StringVector args = command_line.GetArgs();
[email protected]4c35abc2014-05-14 02:13:58407 // Browser tests will add about:blank to the command line. This should
408 // never be interpreted as a file to open, as doing so with an app that
409 // has write access will result in a file 'about' being created, which
410 // causes problems on the bots.
411 if (args.empty() || (command_line.HasSwitch(switches::kTestType) &&
412 args[0] == about_blank_url)) {
rkc79bf63c2016-08-25 21:07:23413 std::unique_ptr<app_runtime::LaunchData> launch_data =
Gyuyoung Kim637cefb2018-02-03 03:03:21414 std::make_unique<app_runtime::LaunchData>();
andra.paraschiv4e4fb8bb2016-12-15 11:13:54415 if (!launch_id.empty())
416 launch_data->id.reset(new std::string(launch_id));
michaelpg4d80e562017-04-04 01:48:14417 AppRuntimeEventRouter::DispatchOnLaunchedEvent(context, app, source,
rkc79bf63c2016-08-25 21:07:23418 std::move(launch_data));
[email protected]a228c842012-09-04 10:07:05419 return;
420 }
421
[email protected]3567d142014-05-12 11:49:43422 base::FilePath file_path(command_line.GetArgs()[0]);
423 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14424 new PlatformAppPathLauncher(context, app, file_path);
[email protected]3567d142014-05-12 11:49:43425 launcher->LaunchWithRelativePath(current_directory);
[email protected]a228c842012-09-04 10:07:05426}
427
michaelpg4d80e562017-04-04 01:48:14428void LaunchPlatformAppWithPath(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11429 const Extension* app,
[email protected]650b2d52013-02-10 03:41:45430 const base::FilePath& file_path) {
[email protected]a228c842012-09-04 10:07:05431 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14432 new PlatformAppPathLauncher(context, app, file_path);
jdufault9d3e955f2016-08-16 22:19:11433 launcher->Launch();
434}
435
436void LaunchPlatformAppWithAction(
michaelpg4d80e562017-04-04 01:48:14437 content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11438 const extensions::Extension* app,
439 std::unique_ptr<app_runtime::ActionData> action_data,
440 const base::FilePath& file_path) {
tbarzic8bf50fca2017-06-22 03:02:18441 CHECK(!action_data || !action_data->is_lock_screen_action ||
442 !*action_data->is_lock_screen_action ||
443 app->permissions_data()->HasAPIPermission(
444 extensions::APIPermission::kLockScreen))
445 << "Launching lock screen action handler requires lockScreen permission.";
446
jdufault9d3e955f2016-08-16 22:19:11447 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14448 new PlatformAppPathLauncher(context, app, file_path);
jdufault9d3e955f2016-08-16 22:19:11449 launcher->set_action_data(std::move(action_data));
Eric Willigers618e1302019-06-23 23:03:47450 launcher->set_launch_source(extensions::AppLaunchSource::kSourceUntracked);
[email protected]12e540452012-05-26 07:09:36451 launcher->Launch();
452}
453
michaelpg4d80e562017-04-04 01:48:14454void LaunchPlatformApp(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11455 const Extension* app,
cylee988a9bb52014-11-04 16:39:16456 extensions::AppLaunchSource source) {
457 LaunchPlatformAppWithCommandLine(
michaelpg4d80e562017-04-04 01:48:14458 context, app, base::CommandLine(base::CommandLine::NO_PROGRAM),
jdufault9d3e955f2016-08-16 22:19:11459 base::FilePath(), source);
[email protected]2a69b942013-05-31 09:37:53460}
461
[email protected]3113a232014-06-04 09:40:29462void LaunchPlatformAppWithFileHandler(
michaelpg4d80e562017-04-04 01:48:14463 content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11464 const Extension* app,
[email protected]3113a232014-06-04 09:40:29465 const std::string& handler_id,
cmihail20232c22016-02-25 02:15:21466 const std::vector<base::FilePath>& entry_paths) {
[email protected]af8dc08e2012-11-22 01:58:42467 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14468 new PlatformAppPathLauncher(context, app, entry_paths);
[email protected]af8dc08e2012-11-22 01:58:42469 launcher->LaunchWithHandler(handler_id);
470}
471
michaelpg4d80e562017-04-04 01:48:14472void RestartPlatformApp(content::BrowserContext* context,
473 const Extension* app) {
474 EventRouter* event_router = EventRouter::Get(context);
jdufault9d3e955f2016-08-16 22:19:11475 bool listening_to_restart = event_router->ExtensionHasEventListener(
476 app->id(), app_runtime::OnRestarted::kEventName);
[email protected]771c8d272013-05-17 09:47:40477
478 if (listening_to_restart) {
michaelpg4d80e562017-04-04 01:48:14479 AppRuntimeEventRouter::DispatchOnRestartedEvent(context, app);
[email protected]771c8d272013-05-17 09:47:40480 return;
481 }
482
[email protected]2d9f2a792014-01-24 12:44:09483 extensions::ExtensionPrefs* extension_prefs =
michaelpg4d80e562017-04-04 01:48:14484 extensions::ExtensionPrefs::Get(context);
jdufault9d3e955f2016-08-16 22:19:11485 bool had_windows = extension_prefs->IsActive(app->id());
486 extension_prefs->SetIsActive(app->id(), false);
487 bool listening_to_launch = event_router->ExtensionHasEventListener(
488 app->id(), app_runtime::OnLaunched::kEventName);
[email protected]771c8d272013-05-17 09:47:40489
cylee988a9bb52014-11-04 16:39:16490 if (listening_to_launch && had_windows) {
491 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
Eric Willigers618e1302019-06-23 23:03:47492 context, app, extensions::AppLaunchSource::kSourceRestart, nullptr);
cylee988a9bb52014-11-04 16:39:16493 }
[email protected]fc2a40f2013-03-13 13:14:57494}
495
michaelpg4d80e562017-04-04 01:48:14496void LaunchPlatformAppWithUrl(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11497 const Extension* app,
[email protected]43197ea22013-09-10 15:31:56498 const std::string& handler_id,
499 const GURL& url,
500 const GURL& referrer_url) {
[email protected]ce5f1b32014-06-22 01:46:45501 AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
michaelpg4d80e562017-04-04 01:48:14502 context, app, handler_id, url, referrer_url);
[email protected]43197ea22013-09-10 15:31:56503}
504
[email protected]24c81d692013-08-07 14:09:48505} // namespace apps