OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/launcher.h" | 5 #include "apps/launcher.h" |
6 | 6 |
7 #include "apps/browser/api/app_runtime/app_runtime_api.h" | 7 #include "apps/browser/api/app_runtime/app_runtime_api.h" |
8 #include "apps/browser/file_handler_util.h" | 8 #include "apps/browser/file_handler_util.h" |
9 #include "apps/common/api/app_runtime.h" | 9 #include "apps/common/api/app_runtime.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 } | 82 } |
83 | 83 |
84 // Helper method to launch the platform app |extension| with no data. This | 84 // Helper method to launch the platform app |extension| with no data. This |
85 // should be called in the fallback case, where it has been impossible to | 85 // should be called in the fallback case, where it has been impossible to |
86 // load or obtain file launch data. | 86 // load or obtain file launch data. |
87 void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) { | 87 void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) { |
88 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 88 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
89 AppEventRouter::DispatchOnLaunchedEvent(profile, extension); | 89 AppEventRouter::DispatchOnLaunchedEvent(profile, extension); |
90 } | 90 } |
91 | 91 |
92 // Class to handle launching of platform apps to open a specific path. | 92 // Class to handle launching of platform apps to open specific paths. |
93 // An instance of this class is created for each launch. The lifetime of these | 93 // An instance of this class is created for each launch. The lifetime of these |
94 // instances is managed by reference counted pointers. As long as an instance | 94 // instances is managed by reference counted pointers. As long as an instance |
95 // has outstanding tasks on a message queue it will be retained; once all | 95 // has outstanding tasks on a message queue it will be retained; once all |
96 // outstanding tasks are completed it will be deleted. | 96 // outstanding tasks are completed it will be deleted. |
97 class PlatformAppPathLauncher | 97 class PlatformAppPathLauncher |
98 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> { | 98 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> { |
99 public: | 99 public: |
100 PlatformAppPathLauncher(Profile* profile, | 100 PlatformAppPathLauncher(Profile* profile, |
101 const Extension* extension, | 101 const Extension* extension, |
102 const std::vector<base::FilePath>& file_paths) | |
103 : profile_(profile), extension_(extension), file_paths_(file_paths) {} | |
104 | |
105 PlatformAppPathLauncher(Profile* profile, | |
106 const Extension* extension, | |
102 const base::FilePath& file_path) | 107 const base::FilePath& file_path) |
103 : profile_(profile), extension_(extension), file_path_(file_path) {} | 108 : profile_(profile), extension_(extension) { |
109 if (!file_path.empty()) | |
110 file_paths_.push_back(file_path); | |
111 } | |
104 | 112 |
105 void Launch() { | 113 void Launch() { |
106 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 114 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
107 if (file_path_.empty()) { | 115 if (file_paths_.empty()) { |
108 LaunchPlatformAppWithNoData(profile_, extension_); | 116 LaunchPlatformAppWithNoData(profile_, extension_); |
109 return; | 117 return; |
110 } | 118 } |
111 | 119 |
112 DCHECK(file_path_.IsAbsolute()); | 120 for (size_t i = 0; i < file_paths_.size(); ++i) { |
121 DCHECK(file_paths_[i].IsAbsolute()); | |
122 } | |
113 | 123 |
114 if (HasFileSystemWritePermission(extension_)) { | 124 if (HasFileSystemWritePermission(extension_)) { |
115 std::vector<base::FilePath> paths; | |
116 paths.push_back(file_path_); | |
117 CheckWritableFiles( | 125 CheckWritableFiles( |
118 paths, | 126 file_paths_, |
119 profile_, | 127 profile_, |
120 false, | 128 false, |
121 base::Bind(&PlatformAppPathLauncher::OnFileValid, this), | 129 base::Bind(&PlatformAppPathLauncher::OnFileValid, this), |
122 base::Bind(&PlatformAppPathLauncher::OnFileInvalid, this)); | 130 base::Bind(&PlatformAppPathLauncher::OnFileInvalid, this)); |
123 return; | 131 return; |
124 } | 132 } |
125 | 133 |
126 OnFileValid(); | 134 OnFileValid(); |
127 } | 135 } |
128 | 136 |
(...skipping 12 matching lines...) Expand all Loading... | |
141 } | 149 } |
142 | 150 |
143 private: | 151 private: |
144 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>; | 152 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>; |
145 | 153 |
146 virtual ~PlatformAppPathLauncher() {} | 154 virtual ~PlatformAppPathLauncher() {} |
147 | 155 |
148 void MakePathAbsolute(const base::FilePath& current_directory) { | 156 void MakePathAbsolute(const base::FilePath& current_directory) { |
149 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 157 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
150 | 158 |
151 if (!DoMakePathAbsolute(current_directory, &file_path_)) { | 159 for (std::vector<base::FilePath>::iterator it = file_paths_.begin(); |
152 LOG(WARNING) << "Cannot make absolute path from " << file_path_.value(); | 160 it != file_paths_.end();) { |
153 file_path_ = base::FilePath(); | 161 if (!DoMakePathAbsolute(current_directory, &*it)) { |
162 LOG(WARNING) << "Cannot make absolute path from " << it->value(); | |
163 file_paths_.erase(it); | |
benwells
2014/05/28 22:46:14
should this be it = file_path_.erase(it)?
I'd be
hirono
2014/05/29 04:08:03
Done.
| |
164 } else { | |
165 ++it; | |
166 } | |
154 } | 167 } |
155 | 168 |
156 BrowserThread::PostTask(BrowserThread::UI, | 169 BrowserThread::PostTask(BrowserThread::UI, |
157 FROM_HERE, | 170 FROM_HERE, |
158 base::Bind(&PlatformAppPathLauncher::Launch, this)); | 171 base::Bind(&PlatformAppPathLauncher::Launch, this)); |
159 } | 172 } |
160 | 173 |
161 void OnFileValid() { | 174 void OnFileValid() { StepToFillMimeTypes(); } |
175 | |
176 void OnFileInvalid(const base::FilePath& /* error_path */) { | |
177 LaunchWithNoLaunchData(); | |
178 } | |
179 | |
180 void StepToFillMimeTypes() { | |
benwells
2014/05/28 22:46:14
This logic is more complicated than it needs to be
hirono
2014/05/29 04:08:03
Done.
| |
181 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
182 DCHECK(file_paths_.size() >= mime_types_.size()); | |
183 | |
184 if (file_paths_.size() == mime_types_.size()) { | |
185 LaunchWithMimeType(); | |
186 return; | |
187 } | |
188 | |
162 #if defined(OS_CHROMEOS) | 189 #if defined(OS_CHROMEOS) |
163 if (file_manager::util::IsUnderSpecialPath(profile_, file_path_)) { | 190 if (file_manager::util::IsUnderSpecialPath( |
191 profile_, file_paths_[mime_types_.size()])) { | |
164 file_manager::util::GetSpecialPathMimeType( | 192 file_manager::util::GetSpecialPathMimeType( |
165 profile_, | 193 profile_, |
166 file_path_, | 194 file_paths_[mime_types_.size()], |
167 base::Bind(&PlatformAppPathLauncher::OnGotMimeType, this)); | 195 base::Bind(&PlatformAppPathLauncher::OnGotMimeType, this)); |
168 return; | 196 return; |
169 } | 197 } |
170 #endif | 198 #endif |
171 | 199 |
172 BrowserThread::PostTask( | 200 BrowserThread::PostTask( |
173 BrowserThread::FILE, | 201 BrowserThread::FILE, |
174 FROM_HERE, | 202 FROM_HERE, |
175 base::Bind(&PlatformAppPathLauncher::GetMimeTypeAndLaunch, this)); | 203 base::Bind(&PlatformAppPathLauncher::GetMimeTypeAndStep, this)); |
176 } | 204 } |
177 | 205 |
178 void OnFileInvalid(const base::FilePath& /* error_path */) { | 206 void GetMimeTypeAndStep() { |
179 LaunchWithNoLaunchData(); | |
180 } | |
181 | |
182 void GetMimeTypeAndLaunch() { | |
183 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 207 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
184 | 208 |
209 const base::FilePath& file_path = file_paths_[mime_types_.size()]; | |
210 | |
185 // If the file doesn't exist, or is a directory, launch with no launch data. | 211 // If the file doesn't exist, or is a directory, launch with no launch data. |
186 if (!base::PathExists(file_path_) || | 212 if (!base::PathExists(file_path) || base::DirectoryExists(file_path)) { |
187 base::DirectoryExists(file_path_)) { | 213 LOG(WARNING) << "No file exists with path " << file_path.value(); |
188 LOG(WARNING) << "No file exists with path " << file_path_.value(); | 214 BrowserThread::PostTask( |
189 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | 215 BrowserThread::UI, |
190 &PlatformAppPathLauncher::LaunchWithNoLaunchData, this)); | 216 FROM_HERE, |
217 base::Bind(&PlatformAppPathLauncher::LaunchWithNoLaunchData, this)); | |
191 return; | 218 return; |
192 } | 219 } |
193 | 220 |
194 std::string mime_type; | 221 std::string mime_type; |
195 if (!net::GetMimeTypeFromFile(file_path_, &mime_type)) { | 222 if (!net::GetMimeTypeFromFile(file_path, &mime_type)) { |
196 // If MIME type of the file can't be determined by its path, | 223 // If MIME type of the file can't be determined by its path, |
197 // try to sniff it by its content. | 224 // try to sniff it by its content. |
198 std::vector<char> content(net::kMaxBytesToSniff); | 225 std::vector<char> content(net::kMaxBytesToSniff); |
199 int bytes_read = base::ReadFile(file_path_, &content[0], content.size()); | 226 int bytes_read = base::ReadFile(file_path, &content[0], content.size()); |
200 if (bytes_read >= 0) { | 227 if (bytes_read >= 0) { |
201 net::SniffMimeType(&content[0], | 228 net::SniffMimeType(&content[0], |
202 bytes_read, | 229 bytes_read, |
203 net::FilePathToFileURL(file_path_), | 230 net::FilePathToFileURL(file_path), |
204 std::string(), // type_hint (passes no hint) | 231 std::string(), // type_hint (passes no hint) |
205 &mime_type); | 232 &mime_type); |
206 } | 233 } |
207 if (mime_type.empty()) | 234 if (mime_type.empty()) |
208 mime_type = kFallbackMimeType; | 235 mime_type = kFallbackMimeType; |
209 } | 236 } |
210 | 237 mime_types_.push_back(mime_type); |
211 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | 238 BrowserThread::PostTask( |
212 &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type)); | 239 BrowserThread::UI, |
240 FROM_HERE, | |
241 base::Bind(&PlatformAppPathLauncher::StepToFillMimeTypes, this)); | |
213 } | 242 } |
214 | 243 |
215 #if defined(OS_CHROMEOS) | 244 #if defined(OS_CHROMEOS) |
216 void OnGotMimeType(bool success, const std::string& mime_type) { | 245 void OnGotMimeType(bool success, const std::string& mime_type) { |
217 if (!success) { | 246 if (!success) { |
218 LaunchWithNoLaunchData(); | 247 LaunchWithNoLaunchData(); |
219 return; | 248 return; |
220 } | 249 } |
221 LaunchWithMimeType(mime_type.empty() ? kFallbackMimeType : mime_type); | 250 mime_types_.push_back(mime_type.empty() ? kFallbackMimeType : mime_type); |
251 StepToFillMimeTypes(); | |
222 } | 252 } |
223 #endif | 253 #endif |
224 | 254 |
225 void LaunchWithNoLaunchData() { | 255 void LaunchWithNoLaunchData() { |
226 // This method is required as an entry point on the UI thread. | 256 // This method is required as an entry point on the UI thread. |
227 LaunchPlatformAppWithNoData(profile_, extension_); | 257 LaunchPlatformAppWithNoData(profile_, extension_); |
228 } | 258 } |
229 | 259 |
230 void LaunchWithMimeType(const std::string& mime_type) { | 260 void LaunchWithMimeType() { |
261 DCHECK(file_paths_.size() == mime_types_.size()); | |
262 | |
231 // Find file handler from the platform app for the file being opened. | 263 // Find file handler from the platform app for the file being opened. |
232 const extensions::FileHandlerInfo* handler = NULL; | 264 const extensions::FileHandlerInfo* handler = NULL; |
233 if (!handler_id_.empty()) | 265 if (!handler_id_.empty()) { |
234 handler = FileHandlerForId(*extension_, handler_id_); | 266 handler = FileHandlerForId(*extension_, handler_id_); |
235 else | 267 if (handler) { |
236 handler = FirstFileHandlerForFile(*extension_, mime_type, file_path_); | 268 for (size_t i = 0; i < file_paths_.size(); ++i) { |
237 if (handler && !FileHandlerCanHandleFile(*handler, mime_type, file_path_)) { | 269 if (!FileHandlerCanHandleFile( |
238 LOG(WARNING) << "Extension does not provide a valid file handler for " | 270 *handler, mime_types_[i], file_paths_[i])) { |
239 << file_path_.value(); | 271 LOG(WARNING) |
240 LaunchWithNoLaunchData(); | 272 << "Extension does not provide a valid file handler for " |
241 return; | 273 << file_paths_[i].value(); |
274 handler = NULL; | |
275 break; | |
276 } | |
277 } | |
278 } | |
279 } else { | |
280 std::set<std::pair<base::FilePath, std::string> > path_and_file_type_set; | |
281 for (size_t i = 0; i < file_paths_.size(); ++i) { | |
282 path_and_file_type_set.insert( | |
283 std::make_pair(file_paths_[i], mime_types_[i])); | |
284 } | |
285 const std::vector<const extensions::FileHandlerInfo*>& handlers = | |
286 extensions::app_file_handler_util::FindFileHandlersForFiles( | |
287 *extension_, path_and_file_type_set); | |
288 if (!handlers.empty()) | |
289 handler = handlers[0]; | |
242 } | 290 } |
243 | 291 |
244 // If this app doesn't have a file handler that supports the file, launch | 292 // If this app doesn't have a file handler that supports the file, launch |
245 // with no launch data. | 293 // with no launch data. |
246 if (!handler) { | 294 if (!handler) { |
247 LOG(WARNING) << "Extension does not provide a valid file handler for " | 295 LOG(WARNING) << "Extension does not provide a valid file handler."; |
248 << file_path_.value(); | |
249 LaunchWithNoLaunchData(); | 296 LaunchWithNoLaunchData(); |
250 return; | 297 return; |
251 } | 298 } |
252 | 299 |
253 if (handler_id_.empty()) | 300 if (handler_id_.empty()) |
254 handler_id_ = handler->id; | 301 handler_id_ = handler->id; |
255 | 302 |
256 // Access needs to be granted to the file for the process associated with | 303 // Access needs to be granted to the file for the process associated with |
257 // the extension. To do this the ExtensionHost is needed. This might not be | 304 // the extension. To do this the ExtensionHost is needed. This might not be |
258 // available, or it might be in the process of being unloaded, in which case | 305 // available, or it might be in the process of being unloaded, in which case |
259 // the lazy background task queue is used to load the extension and then | 306 // the lazy background task queue is used to load the extension and then |
260 // call back to us. | 307 // call back to us. |
261 extensions::LazyBackgroundTaskQueue* queue = | 308 extensions::LazyBackgroundTaskQueue* const queue = |
262 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); | 309 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); |
263 if (queue->ShouldEnqueueTask(profile_, extension_)) { | 310 if (queue->ShouldEnqueueTask(profile_, extension_)) { |
264 queue->AddPendingTask(profile_, extension_->id(), base::Bind( | 311 queue->AddPendingTask( |
265 &PlatformAppPathLauncher::GrantAccessToFileAndLaunch, | 312 profile_, |
266 this, mime_type)); | 313 extension_->id(), |
314 base::Bind(&PlatformAppPathLauncher::GrantAccessToFileAndLaunch, | |
315 this)); | |
267 return; | 316 return; |
268 } | 317 } |
269 | 318 |
270 extensions::ProcessManager* process_manager = | 319 extensions::ProcessManager* const process_manager = |
271 ExtensionSystem::Get(profile_)->process_manager(); | 320 ExtensionSystem::Get(profile_)->process_manager(); |
272 ExtensionHost* host = | 321 ExtensionHost* const host = |
273 process_manager->GetBackgroundHostForExtension(extension_->id()); | 322 process_manager->GetBackgroundHostForExtension(extension_->id()); |
274 DCHECK(host); | 323 DCHECK(host); |
275 GrantAccessToFileAndLaunch(mime_type, host); | 324 GrantAccessToFileAndLaunch(host); |
276 } | 325 } |
277 | 326 |
278 void GrantAccessToFileAndLaunch(const std::string& mime_type, | 327 void GrantAccessToFileAndLaunch(ExtensionHost* host) { |
benwells
2014/05/28 22:46:14
Nit should be GrantAccessToFilesAndLaunch
hirono
2014/05/29 04:08:03
Done.
| |
279 ExtensionHost* host) { | |
280 // If there was an error loading the app page, |host| will be NULL. | 328 // If there was an error loading the app page, |host| will be NULL. |
281 if (!host) { | 329 if (!host) { |
282 LOG(ERROR) << "Could not load app page for " << extension_->id(); | 330 LOG(ERROR) << "Could not load app page for " << extension_->id(); |
283 return; | 331 return; |
284 } | 332 } |
285 | 333 |
286 GrantedFileEntry file_entry = | 334 std::vector<GrantedFileEntry> file_entries; |
287 CreateFileEntry(profile_, | 335 for (size_t i = 0; i < file_paths_.size(); ++i) { |
288 extension_, | 336 file_entries.push_back( |
289 host->render_process_host()->GetID(), | 337 CreateFileEntry(profile_, |
290 file_path_, | 338 extension_, |
291 false); | 339 host->render_process_host()->GetID(), |
292 AppEventRouter::DispatchOnLaunchedEventWithFileEntry( | 340 file_paths_[i], |
293 profile_, extension_, handler_id_, mime_type, file_entry); | 341 false)); |
342 } | |
343 | |
344 AppEventRouter::DispatchOnLaunchedEventWithFileEntries( | |
345 profile_, extension_, handler_id_, mime_types_, file_entries); | |
294 } | 346 } |
295 | 347 |
296 // The profile the app should be run in. | 348 // The profile the app should be run in. |
297 Profile* profile_; | 349 Profile* profile_; |
298 // The extension providing the app. | 350 // The extension providing the app. |
299 // TODO(benwells): Hold onto the extension ID instead of a pointer as it | 351 // TODO(benwells): Hold onto the extension ID instead of a pointer as it |
300 // is possible the extension will be unloaded while we're doing our thing. | 352 // is possible the extension will be unloaded while we're doing our thing. |
301 // See http://crbug.com/372270 for details. | 353 // See http://crbug.com/372270 for details. |
302 const Extension* extension_; | 354 const Extension* extension_; |
303 // The path to be passed through to the app. | 355 // The path to be passed through to the app. |
304 base::FilePath file_path_; | 356 std::vector<base::FilePath> file_paths_; |
357 std::vector<std::string> mime_types_; | |
305 // The ID of the file handler used to launch the app. | 358 // The ID of the file handler used to launch the app. |
306 std::string handler_id_; | 359 std::string handler_id_; |
307 | 360 |
308 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); | 361 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); |
309 }; | 362 }; |
310 | 363 |
311 } // namespace | 364 } // namespace |
312 | 365 |
313 void LaunchPlatformAppWithCommandLine(Profile* profile, | 366 void LaunchPlatformAppWithCommandLine(Profile* profile, |
314 const Extension* extension, | 367 const Extension* extension, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
362 launcher->Launch(); | 415 launcher->Launch(); |
363 } | 416 } |
364 | 417 |
365 void LaunchPlatformApp(Profile* profile, const Extension* extension) { | 418 void LaunchPlatformApp(Profile* profile, const Extension* extension) { |
366 LaunchPlatformAppWithCommandLine(profile, | 419 LaunchPlatformAppWithCommandLine(profile, |
367 extension, | 420 extension, |
368 CommandLine(CommandLine::NO_PROGRAM), | 421 CommandLine(CommandLine::NO_PROGRAM), |
369 base::FilePath()); | 422 base::FilePath()); |
370 } | 423 } |
371 | 424 |
372 void LaunchPlatformAppWithFileHandler(Profile* profile, | 425 void LaunchPlatformAppWithFileHandler( |
373 const Extension* extension, | 426 Profile* profile, |
374 const std::string& handler_id, | 427 const Extension* extension, |
375 const base::FilePath& file_path) { | 428 const std::string& handler_id, |
429 const std::vector<base::FilePath>& file_paths) { | |
376 scoped_refptr<PlatformAppPathLauncher> launcher = | 430 scoped_refptr<PlatformAppPathLauncher> launcher = |
377 new PlatformAppPathLauncher(profile, extension, file_path); | 431 new PlatformAppPathLauncher(profile, extension, file_paths); |
378 launcher->LaunchWithHandler(handler_id); | 432 launcher->LaunchWithHandler(handler_id); |
379 } | 433 } |
380 | 434 |
381 void RestartPlatformApp(Profile* profile, const Extension* extension) { | 435 void RestartPlatformApp(Profile* profile, const Extension* extension) { |
382 EventRouter* event_router = EventRouter::Get(profile); | 436 EventRouter* event_router = EventRouter::Get(profile); |
383 bool listening_to_restart = event_router-> | 437 bool listening_to_restart = event_router-> |
384 ExtensionHasEventListener(extension->id(), | 438 ExtensionHasEventListener(extension->id(), |
385 app_runtime::OnRestarted::kEventName); | 439 app_runtime::OnRestarted::kEventName); |
386 | 440 |
387 if (listening_to_restart) { | 441 if (listening_to_restart) { |
(...skipping 16 matching lines...) Expand all Loading... | |
404 void LaunchPlatformAppWithUrl(Profile* profile, | 458 void LaunchPlatformAppWithUrl(Profile* profile, |
405 const Extension* extension, | 459 const Extension* extension, |
406 const std::string& handler_id, | 460 const std::string& handler_id, |
407 const GURL& url, | 461 const GURL& url, |
408 const GURL& referrer_url) { | 462 const GURL& referrer_url) { |
409 AppEventRouter::DispatchOnLaunchedEventWithUrl( | 463 AppEventRouter::DispatchOnLaunchedEventWithUrl( |
410 profile, extension, handler_id, url, referrer_url); | 464 profile, extension, handler_id, url, referrer_url); |
411 } | 465 } |
412 | 466 |
413 } // namespace apps | 467 } // namespace apps |
OLD | NEW |