[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 5 | #include "apps/launcher.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 6 | |
[email protected] | fc243fe | 2014-07-18 13:06:23 | [diff] [blame] | 7 | #include <set> |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame^] | 8 | #include <utility> |
[email protected] | fc243fe | 2014-07-18 13:06:23 | [diff] [blame] | 9 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 11 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 13 | #include "base/logging.h" |
| 14 | #include "base/memory/ref_counted.h" |
[email protected] | 46acbf1 | 2013-06-10 18:43:42 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
[email protected] | e3b1938 | 2013-01-12 03:58:14 | [diff] [blame] | 17 | #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 18 | #include "chrome/browser/extensions/api/file_handlers/mime_util.h" |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 19 | #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 20 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 22 | #include "content/public/browser/render_process_host.h" |
[email protected] | a6db612 | 2012-09-03 06:00:23 | [diff] [blame] | 23 | #include "content/public/browser/web_contents.h" |
[email protected] | 4c35abc | 2014-05-14 02:13:58 | [diff] [blame] | 24 | #include "content/public/common/content_switches.h" |
| 25 | #include "content/public/common/url_constants.h" |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 26 | #include "extensions/browser/api/app_runtime/app_runtime_api.h" |
[email protected] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 27 | #include "extensions/browser/event_router.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 28 | #include "extensions/browser/extension_host.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 29 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 30 | #include "extensions/browser/extension_system.h" |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 31 | #include "extensions/browser/granted_file_entry.h" |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 32 | #include "extensions/browser/lazy_background_task_queue.h" |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 33 | #include "extensions/browser/process_manager.h" |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 34 | #include "extensions/common/api/app_runtime.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 35 | #include "extensions/common/extension.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 36 | #include "extensions/common/extension_messages.h" |
[email protected] | 70c39bb | 2013-11-26 22:59:28 | [diff] [blame] | 37 | #include "extensions/common/manifest_handlers/kiosk_mode_info.h" |
[email protected] | f7fc72c | 2014-04-22 13:01:52 | [diff] [blame] | 38 | #include "net/base/filename_util.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 39 | #include "net/base/net_util.h" |
[email protected] | 43197ea2 | 2013-09-10 15:31:56 | [diff] [blame] | 40 | #include "url/gurl.h" |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 41 | |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 42 | #if defined(OS_CHROMEOS) |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame^] | 43 | #include "components/user_manager/user_manager.h" |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 44 | #endif |
| 45 | |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 46 | namespace app_runtime = extensions::core_api::app_runtime; |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 47 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 48 | using content::BrowserThread; |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 49 | using extensions::AppRuntimeEventRouter; |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 50 | using extensions::app_file_handler_util::CreateFileEntry; |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 51 | using extensions::app_file_handler_util::FileHandlerCanHandleFile; |
| 52 | using extensions::app_file_handler_util::FileHandlerForId; |
| 53 | using extensions::app_file_handler_util::FirstFileHandlerForFile; |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 54 | using extensions::app_file_handler_util::HasFileSystemWritePermission; |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 55 | using extensions::app_file_handler_util::PrepareFilesForWritableApp; |
[email protected] | 3a368a2 | 2014-03-26 19:29:19 | [diff] [blame] | 56 | using extensions::EventRouter; |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 57 | using extensions::Extension; |
| 58 | using extensions::ExtensionHost; |
| 59 | using extensions::ExtensionSystem; |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 60 | using extensions::GrantedFileEntry; |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 61 | |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 62 | namespace apps { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 63 | |
| 64 | namespace { |
| 65 | |
[email protected] | 9be0bad | 2013-04-18 05:51:36 | [diff] [blame] | 66 | const char kFallbackMimeType[] = "application/octet-stream"; |
| 67 | |
[email protected] | 3567d14 | 2014-05-12 11:49:43 | [diff] [blame] | 68 | bool DoMakePathAbsolute(const base::FilePath& current_directory, |
| 69 | base::FilePath* file_path) { |
[email protected] | a5a0be0 | 2012-07-18 05:51:54 | [diff] [blame] | 70 | DCHECK(file_path); |
| 71 | if (file_path->IsAbsolute()) |
| 72 | return true; |
| 73 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 74 | if (current_directory.empty()) { |
| 75 | *file_path = base::MakeAbsoluteFilePath(*file_path); |
| 76 | return !file_path->empty(); |
| 77 | } |
[email protected] | a5a0be0 | 2012-07-18 05:51:54 | [diff] [blame] | 78 | |
| 79 | if (!current_directory.IsAbsolute()) |
| 80 | return false; |
| 81 | |
| 82 | *file_path = current_directory.Append(*file_path); |
| 83 | return true; |
| 84 | } |
| 85 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 86 | // Helper method to launch the platform app |extension| with no data. This |
| 87 | // should be called in the fallback case, where it has been impossible to |
| 88 | // load or obtain file launch data. |
| 89 | void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) { |
[email protected] | 3567d14 | 2014-05-12 11:49:43 | [diff] [blame] | 90 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 91 | AppRuntimeEventRouter::DispatchOnLaunchedEvent(profile, extension); |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 94 | // Class to handle launching of platform apps to open specific paths. |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 95 | // 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] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 99 | class PlatformAppPathLauncher |
| 100 | : public base::RefCountedThreadSafe<PlatformAppPathLauncher> { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 101 | public: |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 102 | PlatformAppPathLauncher(Profile* profile, |
| 103 | const Extension* extension, |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 104 | const std::vector<base::FilePath>& file_paths) |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 105 | : profile_(profile), |
| 106 | extension_(extension), |
| 107 | file_paths_(file_paths), |
| 108 | collector_(profile) {} |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 109 | |
| 110 | PlatformAppPathLauncher(Profile* profile, |
| 111 | const Extension* extension, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 112 | const base::FilePath& file_path) |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 113 | : profile_(profile), extension_(extension), collector_(profile) { |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 114 | if (!file_path.empty()) |
| 115 | file_paths_.push_back(file_path); |
| 116 | } |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 117 | |
| 118 | void Launch() { |
[email protected] | 3567d14 | 2014-05-12 11:49:43 | [diff] [blame] | 119 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 120 | if (file_paths_.empty()) { |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 121 | LaunchPlatformAppWithNoData(profile_, extension_); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 125 | for (size_t i = 0; i < file_paths_.size(); ++i) { |
| 126 | DCHECK(file_paths_[i].IsAbsolute()); |
| 127 | } |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 128 | |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 129 | if (HasFileSystemWritePermission(extension_)) { |
[email protected] | 07aa825a | 2014-06-03 12:39:32 | [diff] [blame] | 130 | PrepareFilesForWritableApp( |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 131 | file_paths_, |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 132 | profile_, |
[email protected] | 6b7ecdd | 2013-08-29 14:16:24 | [diff] [blame] | 133 | false, |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 134 | base::Bind(&PlatformAppPathLauncher::OnFilesValid, this), |
| 135 | base::Bind(&PlatformAppPathLauncher::OnFilesInvalid, this)); |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 136 | return; |
| 137 | } |
[email protected] | 906ae21 | 2013-03-24 01:37:13 | [diff] [blame] | 138 | |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 139 | OnFilesValid(); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 142 | void LaunchWithHandler(const std::string& handler_id) { |
| 143 | handler_id_ = handler_id; |
| 144 | Launch(); |
| 145 | } |
| 146 | |
[email protected] | 3567d14 | 2014-05-12 11:49:43 | [diff] [blame] | 147 | void LaunchWithRelativePath(const base::FilePath& current_directory) { |
| 148 | BrowserThread::PostTask( |
| 149 | BrowserThread::FILE, |
| 150 | FROM_HERE, |
| 151 | base::Bind(&PlatformAppPathLauncher::MakePathAbsolute, |
| 152 | this, |
| 153 | current_directory)); |
| 154 | } |
| 155 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 156 | private: |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 157 | friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>; |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 158 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 159 | virtual ~PlatformAppPathLauncher() {} |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 160 | |
[email protected] | 3567d14 | 2014-05-12 11:49:43 | [diff] [blame] | 161 | void MakePathAbsolute(const base::FilePath& current_directory) { |
| 162 | DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 163 | |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 164 | for (std::vector<base::FilePath>::iterator it = file_paths_.begin(); |
| 165 | it != file_paths_.end(); |
| 166 | ++it) { |
| 167 | if (!DoMakePathAbsolute(current_directory, &*it)) { |
| 168 | LOG(WARNING) << "Cannot make absolute path from " << it->value(); |
| 169 | BrowserThread::PostTask( |
| 170 | BrowserThread::UI, |
| 171 | FROM_HERE, |
| 172 | base::Bind(&PlatformAppPathLauncher::LaunchWithNoLaunchData, this)); |
| 173 | return; |
| 174 | } |
[email protected] | 3567d14 | 2014-05-12 11:49:43 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | BrowserThread::PostTask(BrowserThread::UI, |
| 178 | FROM_HERE, |
| 179 | base::Bind(&PlatformAppPathLauncher::Launch, this)); |
| 180 | } |
| 181 | |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 182 | void OnFilesValid() { |
| 183 | collector_.CollectForLocalPaths( |
| 184 | file_paths_, |
| 185 | base::Bind(&PlatformAppPathLauncher::OnMimeTypesCollected, this)); |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 188 | void OnFilesInvalid(const base::FilePath& /* error_path */) { |
[email protected] | ffb8706 | 2013-08-29 10:02:25 | [diff] [blame] | 189 | LaunchWithNoLaunchData(); |
| 190 | } |
| 191 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 192 | void LaunchWithNoLaunchData() { |
| 193 | // This method is required as an entry point on the UI thread. |
| 194 | LaunchPlatformAppWithNoData(profile_, extension_); |
| 195 | } |
| 196 | |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 197 | void OnMimeTypesCollected(scoped_ptr<std::vector<std::string> > mime_types) { |
| 198 | DCHECK(file_paths_.size() == mime_types->size()); |
| 199 | |
| 200 | // If fetching a mime type failed, then use a fallback one. |
| 201 | for (size_t i = 0; i < mime_types->size(); ++i) { |
| 202 | const std::string mime_type = |
| 203 | !(*mime_types)[i].empty() ? (*mime_types)[i] : kFallbackMimeType; |
| 204 | mime_types_.push_back(mime_type); |
| 205 | } |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 206 | |
[email protected] | 8427a062 | 2013-02-11 17:00:57 | [diff] [blame] | 207 | // Find file handler from the platform app for the file being opened. |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 208 | const extensions::FileHandlerInfo* handler = NULL; |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 209 | if (!handler_id_.empty()) { |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 210 | handler = FileHandlerForId(*extension_, handler_id_); |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 211 | if (handler) { |
| 212 | for (size_t i = 0; i < file_paths_.size(); ++i) { |
| 213 | if (!FileHandlerCanHandleFile( |
| 214 | *handler, mime_types_[i], file_paths_[i])) { |
| 215 | LOG(WARNING) |
| 216 | << "Extension does not provide a valid file handler for " |
| 217 | << file_paths_[i].value(); |
| 218 | handler = NULL; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } else { |
| 224 | std::set<std::pair<base::FilePath, std::string> > path_and_file_type_set; |
| 225 | for (size_t i = 0; i < file_paths_.size(); ++i) { |
| 226 | path_and_file_type_set.insert( |
| 227 | std::make_pair(file_paths_[i], mime_types_[i])); |
| 228 | } |
| 229 | const std::vector<const extensions::FileHandlerInfo*>& handlers = |
| 230 | extensions::app_file_handler_util::FindFileHandlersForFiles( |
| 231 | *extension_, path_and_file_type_set); |
| 232 | if (!handlers.empty()) |
| 233 | handler = handlers[0]; |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 234 | } |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 235 | |
[email protected] | 8427a062 | 2013-02-11 17:00:57 | [diff] [blame] | 236 | // If this app doesn't have a file handler that supports the file, launch |
| 237 | // with no launch data. |
| 238 | if (!handler) { |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 239 | LOG(WARNING) << "Extension does not provide a valid file handler."; |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 240 | LaunchWithNoLaunchData(); |
| 241 | return; |
| 242 | } |
| 243 | |
[email protected] | 56573d7 | 2013-05-09 06:36:37 | [diff] [blame] | 244 | if (handler_id_.empty()) |
| 245 | handler_id_ = handler->id; |
| 246 | |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 247 | // Access needs to be granted to the file for the process associated with |
| 248 | // the extension. To do this the ExtensionHost is needed. This might not be |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 249 | // available, or it might be in the process of being unloaded, in which case |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 250 | // the lazy background task queue is used to load the extension and then |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 251 | // call back to us. |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 252 | extensions::LazyBackgroundTaskQueue* const queue = |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 253 | ExtensionSystem::Get(profile_)->lazy_background_task_queue(); |
| 254 | if (queue->ShouldEnqueueTask(profile_, extension_)) { |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 255 | queue->AddPendingTask( |
| 256 | profile_, |
| 257 | extension_->id(), |
| 258 | base::Bind(&PlatformAppPathLauncher::GrantAccessToFilesAndLaunch, |
| 259 | this)); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 263 | extensions::ProcessManager* const process_manager = |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 264 | ExtensionSystem::Get(profile_)->process_manager(); |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 265 | ExtensionHost* const host = |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 266 | process_manager->GetBackgroundHostForExtension(extension_->id()); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 267 | DCHECK(host); |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 268 | GrantAccessToFilesAndLaunch(host); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 271 | void GrantAccessToFilesAndLaunch(ExtensionHost* host) { |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 272 | // If there was an error loading the app page, |host| will be NULL. |
| 273 | if (!host) { |
| 274 | LOG(ERROR) << "Could not load app page for " << extension_->id(); |
| 275 | return; |
| 276 | } |
| 277 | |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 278 | std::vector<GrantedFileEntry> file_entries; |
| 279 | for (size_t i = 0; i < file_paths_.size(); ++i) { |
| 280 | file_entries.push_back( |
| 281 | CreateFileEntry(profile_, |
| 282 | extension_, |
| 283 | host->render_process_host()->GetID(), |
| 284 | file_paths_[i], |
| 285 | false)); |
| 286 | } |
| 287 | |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 288 | AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 289 | profile_, extension_, handler_id_, mime_types_, file_entries); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 292 | // The profile the app should be run in. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 293 | Profile* profile_; |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 294 | // The extension providing the app. |
[email protected] | 3567d14 | 2014-05-12 11:49:43 | [diff] [blame] | 295 | // TODO(benwells): Hold onto the extension ID instead of a pointer as it |
| 296 | // is possible the extension will be unloaded while we're doing our thing. |
| 297 | // See http://crbug.com/372270 for details. |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 298 | const Extension* extension_; |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 299 | // The path to be passed through to the app. |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 300 | std::vector<base::FilePath> file_paths_; |
| 301 | std::vector<std::string> mime_types_; |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 302 | // The ID of the file handler used to launch the app. |
| 303 | std::string handler_id_; |
[email protected] | fb6de88 | 2014-07-03 07:50:17 | [diff] [blame] | 304 | extensions::app_file_handler_util::MimeTypeCollector collector_; |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 305 | |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 306 | DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); |
[email protected] | 4e04f1e | 2012-06-20 03:20:31 | [diff] [blame] | 307 | }; |
| 308 | |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 309 | } // namespace |
| 310 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 311 | void LaunchPlatformAppWithCommandLine(Profile* profile, |
| 312 | const Extension* extension, |
[email protected] | 7d501ce | 2013-12-17 03:49:12 | [diff] [blame] | 313 | const CommandLine& command_line, |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 314 | const base::FilePath& current_directory) { |
[email protected] | 14a18bf | 2013-09-26 08:42:30 | [diff] [blame] | 315 | // An app with "kiosk_only" should not be installed and launched |
| 316 | // outside of ChromeOS kiosk mode in the first place. This is a defensive |
| 317 | // check in case this scenario does occur. |
| 318 | if (extensions::KioskModeInfo::IsKioskOnly(extension)) { |
| 319 | bool in_kiosk_mode = false; |
| 320 | #if defined(OS_CHROMEOS) |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame^] | 321 | user_manager::UserManager* user_manager = user_manager::UserManager::Get(); |
[email protected] | 14a18bf | 2013-09-26 08:42:30 | [diff] [blame] | 322 | in_kiosk_mode = user_manager && user_manager->IsLoggedInAsKioskApp(); |
| 323 | #endif |
| 324 | if (!in_kiosk_mode) { |
| 325 | LOG(ERROR) << "App with 'kiosk_only' attribute must be run in " |
| 326 | << " ChromeOS kiosk mode."; |
| 327 | NOTREACHED(); |
[email protected] | 8a01117 | 2013-08-09 04:29:23 | [diff] [blame] | 328 | return; |
| 329 | } |
[email protected] | 14a18bf | 2013-09-26 08:42:30 | [diff] [blame] | 330 | } |
[email protected] | 8a01117 | 2013-08-09 04:29:23 | [diff] [blame] | 331 | |
[email protected] | 4c35abc | 2014-05-14 02:13:58 | [diff] [blame] | 332 | #if defined(OS_WIN) |
| 333 | base::CommandLine::StringType about_blank_url( |
[email protected] | 8e09c7af | 2014-06-10 11:46:17 | [diff] [blame] | 334 | base::ASCIIToWide(url::kAboutBlankURL)); |
[email protected] | 4c35abc | 2014-05-14 02:13:58 | [diff] [blame] | 335 | #else |
[email protected] | 8e09c7af | 2014-06-10 11:46:17 | [diff] [blame] | 336 | base::CommandLine::StringType about_blank_url(url::kAboutBlankURL); |
[email protected] | 4c35abc | 2014-05-14 02:13:58 | [diff] [blame] | 337 | #endif |
| 338 | CommandLine::StringVector args = command_line.GetArgs(); |
| 339 | // Browser tests will add about:blank to the command line. This should |
| 340 | // never be interpreted as a file to open, as doing so with an app that |
| 341 | // has write access will result in a file 'about' being created, which |
| 342 | // causes problems on the bots. |
| 343 | if (args.empty() || (command_line.HasSwitch(switches::kTestType) && |
| 344 | args[0] == about_blank_url)) { |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 345 | LaunchPlatformAppWithNoData(profile, extension); |
| 346 | return; |
| 347 | } |
| 348 | |
[email protected] | 3567d14 | 2014-05-12 11:49:43 | [diff] [blame] | 349 | base::FilePath file_path(command_line.GetArgs()[0]); |
| 350 | scoped_refptr<PlatformAppPathLauncher> launcher = |
| 351 | new PlatformAppPathLauncher(profile, extension, file_path); |
| 352 | launcher->LaunchWithRelativePath(current_directory); |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void LaunchPlatformAppWithPath(Profile* profile, |
| 356 | const Extension* extension, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 357 | const base::FilePath& file_path) { |
[email protected] | a228c84 | 2012-09-04 10:07:05 | [diff] [blame] | 358 | scoped_refptr<PlatformAppPathLauncher> launcher = |
| 359 | new PlatformAppPathLauncher(profile, extension, file_path); |
[email protected] | 12e54045 | 2012-05-26 07:09:36 | [diff] [blame] | 360 | launcher->Launch(); |
| 361 | } |
| 362 | |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 363 | void LaunchPlatformApp(Profile* profile, const Extension* extension) { |
[email protected] | 7d501ce | 2013-12-17 03:49:12 | [diff] [blame] | 364 | LaunchPlatformAppWithCommandLine(profile, |
| 365 | extension, |
| 366 | CommandLine(CommandLine::NO_PROGRAM), |
| 367 | base::FilePath()); |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 370 | void LaunchPlatformAppWithFileHandler( |
| 371 | Profile* profile, |
| 372 | const Extension* extension, |
| 373 | const std::string& handler_id, |
| 374 | const std::vector<base::FilePath>& file_paths) { |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 375 | scoped_refptr<PlatformAppPathLauncher> launcher = |
[email protected] | 3113a23 | 2014-06-04 09:40:29 | [diff] [blame] | 376 | new PlatformAppPathLauncher(profile, extension, file_paths); |
[email protected] | af8dc08e | 2012-11-22 01:58:42 | [diff] [blame] | 377 | launcher->LaunchWithHandler(handler_id); |
| 378 | } |
| 379 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 380 | void RestartPlatformApp(Profile* profile, const Extension* extension) { |
[email protected] | 3a368a2 | 2014-03-26 19:29:19 | [diff] [blame] | 381 | EventRouter* event_router = EventRouter::Get(profile); |
[email protected] | c7c0c92 | 2014-01-09 00:03:36 | [diff] [blame] | 382 | bool listening_to_restart = event_router-> |
| 383 | ExtensionHasEventListener(extension->id(), |
| 384 | app_runtime::OnRestarted::kEventName); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 385 | |
| 386 | if (listening_to_restart) { |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 387 | AppRuntimeEventRouter::DispatchOnRestartedEvent(profile, extension); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
[email protected] | 2d9f2a79 | 2014-01-24 12:44:09 | [diff] [blame] | 391 | extensions::ExtensionPrefs* extension_prefs = |
| 392 | extensions::ExtensionPrefs::Get(profile); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 393 | bool had_windows = extension_prefs->IsActive(extension->id()); |
| 394 | extension_prefs->SetIsActive(extension->id(), false); |
[email protected] | c7c0c92 | 2014-01-09 00:03:36 | [diff] [blame] | 395 | bool listening_to_launch = event_router-> |
| 396 | ExtensionHasEventListener(extension->id(), |
| 397 | app_runtime::OnLaunched::kEventName); |
[email protected] | 771c8d27 | 2013-05-17 09:47:40 | [diff] [blame] | 398 | |
| 399 | if (listening_to_launch && had_windows) |
| 400 | LaunchPlatformAppWithNoData(profile, extension); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 401 | } |
| 402 | |
[email protected] | 43197ea2 | 2013-09-10 15:31:56 | [diff] [blame] | 403 | void LaunchPlatformAppWithUrl(Profile* profile, |
| 404 | const Extension* extension, |
| 405 | const std::string& handler_id, |
| 406 | const GURL& url, |
| 407 | const GURL& referrer_url) { |
[email protected] | ce5f1b3 | 2014-06-22 01:46:45 | [diff] [blame] | 408 | AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( |
[email protected] | 43197ea2 | 2013-09-10 15:31:56 | [diff] [blame] | 409 | profile, extension, handler_id, url, referrer_url); |
| 410 | } |
| 411 | |
[email protected] | 24c81d69 | 2013-08-07 14:09:48 | [diff] [blame] | 412 | } // namespace apps |