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

Side by Side 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 unified diff | Download patch
OLDNEW
(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_intent_dispatcher.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/file_path.h"
10 #include "base/file_util.h"
11 #include "base/string16.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/extensions/extension_host.h"
14 #include "chrome/common/extensions/extension_messages.h"
15 #include "chrome/common/extensions/url_pattern.h"
16 #include "chrome/common/extensions/url_pattern_set.h"
17 #include "content/public/browser/child_process_security_policy.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/web_intents_dispatcher.h"
21 #include "content/public/common/url_constants.h"
22 #include "googleurl/src/gurl.h"
23 #include "net/base/mime_util.h"
24 #include "net/base/net_util.h"
25 #include "webkit/glue/web_intent_data.h"
26 #include "webkit/glue/web_intent_service_data.h"
27
28 namespace extensions {
29
30 // static
31 void PlatformAppIntentDispatcher::DispatchIntentData(ExtensionHost* host) {
32 scoped_refptr<PlatformAppIntentDispatcher> dispatcher =
33 new PlatformAppIntentDispatcher(host);
34 content::BrowserThread::PostTask(
35 content::BrowserThread::FILE,
36 FROM_HERE,
37 base::Bind(&PlatformAppIntentDispatcher::DoDispatchIntentData,
38 dispatcher));
39 }
40
41 PlatformAppIntentDispatcher::PlatformAppIntentDispatcher(ExtensionHost* host)
42 : host_(host) {}
43
44 void PlatformAppIntentDispatcher::DoDispatchIntentData() {
45 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
46 // See if we have a command line argument.
47 CommandLine* command_line = CommandLine::ForCurrentProcess();
48 CommandLine::StringVector args = command_line->GetArgs();
49 if (!args.size())
50 return;
51
52 // Check the file exists.
53 FilePath file_path(args[0]);
54 std::string mime_type;
55 if (!file_util::PathExists(file_path))
56 return;
57
58 // Get the MIME type of the file.
59 if (!net::GetMimeTypeFromFile(file_path, &mime_type))
60 return;
61
62 // Find the intent service from the platform app for the file being opened.
63 const string16 open_action = ASCIIToUTF16("http://webintents.org/view");
64 webkit_glue::WebIntentServiceData service;
65 bool found_service = false;
66
67 std::vector<webkit_glue::WebIntentServiceData> services =
68 host_->extension()->intents_services();
69 for (size_t i = 0; i < services.size(); i++) {
70 std::string service_type_ascii = UTF16ToASCII(services[i].type);
71 if (services[i].action == open_action &&
72 net::MatchesMimeType(service_type_ascii, mime_type)) {
73 service = services[i];
74 found_service = true;
75 }
76 }
77 if (!found_service)
78 return;
79
80 // Give the app permission to use the file:// scheme.
81 using content::ChildProcessSecurityPolicy;
82 ChildProcessSecurityPolicy* policy =
83 ChildProcessSecurityPolicy::GetInstance();
84 DCHECK(policy);
85 int child_id = host_->render_process_host()->GetID();
86 policy->GrantScheme(child_id, chrome::kFileScheme);
87
88 // Give the renderer permission to the given URL.
89 ExtensionAPIPermissionSet apis; // empty
90 URLPatternSet script_hosts; // empty
91 GURL file_url(net::FilePathToFileURL(file_path));
92 URLPattern file_url_pattern(URLPattern::SCHEME_FILE, file_url.spec());
93 URLPatternSet explicit_hosts;
94 explicit_hosts.AddPattern(file_url_pattern);
95 host_->render_process_host()->Send(new ExtensionMsg_UpdatePermissions(
96 static_cast<int>(UpdatedExtensionPermissionsInfo::ADDED),
97 host_->extension()->id(),
98 apis,
99 explicit_hosts,
100 script_hosts));
101
102 // Dispatch the intent data to the renderer.
103 string16 mime_type_utf16 = ASCIIToUTF16(mime_type);
104 webkit_glue::WebIntentData intent_data(open_action, mime_type_utf16,
105 ASCIIToUTF16(file_url.spec()));
106 DirectDispatchIntent(intent_data, host_->render_view_host());
107 }
108
109 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698