blob: a0f747cd0c706cae1162de900af0420275844dba [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"
rkc79bf63c2016-08-25 21:07:2315#include "base/memory/ptr_util.h"
[email protected]12e540452012-05-26 07:09:3616#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"
Michael Giuffridafc9947e2017-07-26 09:37:4719#include "base/task_scheduler/post_task.h"
20#include "base/task_scheduler/task_traits.h"
michaelpg4d80e562017-04-04 01:48:1421#include "content/public/browser/browser_context.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"
[email protected]98b6d942013-11-10 00:34:0738#include "extensions/browser/process_manager.h"
[email protected]ce5f1b32014-06-22 01:46:4539#include "extensions/common/api/app_runtime.h"
[email protected]e4452d32013-11-15 23:07:4140#include "extensions/common/extension.h"
[email protected]70c39bb2013-11-26 22:59:2841#include "extensions/common/manifest_handlers/kiosk_mode_info.h"
tbarzic8bf50fca2017-06-22 03:02:1842#include "extensions/common/permissions/api_permission.h"
43#include "extensions/common/permissions/permissions_data.h"
[email protected]f7fc72c2014-04-22 13:01:5244#include "net/base/filename_util.h"
[email protected]43197ea22013-09-10 15:31:5645#include "url/gurl.h"
[email protected]12e540452012-05-26 07:09:3646
[email protected]906ae212013-03-24 01:37:1347#if defined(OS_CHROMEOS)
[email protected]4d390782014-08-15 09:22:5848#include "components/user_manager/user_manager.h"
[email protected]906ae212013-03-24 01:37:1349#endif
50
kalmane58e62232015-07-23 18:27:2251namespace app_runtime = extensions::api::app_runtime;
[email protected]e054ea12013-08-20 00:41:5752
[email protected]12e540452012-05-26 07:09:3653using content::BrowserThread;
[email protected]ce5f1b32014-06-22 01:46:4554using extensions::AppRuntimeEventRouter;
rkc79bf63c2016-08-25 21:07:2355using extensions::api::app_runtime::PlayStoreStatus;
[email protected]fc2a40f2013-03-13 13:14:5756using extensions::app_file_handler_util::CreateFileEntry;
cmihail20232c22016-02-25 02:15:2157using extensions::app_file_handler_util::FileHandlerCanHandleEntry;
[email protected]ce5f1b32014-06-22 01:46:4558using extensions::app_file_handler_util::FileHandlerForId;
[email protected]ffb87062013-08-29 10:02:2559using extensions::app_file_handler_util::HasFileSystemWritePermission;
[email protected]ce5f1b32014-06-22 01:46:4560using extensions::app_file_handler_util::PrepareFilesForWritableApp;
[email protected]3a368a22014-03-26 19:29:1961using extensions::EventRouter;
[email protected]24c81d692013-08-07 14:09:4862using extensions::Extension;
63using extensions::ExtensionHost;
[email protected]ce5f1b32014-06-22 01:46:4564using extensions::GrantedFileEntry;
[email protected]d9ede582012-08-14 19:21:3865
[email protected]24c81d692013-08-07 14:09:4866namespace apps {
[email protected]12e540452012-05-26 07:09:3667
68namespace {
69
[email protected]9be0bad2013-04-18 05:51:3670const char kFallbackMimeType[] = "application/octet-stream";
71
[email protected]3567d142014-05-12 11:49:4372bool DoMakePathAbsolute(const base::FilePath& current_directory,
73 base::FilePath* file_path) {
[email protected]a5a0be02012-07-18 05:51:5474 DCHECK(file_path);
75 if (file_path->IsAbsolute())
76 return true;
77
[email protected]15476932013-04-12 05:17:1578 if (current_directory.empty()) {
kinaba6e0570b2014-10-21 05:43:3479 base::FilePath absolute_path = base::MakeAbsoluteFilePath(*file_path);
80 if (absolute_path.empty())
81 return false;
82 *file_path = absolute_path;
83 return true;
[email protected]15476932013-04-12 05:17:1584 }
[email protected]a5a0be02012-07-18 05:51:5485
86 if (!current_directory.IsAbsolute())
87 return false;
88
89 *file_path = current_directory.Append(*file_path);
90 return true;
91}
92
[email protected]3113a232014-06-04 09:40:2993// Class to handle launching of platform apps to open specific paths.
[email protected]4e04f1e2012-06-20 03:20:3194// An instance of this class is created for each launch. The lifetime of these
95// instances is managed by reference counted pointers. As long as an instance
96// has outstanding tasks on a message queue it will be retained; once all
97// outstanding tasks are completed it will be deleted.
[email protected]a228c842012-09-04 10:07:0598class PlatformAppPathLauncher
99 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> {
[email protected]12e540452012-05-26 07:09:36100 public:
michaelpg4d80e562017-04-04 01:48:14101 PlatformAppPathLauncher(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11102 const Extension* app,
cmihail20232c22016-02-25 02:15:21103 const std::vector<base::FilePath>& entry_paths)
michaelpg4d80e562017-04-04 01:48:14104 : context_(context),
jdufault9d3e955f2016-08-16 22:19:11105 extension_id(app->id()),
cmihail20232c22016-02-25 02:15:21106 entry_paths_(entry_paths),
michaelpg4d80e562017-04-04 01:48:14107 mime_type_collector_(context),
108 is_directory_collector_(context) {}
[email protected]3113a232014-06-04 09:40:29109
michaelpg4d80e562017-04-04 01:48:14110 PlatformAppPathLauncher(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11111 const Extension* app,
[email protected]650b2d52013-02-10 03:41:45112 const base::FilePath& file_path)
michaelpg4d80e562017-04-04 01:48:14113 : context_(context),
jdufault9d3e955f2016-08-16 22:19:11114 extension_id(app->id()),
michaelpg4d80e562017-04-04 01:48:14115 mime_type_collector_(context),
116 is_directory_collector_(context) {
[email protected]3113a232014-06-04 09:40:29117 if (!file_path.empty())
cmihail20232c22016-02-25 02:15:21118 entry_paths_.push_back(file_path);
[email protected]3113a232014-06-04 09:40:29119 }
[email protected]12e540452012-05-26 07:09:36120
jdufault9d3e955f2016-08-16 22:19:11121 void set_action_data(std::unique_ptr<app_runtime::ActionData> action_data) {
122 action_data_ = std::move(action_data);
123 }
124
125 void set_launch_source(extensions::AppLaunchSource launch_source) {
126 launch_source_ = launch_source;
127 }
128
[email protected]12e540452012-05-26 07:09:36129 void Launch() {
[email protected]3567d142014-05-12 11:49:43130 DCHECK_CURRENTLY_ON(BrowserThread::UI);
benwells8af39482014-11-20 08:32:31131
jdufault9d3e955f2016-08-16 22:19:11132 const Extension* app = GetExtension();
133 if (!app)
benwells8af39482014-11-20 08:32:31134 return;
135
cmihail20232c22016-02-25 02:15:21136 if (entry_paths_.empty()) {
cylee988a9bb52014-11-04 16:39:16137 LaunchWithNoLaunchData();
[email protected]12e540452012-05-26 07:09:36138 return;
139 }
140
cmihail20232c22016-02-25 02:15:21141 for (size_t i = 0; i < entry_paths_.size(); ++i) {
142 DCHECK(entry_paths_[i].IsAbsolute());
[email protected]3113a232014-06-04 09:40:29143 }
[email protected]906ae212013-03-24 01:37:13144
cmihail20232c22016-02-25 02:15:21145 is_directory_collector_.CollectForEntriesPaths(
146 entry_paths_,
147 base::Bind(&PlatformAppPathLauncher::OnAreDirectoriesCollected, this,
jdufault9d3e955f2016-08-16 22:19:11148 HasFileSystemWritePermission(app)));
[email protected]12e540452012-05-26 07:09:36149 }
150
[email protected]af8dc08e2012-11-22 01:58:42151 void LaunchWithHandler(const std::string& handler_id) {
152 handler_id_ = handler_id;
153 Launch();
154 }
155
[email protected]3567d142014-05-12 11:49:43156 void LaunchWithRelativePath(const base::FilePath& current_directory) {
Michael Giuffridafc9947e2017-07-26 09:37:47157 base::PostTaskWithTraits(
[email protected]3567d142014-05-12 11:49:43158 FROM_HERE,
Michael Giuffridafc9947e2017-07-26 09:37:47159 {base::TaskPriority::USER_VISIBLE, base::MayBlock(),
160 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
161 base::Bind(&PlatformAppPathLauncher::MakePathAbsolute, this,
[email protected]3567d142014-05-12 11:49:43162 current_directory));
163 }
164
[email protected]12e540452012-05-26 07:09:36165 private:
[email protected]a228c842012-09-04 10:07:05166 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>;
[email protected]12e540452012-05-26 07:09:36167
[email protected]a228c842012-09-04 10:07:05168 virtual ~PlatformAppPathLauncher() {}
[email protected]12e540452012-05-26 07:09:36169
[email protected]3567d142014-05-12 11:49:43170 void MakePathAbsolute(const base::FilePath& current_directory) {
cmihail20232c22016-02-25 02:15:21171 for (std::vector<base::FilePath>::iterator it = entry_paths_.begin();
172 it != entry_paths_.end(); ++it) {
[email protected]3113a232014-06-04 09:40:29173 if (!DoMakePathAbsolute(current_directory, &*it)) {
174 LOG(WARNING) << "Cannot make absolute path from " << it->value();
175 BrowserThread::PostTask(
176 BrowserThread::UI,
177 FROM_HERE,
178 base::Bind(&PlatformAppPathLauncher::LaunchWithNoLaunchData, this));
179 return;
180 }
[email protected]3567d142014-05-12 11:49:43181 }
182
183 BrowserThread::PostTask(BrowserThread::UI,
184 FROM_HERE,
185 base::Bind(&PlatformAppPathLauncher::Launch, this));
186 }
187
mostynbecb4a22b2016-04-04 06:08:01188 void OnFilesValid(std::unique_ptr<std::set<base::FilePath>> directory_paths) {
cmihail20232c22016-02-25 02:15:21189 mime_type_collector_.CollectForLocalPaths(
190 entry_paths_,
191 base::Bind(
192 &PlatformAppPathLauncher::OnAreDirectoriesAndMimeTypesCollected,
193 this, base::Passed(std::move(directory_paths))));
[email protected]ffb87062013-08-29 10:02:25194 }
195
[email protected]fb6de882014-07-03 07:50:17196 void OnFilesInvalid(const base::FilePath& /* error_path */) {
[email protected]ffb87062013-08-29 10:02:25197 LaunchWithNoLaunchData();
198 }
199
[email protected]a228c842012-09-04 10:07:05200 void LaunchWithNoLaunchData() {
201 // This method is required as an entry point on the UI thread.
cylee988a9bb52014-11-04 16:39:16202 DCHECK_CURRENTLY_ON(BrowserThread::UI);
benwells8af39482014-11-20 08:32:31203
jdufault9d3e955f2016-08-16 22:19:11204 const Extension* app = GetExtension();
205 if (!app)
benwells8af39482014-11-20 08:32:31206 return;
207
rkc79bf63c2016-08-25 21:07:23208 std::unique_ptr<app_runtime::LaunchData> launch_data =
209 base::MakeUnique<app_runtime::LaunchData>();
210 launch_data->action_data = std::move(action_data_);
211
cylee988a9bb52014-11-04 16:39:16212 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
michaelpg4d80e562017-04-04 01:48:14213 context_, app, launch_source_, std::move(launch_data));
[email protected]a228c842012-09-04 10:07:05214 }
215
cmihail20232c22016-02-25 02:15:21216 void OnAreDirectoriesCollected(
217 bool has_file_system_write_permission,
mostynbecb4a22b2016-04-04 06:08:01218 std::unique_ptr<std::set<base::FilePath>> directory_paths) {
cmihail20232c22016-02-25 02:15:21219 if (has_file_system_write_permission) {
220 std::set<base::FilePath>* const directory_paths_ptr =
221 directory_paths.get();
222 PrepareFilesForWritableApp(
michaelpg4d80e562017-04-04 01:48:14223 entry_paths_, context_, *directory_paths_ptr,
cmihail20232c22016-02-25 02:15:21224 base::Bind(&PlatformAppPathLauncher::OnFilesValid, this,
225 base::Passed(std::move(directory_paths))),
226 base::Bind(&PlatformAppPathLauncher::OnFilesInvalid, this));
227 return;
228 }
229
230 OnFilesValid(std::move(directory_paths));
231 }
232
233 void OnAreDirectoriesAndMimeTypesCollected(
mostynbecb4a22b2016-04-04 06:08:01234 std::unique_ptr<std::set<base::FilePath>> directory_paths,
235 std::unique_ptr<std::vector<std::string>> mime_types) {
cmihail20232c22016-02-25 02:15:21236 DCHECK(entry_paths_.size() == mime_types->size());
237 // If fetching a mime type failed, then use a fallback one.
238 for (size_t i = 0; i < entry_paths_.size(); ++i) {
239 const std::string mime_type =
240 !(*mime_types)[i].empty() ? (*mime_types)[i] : kFallbackMimeType;
241 bool is_directory =
242 directory_paths->find(entry_paths_[i]) != directory_paths->end();
243 entries_.push_back(
244 extensions::EntryInfo(entry_paths_[i], mime_type, is_directory));
245 }
[email protected]fb6de882014-07-03 07:50:17246
jdufault9d3e955f2016-08-16 22:19:11247 const Extension* app = GetExtension();
248 if (!app)
benwells8af39482014-11-20 08:32:31249 return;
250
[email protected]8427a0622013-02-11 17:00:57251 // Find file handler from the platform app for the file being opened.
[email protected]24c81d692013-08-07 14:09:48252 const extensions::FileHandlerInfo* handler = NULL;
[email protected]3113a232014-06-04 09:40:29253 if (!handler_id_.empty()) {
jdufault9d3e955f2016-08-16 22:19:11254 handler = FileHandlerForId(*app, handler_id_);
[email protected]3113a232014-06-04 09:40:29255 if (handler) {
cmihail20232c22016-02-25 02:15:21256 for (size_t i = 0; i < entry_paths_.size(); ++i) {
257 if (!FileHandlerCanHandleEntry(*handler, entries_[i])) {
[email protected]3113a232014-06-04 09:40:29258 LOG(WARNING)
259 << "Extension does not provide a valid file handler for "
cmihail20232c22016-02-25 02:15:21260 << entry_paths_[i].value();
[email protected]3113a232014-06-04 09:40:29261 handler = NULL;
262 break;
263 }
264 }
265 }
266 } else {
[email protected]3113a232014-06-04 09:40:29267 const std::vector<const extensions::FileHandlerInfo*>& handlers =
cmihail20232c22016-02-25 02:15:21268 extensions::app_file_handler_util::FindFileHandlersForEntries(
jdufault9d3e955f2016-08-16 22:19:11269 *app, entries_);
[email protected]3113a232014-06-04 09:40:29270 if (!handlers.empty())
271 handler = handlers[0];
[email protected]af8dc08e2012-11-22 01:58:42272 }
[email protected]af8dc08e2012-11-22 01:58:42273
[email protected]8427a0622013-02-11 17:00:57274 // If this app doesn't have a file handler that supports the file, launch
275 // with no launch data.
276 if (!handler) {
[email protected]3113a232014-06-04 09:40:29277 LOG(WARNING) << "Extension does not provide a valid file handler.";
[email protected]12e540452012-05-26 07:09:36278 LaunchWithNoLaunchData();
279 return;
280 }
281
[email protected]56573d72013-05-09 06:36:37282 if (handler_id_.empty())
283 handler_id_ = handler->id;
284
[email protected]4e04f1e2012-06-20 03:20:31285 // Access needs to be granted to the file for the process associated with
286 // the extension. To do this the ExtensionHost is needed. This might not be
[email protected]12e540452012-05-26 07:09:36287 // available, or it might be in the process of being unloaded, in which case
[email protected]4e04f1e2012-06-20 03:20:31288 // the lazy background task queue is used to load the extension and then
[email protected]12e540452012-05-26 07:09:36289 // call back to us.
[email protected]3113a232014-06-04 09:40:29290 extensions::LazyBackgroundTaskQueue* const queue =
michaelpg4d80e562017-04-04 01:48:14291 extensions::LazyBackgroundTaskQueue::Get(context_);
292 if (queue->ShouldEnqueueTask(context_, app)) {
[email protected]3113a232014-06-04 09:40:29293 queue->AddPendingTask(
michaelpg4d80e562017-04-04 01:48:14294 context_, extension_id,
[email protected]3113a232014-06-04 09:40:29295 base::Bind(&PlatformAppPathLauncher::GrantAccessToFilesAndLaunch,
296 this));
[email protected]12e540452012-05-26 07:09:36297 return;
298 }
299
[email protected]3113a232014-06-04 09:40:29300 extensions::ProcessManager* const process_manager =
michaelpg4d80e562017-04-04 01:48:14301 extensions::ProcessManager::Get(context_);
[email protected]3113a232014-06-04 09:40:29302 ExtensionHost* const host =
benwells8af39482014-11-20 08:32:31303 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]12e540452012-05-26 07:09:36304 DCHECK(host);
[email protected]3113a232014-06-04 09:40:29305 GrantAccessToFilesAndLaunch(host);
[email protected]12e540452012-05-26 07:09:36306 }
307
[email protected]3113a232014-06-04 09:40:29308 void GrantAccessToFilesAndLaunch(ExtensionHost* host) {
jdufault9d3e955f2016-08-16 22:19:11309 const Extension* app = GetExtension();
310 if (!app)
benwells8af39482014-11-20 08:32:31311 return;
312
[email protected]12e540452012-05-26 07:09:36313 // If there was an error loading the app page, |host| will be NULL.
314 if (!host) {
benwells8af39482014-11-20 08:32:31315 LOG(ERROR) << "Could not load app page for " << extension_id;
[email protected]12e540452012-05-26 07:09:36316 return;
317 }
318
cmihail20232c22016-02-25 02:15:21319 std::vector<GrantedFileEntry> granted_entries;
320 for (size_t i = 0; i < entry_paths_.size(); ++i) {
jdufault9d3e955f2016-08-16 22:19:11321 granted_entries.push_back(
michaelpg4d80e562017-04-04 01:48:14322 CreateFileEntry(context_, app, host->render_process_host()->GetID(),
jdufault9d3e955f2016-08-16 22:19:11323 entries_[i].path, entries_[i].is_directory));
[email protected]3113a232014-06-04 09:40:29324 }
325
[email protected]ce5f1b32014-06-22 01:46:45326 AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries(
michaelpg4d80e562017-04-04 01:48:14327 context_, app, launch_source_, handler_id_, entries_, granted_entries,
jdufault9d3e955f2016-08-16 22:19:11328 std::move(action_data_));
benwells8af39482014-11-20 08:32:31329 }
330
331 const Extension* GetExtension() const {
michaelpg4d80e562017-04-04 01:48:14332 return extensions::ExtensionRegistry::Get(context_)->GetExtensionById(
benwells8af39482014-11-20 08:32:31333 extension_id, extensions::ExtensionRegistry::EVERYTHING);
[email protected]12e540452012-05-26 07:09:36334 }
335
michaelpg4d80e562017-04-04 01:48:14336 // The browser context the app should be run in.
337 content::BrowserContext* context_;
benwells8af39482014-11-20 08:32:31338 // The id of the extension providing the app. A pointer to the extension is
339 // not kept as the extension may be unloaded and deleted during the course of
340 // the launch.
341 const std::string extension_id;
jdufault9d3e955f2016-08-16 22:19:11342 extensions::AppLaunchSource launch_source_ = extensions::SOURCE_FILE_HANDLER;
343 std::unique_ptr<app_runtime::ActionData> action_data_;
cmihail20232c22016-02-25 02:15:21344 // A list of files and directories to be passed through to the app.
345 std::vector<base::FilePath> entry_paths_;
346 // A corresponding list with EntryInfo for every base::FilePath in
347 // entry_paths_.
348 std::vector<extensions::EntryInfo> entries_;
[email protected]af8dc08e2012-11-22 01:58:42349 // The ID of the file handler used to launch the app.
350 std::string handler_id_;
cmihail20232c22016-02-25 02:15:21351 extensions::app_file_handler_util::MimeTypeCollector mime_type_collector_;
352 extensions::app_file_handler_util::IsDirectoryCollector
353 is_directory_collector_;
[email protected]12e540452012-05-26 07:09:36354
[email protected]a228c842012-09-04 10:07:05355 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher);
[email protected]4e04f1e2012-06-20 03:20:31356};
357
[email protected]12e540452012-05-26 07:09:36358} // namespace
359
michaelpg4d80e562017-04-04 01:48:14360void LaunchPlatformAppWithCommandLine(content::BrowserContext* context,
rkc79bf63c2016-08-25 21:07:23361 const extensions::Extension* app,
pgal.u-szeged214274b2014-10-28 11:59:48362 const base::CommandLine& command_line,
cylee988a9bb52014-11-04 16:39:16363 const base::FilePath& current_directory,
rkc79bf63c2016-08-25 21:07:23364 extensions::AppLaunchSource source,
365 PlayStoreStatus play_store_status) {
michaelpg4d80e562017-04-04 01:48:14366 LaunchPlatformAppWithCommandLineAndLaunchId(context, app, "", command_line,
andra.paraschiv4e4fb8bb2016-12-15 11:13:54367 current_directory, source,
368 play_store_status);
369}
370
371void LaunchPlatformAppWithCommandLineAndLaunchId(
michaelpg4d80e562017-04-04 01:48:14372 content::BrowserContext* context,
andra.paraschiv4e4fb8bb2016-12-15 11:13:54373 const extensions::Extension* app,
374 const std::string& launch_id,
375 const base::CommandLine& command_line,
376 const base::FilePath& current_directory,
377 extensions::AppLaunchSource source,
378 PlayStoreStatus play_store_status) {
[email protected]14a18bf2013-09-26 08:42:30379 // An app with "kiosk_only" should not be installed and launched
380 // outside of ChromeOS kiosk mode in the first place. This is a defensive
381 // check in case this scenario does occur.
jdufault9d3e955f2016-08-16 22:19:11382 if (extensions::KioskModeInfo::IsKioskOnly(app)) {
[email protected]14a18bf2013-09-26 08:42:30383 bool in_kiosk_mode = false;
384#if defined(OS_CHROMEOS)
[email protected]4d390782014-08-15 09:22:58385 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
[email protected]14a18bf2013-09-26 08:42:30386 in_kiosk_mode = user_manager && user_manager->IsLoggedInAsKioskApp();
387#endif
388 if (!in_kiosk_mode) {
389 LOG(ERROR) << "App with 'kiosk_only' attribute must be run in "
andra.paraschiv4e4fb8bb2016-12-15 11:13:54390 << " ChromeOS kiosk mode.";
[email protected]14a18bf2013-09-26 08:42:30391 NOTREACHED();
[email protected]8a011172013-08-09 04:29:23392 return;
393 }
[email protected]14a18bf2013-09-26 08:42:30394 }
[email protected]8a011172013-08-09 04:29:23395
[email protected]4c35abc2014-05-14 02:13:58396#if defined(OS_WIN)
397 base::CommandLine::StringType about_blank_url(
thestige5c64d92014-11-07 01:19:24398 base::ASCIIToUTF16(url::kAboutBlankURL));
[email protected]4c35abc2014-05-14 02:13:58399#else
[email protected]8e09c7af2014-06-10 11:46:17400 base::CommandLine::StringType about_blank_url(url::kAboutBlankURL);
[email protected]4c35abc2014-05-14 02:13:58401#endif
pgal.u-szeged214274b2014-10-28 11:59:48402 base::CommandLine::StringVector args = command_line.GetArgs();
[email protected]4c35abc2014-05-14 02:13:58403 // Browser tests will add about:blank to the command line. This should
404 // never be interpreted as a file to open, as doing so with an app that
405 // has write access will result in a file 'about' being created, which
406 // causes problems on the bots.
407 if (args.empty() || (command_line.HasSwitch(switches::kTestType) &&
408 args[0] == about_blank_url)) {
rkc79bf63c2016-08-25 21:07:23409 std::unique_ptr<app_runtime::LaunchData> launch_data =
410 base::MakeUnique<app_runtime::LaunchData>();
411 if (play_store_status != PlayStoreStatus::PLAY_STORE_STATUS_UNKNOWN)
412 launch_data->play_store_status = play_store_status;
andra.paraschiv4e4fb8bb2016-12-15 11:13:54413 if (!launch_id.empty())
414 launch_data->id.reset(new std::string(launch_id));
michaelpg4d80e562017-04-04 01:48:14415 AppRuntimeEventRouter::DispatchOnLaunchedEvent(context, app, source,
rkc79bf63c2016-08-25 21:07:23416 std::move(launch_data));
[email protected]a228c842012-09-04 10:07:05417 return;
418 }
419
[email protected]3567d142014-05-12 11:49:43420 base::FilePath file_path(command_line.GetArgs()[0]);
421 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14422 new PlatformAppPathLauncher(context, app, file_path);
[email protected]3567d142014-05-12 11:49:43423 launcher->LaunchWithRelativePath(current_directory);
[email protected]a228c842012-09-04 10:07:05424}
425
michaelpg4d80e562017-04-04 01:48:14426void LaunchPlatformAppWithPath(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11427 const Extension* app,
[email protected]650b2d52013-02-10 03:41:45428 const base::FilePath& file_path) {
[email protected]a228c842012-09-04 10:07:05429 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14430 new PlatformAppPathLauncher(context, app, file_path);
jdufault9d3e955f2016-08-16 22:19:11431 launcher->Launch();
432}
433
434void LaunchPlatformAppWithAction(
michaelpg4d80e562017-04-04 01:48:14435 content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11436 const extensions::Extension* app,
437 std::unique_ptr<app_runtime::ActionData> action_data,
438 const base::FilePath& file_path) {
tbarzic8bf50fca2017-06-22 03:02:18439 CHECK(!action_data || !action_data->is_lock_screen_action ||
440 !*action_data->is_lock_screen_action ||
441 app->permissions_data()->HasAPIPermission(
442 extensions::APIPermission::kLockScreen))
443 << "Launching lock screen action handler requires lockScreen permission.";
444
jdufault9d3e955f2016-08-16 22:19:11445 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14446 new PlatformAppPathLauncher(context, app, file_path);
jdufault9d3e955f2016-08-16 22:19:11447 launcher->set_action_data(std::move(action_data));
448 launcher->set_launch_source(extensions::AppLaunchSource::SOURCE_UNTRACKED);
[email protected]12e540452012-05-26 07:09:36449 launcher->Launch();
450}
451
michaelpg4d80e562017-04-04 01:48:14452void LaunchPlatformApp(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11453 const Extension* app,
cylee988a9bb52014-11-04 16:39:16454 extensions::AppLaunchSource source) {
455 LaunchPlatformAppWithCommandLine(
michaelpg4d80e562017-04-04 01:48:14456 context, app, base::CommandLine(base::CommandLine::NO_PROGRAM),
jdufault9d3e955f2016-08-16 22:19:11457 base::FilePath(), source);
[email protected]2a69b942013-05-31 09:37:53458}
459
[email protected]3113a232014-06-04 09:40:29460void LaunchPlatformAppWithFileHandler(
michaelpg4d80e562017-04-04 01:48:14461 content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11462 const Extension* app,
[email protected]3113a232014-06-04 09:40:29463 const std::string& handler_id,
cmihail20232c22016-02-25 02:15:21464 const std::vector<base::FilePath>& entry_paths) {
[email protected]af8dc08e2012-11-22 01:58:42465 scoped_refptr<PlatformAppPathLauncher> launcher =
michaelpg4d80e562017-04-04 01:48:14466 new PlatformAppPathLauncher(context, app, entry_paths);
[email protected]af8dc08e2012-11-22 01:58:42467 launcher->LaunchWithHandler(handler_id);
468}
469
michaelpg4d80e562017-04-04 01:48:14470void RestartPlatformApp(content::BrowserContext* context,
471 const Extension* app) {
472 EventRouter* event_router = EventRouter::Get(context);
jdufault9d3e955f2016-08-16 22:19:11473 bool listening_to_restart = event_router->ExtensionHasEventListener(
474 app->id(), app_runtime::OnRestarted::kEventName);
[email protected]771c8d272013-05-17 09:47:40475
476 if (listening_to_restart) {
michaelpg4d80e562017-04-04 01:48:14477 AppRuntimeEventRouter::DispatchOnRestartedEvent(context, app);
[email protected]771c8d272013-05-17 09:47:40478 return;
479 }
480
[email protected]2d9f2a792014-01-24 12:44:09481 extensions::ExtensionPrefs* extension_prefs =
michaelpg4d80e562017-04-04 01:48:14482 extensions::ExtensionPrefs::Get(context);
jdufault9d3e955f2016-08-16 22:19:11483 bool had_windows = extension_prefs->IsActive(app->id());
484 extension_prefs->SetIsActive(app->id(), false);
485 bool listening_to_launch = event_router->ExtensionHasEventListener(
486 app->id(), app_runtime::OnLaunched::kEventName);
[email protected]771c8d272013-05-17 09:47:40487
cylee988a9bb52014-11-04 16:39:16488 if (listening_to_launch && had_windows) {
489 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
michaelpg4d80e562017-04-04 01:48:14490 context, app, extensions::SOURCE_RESTART, nullptr);
cylee988a9bb52014-11-04 16:39:16491 }
[email protected]fc2a40f2013-03-13 13:14:57492}
493
michaelpg4d80e562017-04-04 01:48:14494void LaunchPlatformAppWithUrl(content::BrowserContext* context,
jdufault9d3e955f2016-08-16 22:19:11495 const Extension* app,
[email protected]43197ea22013-09-10 15:31:56496 const std::string& handler_id,
497 const GURL& url,
498 const GURL& referrer_url) {
[email protected]ce5f1b32014-06-22 01:46:45499 AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
michaelpg4d80e562017-04-04 01:48:14500 context, app, handler_id, url, referrer_url);
[email protected]43197ea22013-09-10 15:31:56501}
502
[email protected]24c81d692013-08-07 14:09:48503} // namespace apps