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