Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2820)

Unified Diff: chrome/browser/extensions/platform_app_launcher.cc

Issue 10332071: Pass command line arguments onto platform apps which provide the right intent. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix bad merge Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/platform_app_launcher.cc
diff --git a/chrome/browser/extensions/platform_app_launcher.cc b/chrome/browser/extensions/platform_app_launcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..09c8da8d48a881c7f7fe1a8de0aec8fac9d01862
--- /dev/null
+++ b/chrome/browser/extensions/platform_app_launcher.cc
@@ -0,0 +1,124 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/platform_app_launcher.h"
+
+#include "base/command_line.h"
+#include "base/file_path.h"
+#include "base/logging.h"
+#include "base/memory/ref_counted.h"
+#include "base/string_util.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/extensions/api/app/app_api.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/common/extensions/extension.h"
+#include "content/public/browser/browser_thread.h"
+#include "net/base/mime_util.h"
+#include "net/base/net_util.h"
+#include "webkit/glue/web_intent_service_data.h"
+
+using content::BrowserThread;
+
+namespace {
+
+class PlatformAppLauncher
+ : public base::RefCountedThreadSafe<PlatformAppLauncher> {
+ public:
+ PlatformAppLauncher(const CommandLine* command_line, Profile* profile,
+ const Extension* extension)
+ : command_line_(command_line),
+ profile_(profile),
+ extension_(extension) {}
+
+ void Launch() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ if (!command_line_ || !command_line_->GetArgs().size()) {
+ LaunchWithNoLaunchData();
+ return;
+ }
+
+ FilePath file_path(command_line_->GetArgs()[0]);
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
+ &PlatformAppLauncher::GetMimeTypeAndLaunch, this, file_path));
+ }
+
+ private:
+ void LaunchWithNoLaunchData() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ extensions::AppEventRouter::DispatchOnLaunchedEvent(profile_, extension_);
+ }
+
+ void GetMimeTypeAndLaunch(const FilePath& file_path) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+
+ std::string mime_type;
+ // If we cannot obtain the MIME type, launch with no launch data.
+ if (!net::GetMimeTypeFromFile(file_path, &mime_type)) {
+ LOG(WARNING) << "Could not obtain MIME type for " << file_path.value();
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
+ &PlatformAppLauncher::LaunchWithNoLaunchData, this));
+ return;
+ }
+
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
+ &PlatformAppLauncher::LaunchWithMimeTypeAndPath, this, file_path,
+ mime_type));
+ }
+
+ void LaunchWithMimeTypeAndPath(const FilePath& file_path,
+ const std::string& mime_type) {
+ // Find the intent service from the platform app for the file being opened.
+ 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
+
+ webkit_glue::WebIntentServiceData service;
+ bool found_service = false;
+
+ std::vector<webkit_glue::WebIntentServiceData> services =
+ extension_->intents_services();
+ for (size_t i = 0; i < services.size(); i++) {
+ std::string service_type_ascii = UTF16ToASCII(services[i].type);
+ if (services[i].action == kViewAction_UTF16 &&
+ net::MatchesMimeType(service_type_ascii, mime_type)) {
+ service = services[i];
+ found_service = true;
+ }
+ }
+
+ // If this app doesn't have an intent that supports the file, launch with
+ // no launch data.
+ if (!found_service) {
+ LOG(WARNING) << "Extension does not provide a valid intent for "
+ << file_path.value();
+ LaunchWithNoLaunchData();
+ return;
+ }
+
+ GURL file_url(net::FilePathToFileURL(file_path));
+ extensions::AppEventRouter::DispatchOnLaunchedEventWithUrl(
+ profile_, extension_, kViewAction_UTF16, mime_type, file_url);
+ }
+
+ const CommandLine* command_line_;
+ Profile* profile_;
+ const Extension* extension_;
+
+ DISALLOW_COPY_AND_ASSIGN(PlatformAppLauncher);
+};
+
+} // namespace
+
+namespace extensions {
+
+void LaunchPlatformApp(const CommandLine* command_line,
+ Profile* profile,
+ const Extension* extension) {
+ // launcher will be freed when nothing has a reference to it. The message
+ // queue will retain a reference for any outstanding task, so when the
+ // launcher has finished it will be freed.
+ scoped_refptr<PlatformAppLauncher> launcher =
+ new PlatformAppLauncher(command_line, profile, extension);
+ launcher->Launch();
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698