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

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

Issue 9845003: Pass command line arguments onto platform apps which provide the right intent. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Removed unneeded header Created 8 years, 9 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_intent_dispatcher.cc
diff --git a/chrome/browser/extensions/platform_app_intent_dispatcher.cc b/chrome/browser/extensions/platform_app_intent_dispatcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..47a03579e8b182f47a31d36f38b546ed5e02b0ba
--- /dev/null
+++ b/chrome/browser/extensions/platform_app_intent_dispatcher.cc
@@ -0,0 +1,109 @@
+// 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_intent_dispatcher.h"
+
+#include "base/bind.h"
+#include "base/command_line.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/string16.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/extensions/extension_host.h"
+#include "chrome/common/extensions/extension_messages.h"
+#include "chrome/common/extensions/url_pattern.h"
+#include "chrome/common/extensions/url_pattern_set.h"
+#include "content/public/browser/child_process_security_policy.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/web_intents_dispatcher.h"
+#include "content/public/common/url_constants.h"
+#include "googleurl/src/gurl.h"
+#include "net/base/mime_util.h"
+#include "net/base/net_util.h"
+#include "webkit/glue/web_intent_data.h"
+#include "webkit/glue/web_intent_service_data.h"
+
+namespace extensions {
+
+// static
+void PlatformAppIntentDispatcher::DispatchIntentData(ExtensionHost* host) {
+ scoped_refptr<PlatformAppIntentDispatcher> dispatcher =
+ new PlatformAppIntentDispatcher(host);
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(&PlatformAppIntentDispatcher::DoDispatchIntentData,
+ dispatcher));
+}
+
+PlatformAppIntentDispatcher::PlatformAppIntentDispatcher(ExtensionHost* host)
+ : host_(host) {}
+
+void PlatformAppIntentDispatcher::DoDispatchIntentData() {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+ // See if we have a command line argument.
+ CommandLine* command_line = CommandLine::ForCurrentProcess();
+ CommandLine::StringVector args = command_line->GetArgs();
+ if (!args.size())
+ return;
+
+ // Check the file exists.
+ FilePath file_path(args[0]);
+ std::string mime_type;
+ if (!file_util::PathExists(file_path))
+ return;
+
+ // Get the MIME type of the file.
+ if (!net::GetMimeTypeFromFile(file_path, &mime_type))
+ return;
+
+ // Find the intent service from the platform app for the file being opened.
+ const string16 open_action = ASCIIToUTF16("http://webintents.org/view");
+ webkit_glue::WebIntentServiceData service;
+ bool found_service = false;
+
+ std::vector<webkit_glue::WebIntentServiceData> services =
+ host_->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 == open_action &&
+ net::MatchesMimeType(service_type_ascii, mime_type)) {
+ service = services[i];
+ found_service = true;
+ }
+ }
+ if (!found_service)
+ return;
+
+ // Give the app permission to use the file:// scheme.
+ using content::ChildProcessSecurityPolicy;
+ ChildProcessSecurityPolicy* policy =
+ ChildProcessSecurityPolicy::GetInstance();
+ DCHECK(policy);
+ int child_id = host_->render_process_host()->GetID();
+ policy->GrantScheme(child_id, chrome::kFileScheme);
+
+ // Give the renderer permission to the given URL.
+ ExtensionAPIPermissionSet apis; // empty
+ URLPatternSet script_hosts; // empty
+ GURL file_url(net::FilePathToFileURL(file_path));
+ URLPattern file_url_pattern(URLPattern::SCHEME_FILE, file_url.spec());
+ URLPatternSet explicit_hosts;
+ explicit_hosts.AddPattern(file_url_pattern);
+ host_->render_process_host()->Send(new ExtensionMsg_UpdatePermissions(
+ static_cast<int>(UpdatedExtensionPermissionsInfo::ADDED),
+ host_->extension()->id(),
+ apis,
+ explicit_hosts,
+ script_hosts));
+
+ // Dispatch the intent data to the renderer.
+ string16 mime_type_utf16 = ASCIIToUTF16(mime_type);
+ webkit_glue::WebIntentData intent_data(open_action, mime_type_utf16,
+ ASCIIToUTF16(file_url.spec()));
+ DirectDispatchIntent(intent_data, host_->render_view_host());
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698