Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/platform_app_launcher.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/file_path.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/string_util.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "chrome/browser/extensions/api/app/app_api.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/common/extensions/extension.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "net/base/mime_util.h" | |
| 18 #include "net/base/net_util.h" | |
| 19 #include "webkit/glue/web_intent_service_data.h" | |
| 20 | |
| 21 using content::BrowserThread; | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 class PlatformAppLauncher | |
| 26 : public base::RefCountedThreadSafe<PlatformAppLauncher> { | |
| 27 public: | |
| 28 PlatformAppLauncher(const CommandLine* command_line, Profile* profile, | |
| 29 const Extension* extension) | |
| 30 : command_line_(command_line), | |
| 31 profile_(profile), | |
| 32 extension_(extension) {} | |
| 33 | |
| 34 void Launch() { | |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 36 if (!command_line_ || !command_line_->GetArgs().size()) { | |
| 37 LaunchWithNoLaunchData(); | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 FilePath file_path(command_line_->GetArgs()[0]); | |
| 42 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | |
| 43 &PlatformAppLauncher::GetMimeTypeAndLaunch, this, file_path)); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 void LaunchWithNoLaunchData() { | |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 49 extensions::AppEventRouter::DispatchOnLaunchedEvent(profile_, extension_); | |
| 50 } | |
| 51 | |
| 52 void GetMimeTypeAndLaunch(const FilePath& file_path) { | |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 54 | |
| 55 std::string mime_type; | |
| 56 // If we cannot obtain the MIME type, launch with no launch data. | |
| 57 if (!net::GetMimeTypeFromFile(file_path, &mime_type)) { | |
| 58 LOG(WARNING) << "Could not obtain MIME type for " << file_path.value(); | |
| 59 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 60 &PlatformAppLauncher::LaunchWithNoLaunchData, this)); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 65 &PlatformAppLauncher::LaunchWithMimeTypeAndPath, this, file_path, | |
| 66 mime_type)); | |
| 67 } | |
| 68 | |
| 69 void LaunchWithMimeTypeAndPath(const FilePath& file_path, | |
| 70 const std::string& mime_type) { | |
| 71 // Find the intent service from the platform app for the file being opened. | |
| 72 const string16 kViewAction_UTF16 = ASCIIToUTF16("http://webintents.org/view" ); | |
|
Mihai Parparita -not on Chrome
2012/05/15 00:39:28
There's no constant for this intent?
benwells
2012/05/16 01:35:29
No. Its used somewhere else (in the downloads code
| |
| 73 | |
| 74 webkit_glue::WebIntentServiceData service; | |
| 75 bool found_service = false; | |
| 76 | |
| 77 std::vector<webkit_glue::WebIntentServiceData> services = | |
| 78 extension_->intents_services(); | |
| 79 for (size_t i = 0; i < services.size(); i++) { | |
| 80 std::string service_type_ascii = UTF16ToASCII(services[i].type); | |
| 81 if (services[i].action == kViewAction_UTF16 && | |
| 82 net::MatchesMimeType(service_type_ascii, mime_type)) { | |
| 83 service = services[i]; | |
| 84 found_service = true; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 // If this app doesn't have an intent that supports the file, launch with | |
| 89 // no launch data. | |
| 90 if (!found_service) { | |
| 91 LOG(WARNING) << "Extension does not provide a valid intent for " | |
| 92 << file_path.value(); | |
| 93 LaunchWithNoLaunchData(); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 GURL file_url(net::FilePathToFileURL(file_path)); | |
| 98 extensions::AppEventRouter::DispatchOnLaunchedEventWithUrl( | |
| 99 profile_, extension_, kViewAction_UTF16, mime_type, file_url); | |
| 100 } | |
| 101 | |
| 102 const CommandLine* command_line_; | |
| 103 Profile* profile_; | |
| 104 const Extension* extension_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(PlatformAppLauncher); | |
| 107 }; | |
| 108 | |
| 109 } // namespace | |
| 110 | |
| 111 namespace extensions { | |
| 112 | |
| 113 void LaunchPlatformApp(const CommandLine* command_line, | |
| 114 Profile* profile, | |
| 115 const Extension* extension) { | |
| 116 // launcher will be freed when nothing has a reference to it. The message | |
| 117 // queue will retain a reference for any outstanding task, so when the | |
| 118 // launcher has finished it will be freed. | |
| 119 scoped_refptr<PlatformAppLauncher> launcher = | |
| 120 new PlatformAppLauncher(command_line, profile, extension); | |
| 121 launcher->Launch(); | |
| 122 } | |
| 123 | |
| 124 } // namespace extensions | |
| OLD | NEW |