| 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_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/threading/thread_restrictions.h" |
| 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/extensions/extension_host.h" |
| 15 #include "chrome/common/extensions/extension_messages.h" |
| 16 #include "chrome/common/extensions/url_pattern.h" |
| 17 #include "chrome/common/extensions/url_pattern_set.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/child_process_security_policy.h" |
| 20 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/web_intents_dispatcher.h" |
| 22 #include "content/public/common/url_constants.h" |
| 23 #include "googleurl/src/gurl.h" |
| 24 #include "net/base/mime_util.h" |
| 25 #include "net/base/net_util.h" |
| 26 #include "webkit/glue/web_intent_data.h" |
| 27 #include "webkit/glue/web_intent_service_data.h" |
| 28 |
| 29 namespace extensions { |
| 30 |
| 31 void DispatchPlatformAppIntentData(ExtensionHost* host) { |
| 32 // See if there is a command line argument. |
| 33 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 34 CommandLine::StringVector args = command_line->GetArgs(); |
| 35 if (!args.size()) |
| 36 return; |
| 37 |
| 38 FilePath file_path(args[0]); |
| 39 std::string mime_type; |
| 40 { |
| 41 // TODO(benwells): Remove this ScopedAllowIO. This should be done by |
| 42 // delaying the creation of the render view until after the MIME type has |
| 43 // been retrieved on the FILE thread. This should be done once the platform |
| 44 // app startup and windowing model has settled down. |
| 45 base::ThreadRestrictions::ScopedAllowIO allow; |
| 46 |
| 47 // Get the MIME type of the file. |
| 48 if (!net::GetMimeTypeFromFile(file_path, &mime_type)) |
| 49 return; |
| 50 } |
| 51 |
| 52 // Find the intent service from the platform app for the file being opened. |
| 53 const string16 kViewAction = ASCIIToUTF16("http://webintents.org/view"); |
| 54 webkit_glue::WebIntentServiceData service; |
| 55 bool found_service = false; |
| 56 |
| 57 std::vector<webkit_glue::WebIntentServiceData> services = |
| 58 host->extension()->intents_services(); |
| 59 for (size_t i = 0; i < services.size(); i++) { |
| 60 std::string service_type_ascii = UTF16ToASCII(services[i].type); |
| 61 if (services[i].action == kViewAction && |
| 62 net::MatchesMimeType(service_type_ascii, mime_type)) { |
| 63 service = services[i]; |
| 64 found_service = true; |
| 65 } |
| 66 } |
| 67 if (!found_service) |
| 68 return; |
| 69 |
| 70 // Give the app permission to use the file:// scheme. This alone gives the |
| 71 // browser process permission to handle URLs of this scheme, but does not |
| 72 // change any cross origin security policies. |
| 73 using content::ChildProcessSecurityPolicy; |
| 74 ChildProcessSecurityPolicy* policy = |
| 75 ChildProcessSecurityPolicy::GetInstance(); |
| 76 DCHECK(policy); |
| 77 int child_id = host->render_process_host()->GetID(); |
| 78 policy->GrantScheme(child_id, chrome::kFileScheme); |
| 79 |
| 80 // Give the renderer permission to the given URL. This updates the renderer |
| 81 // process's cross origin policy to allow the access to this particular file |
| 82 // URL, but only from the platform app extension page. |
| 83 ExtensionAPIPermissionSet apis; |
| 84 URLPatternSet script_hosts; |
| 85 GURL file_url(net::FilePathToFileURL(file_path)); |
| 86 URLPattern file_url_pattern(URLPattern::SCHEME_FILE, file_url.spec()); |
| 87 URLPatternSet explicit_hosts; |
| 88 explicit_hosts.AddPattern(file_url_pattern); |
| 89 host->render_process_host()->Send(new ExtensionMsg_UpdatePermissions( |
| 90 static_cast<int>(UpdatedExtensionPermissionsInfo::ADDED), |
| 91 host->extension()->id(), apis, explicit_hosts, script_hosts)); |
| 92 |
| 93 // Dispatch the intent data to the renderer. |
| 94 string16 mime_type_utf16 = ASCIIToUTF16(mime_type); |
| 95 webkit_glue::WebIntentData intent_data(kViewAction, mime_type_utf16, |
| 96 ASCIIToUTF16(file_url.spec())); |
| 97 DirectDispatchIntent(intent_data, host->render_view_host()); |
| 98 } |
| 99 |
| 100 } // namespace extensions |
| OLD | NEW |