Chromium Code Reviews| 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..eee86e5a7b03b20299daad8a5e94cf0b7fc1aa5f |
| --- /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/threading/thread_restrictions.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" |
|
James Hawkins
2012/03/26 19:55:57
nit: Alphabetize includes.
benwells
2012/03/27 04:24:12
Done.
|
| +#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); |
| + dispatcher->DoDispatchIntentData(); |
| +} |
| + |
| +PlatformAppIntentDispatcher::PlatformAppIntentDispatcher(ExtensionHost* host) |
| + : host_(host) {} |
| + |
| +void PlatformAppIntentDispatcher::DoDispatchIntentData() { |
| + // See if we have a command line argument. |
|
James Hawkins
2012/03/26 19:55:57
nit: Don't use 'we' (or pronouns in general) in co
benwells
2012/03/27 04:24:12
Done.
|
| + CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| + CommandLine::StringVector args = command_line->GetArgs(); |
| + if (!args.size()) |
| + return; |
| + |
| + FilePath file_path(args[0]); |
| + std::string mime_type; |
| + { |
| + // TODO(benwells): Remove this ScopedAllowIO. This should be done by |
| + // delaying the creation of the render view until after the MIME type has |
| + // been retrieved on the FILE thread. This should be done once the platform |
| + // app startup and windowing model has settled down. |
| + base::ThreadRestrictions::ScopedAllowIO allow; |
| + |
| + // 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"); |
|
James Hawkins
2012/03/26 19:55:57
s/open_action/kOpenAction/ for const variable.
benwells
2012/03/27 04:24:12
Done.
|
| + 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 |
|
James Hawkins
2012/03/26 19:55:57
nit: Two spaces before start of comments.
Though
benwells
2012/03/27 04:24:12
Done (comment removed).
|
| + 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(), |
|
James Hawkins
2012/03/26 19:55:57
Optional nit: Can save lines by moving parameters
benwells
2012/03/27 04:24:12
Done.
|
| + 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 |