Chromium Code Reviews| Index: chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc |
| diff --git a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc |
| index 82df13a05fa431bc4733fa35e96608f6ba823222..14a770ca720dd3f9fe087f7731831d1d4eb1957a 100644 |
| --- a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc |
| +++ b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc |
| @@ -14,6 +14,9 @@ |
| #include "chrome/browser/download/download_resource_throttle.h" |
| #include "chrome/browser/download/download_util.h" |
| #include "chrome/browser/extensions/api/streams_private/streams_resource_throttle.h" |
| +#include "chrome/browser/extensions/event_router.h" |
| +#include "chrome/browser/extensions/extension_info_map.h" |
| +#include "chrome/browser/extensions/extension_system.h" |
| #include "chrome/browser/extensions/user_script_listener.h" |
| #include "chrome/browser/external_protocol/external_protocol_handler.h" |
| #include "chrome/browser/google/google_util.h" |
| @@ -23,6 +26,7 @@ |
| #include "chrome/browser/prerender/prerender_manager.h" |
| #include "chrome/browser/prerender/prerender_tracker.h" |
| #include "chrome/browser/prerender/prerender_util.h" |
| +#include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/profiles/profile_io_data.h" |
| #include "chrome/browser/renderer_host/chrome_url_request_user_data.h" |
| #include "chrome/browser/renderer_host/safe_browsing_resource_throttle_factory.h" |
| @@ -32,6 +36,7 @@ |
| #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
| #include "chrome/common/chrome_notification_types.h" |
| #include "chrome/common/extensions/extension_constants.h" |
| +#include "chrome/common/extensions/mime_types_handler.h" |
| #include "chrome/common/extensions/user_script.h" |
| #include "chrome/common/render_messages.h" |
| #include "content/public/browser/browser_thread.h" |
| @@ -40,6 +45,7 @@ |
| #include "content/public/browser/resource_context.h" |
| #include "content/public/browser/resource_dispatcher_host.h" |
| #include "content/public/browser/resource_request_info.h" |
| +#include "extensions/common/constants.h" |
| #include "net/base/load_flags.h" |
| #include "net/base/ssl_config_service.h" |
| #include "net/http/http_response_headers.h" |
| @@ -69,6 +75,9 @@ using content::BrowserThread; |
| using content::RenderViewHost; |
| using content::ResourceDispatcherHostLoginDelegate; |
| using content::ResourceRequestInfo; |
| +using extensions::Event; |
| +using extensions::Extension; |
| +using extensions::ExtensionSystem; |
| namespace { |
| @@ -84,6 +93,66 @@ void NotifyDownloadInitiatedOnUI(int render_process_id, int render_view_id) { |
| content::NotificationService::NoDetails()); |
| } |
| +const char* const kOnExecuteMimeTypeHandlerEvent = |
| + "streamsPrivate.onExecuteMimeTypeHandler"; |
| + |
| +// Goes through the extension's file browser handlers and checks it there is one |
|
kinuko
2013/03/13 11:05:59
nit: it -> if
Zachary Kuznia
2013/03/13 11:13:10
Done.
|
| +// that can handle the |mime_type|. |
| +// |extension| must not be NULL. |
| +bool CanHandleMimeType(const Extension* extension, |
| + const std::string& mime_type) { |
| + MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension); |
| + if (!handler) |
| + return false; |
| + |
| + return handler->CanHandleMIMEType(mime_type); |
| +} |
| + |
| +// Retrieves Profile for a render view host specified by |render_process_id| and |
| +// |render_view_id|. |
| +Profile* GetProfile(int render_process_id, int render_view_id) { |
| + content::RenderViewHost* render_view_host = |
| + content::RenderViewHost::FromID(render_process_id, render_view_id); |
| + if (!render_view_host) |
| + return NULL; |
| + |
| + content::WebContents* web_contents = |
| + content::WebContents::FromRenderViewHost(render_view_host); |
| + if (!web_contents) |
| + return NULL; |
| + |
| + content::BrowserContext* browser_context = web_contents->GetBrowserContext(); |
| + if (!browser_context) |
| + return NULL; |
| + |
| + return Profile::FromBrowserContext(browser_context); |
| +} |
| + |
| +void DispatchEventOnUIThread(const std::string& mime_type, |
| + const GURL& request_url, |
| + const GURL& original_url, |
| + int render_process_id, |
| + int render_view_id, |
| + const std::string& extension_id) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + |
| + Profile* profile = GetProfile(render_process_id, render_view_id); |
| + if (!profile) |
| + return; |
| + |
| + // Create the event's arguments value. |
| + scoped_ptr<ListValue> event_args(new ListValue()); |
| + event_args->Append(new base::StringValue(mime_type)); |
| + event_args->Append(new base::StringValue(original_url.spec())); |
| + event_args->Append(new base::StringValue(request_url.spec())); |
| + |
| + scoped_ptr<Event> event(new Event(kOnExecuteMimeTypeHandlerEvent, |
| + event_args.Pass())); |
| + |
| + ExtensionSystem::Get(profile)->event_router()->DispatchEventToExtension( |
| + extension_id, event.Pass()); |
| +} |
| + |
| } // end namespace |
| ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate( |
| @@ -374,6 +443,80 @@ bool ChromeResourceDispatcherHostDelegate::ShouldForceDownloadResource( |
| return extensions::UserScript::IsURLUserScript(url, mime_type); |
| } |
| +bool ChromeResourceDispatcherHostDelegate::ShouldCreateStream( |
| + content::ResourceContext* resource_context, |
| + const GURL& url, |
| + const std::string& mime_type, |
| + GURL* security_origin) { |
| + ProfileIOData* io_data = |
| + ProfileIOData::FromResourceContext(resource_context); |
| + bool profile_is_incognito = io_data->is_incognito(); |
| + const scoped_refptr<const ExtensionInfoMap> extension_info_map( |
| + io_data->GetExtensionInfoMap()); |
| + std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist(); |
| + // Go through the white-listed extensions and try to use them to intercept |
| + // the URL request. |
| + for (size_t i = 0; i < whitelist.size(); ++i) { |
| + const char* extension_id = whitelist[i].c_str(); |
| + const Extension* extension = |
| + extension_info_map->extensions().GetByID(extension_id); |
| + // The white-listed extension may not be installed, so we have to NULL check |
| + // |extension|. |
| + if (!extension || |
| + (profile_is_incognito && |
| + !extension_info_map->IsIncognitoEnabled(extension_id))) { |
| + continue; |
| + } |
| + |
| + if (CanHandleMimeType(extension, mime_type)) { |
| + *security_origin = Extension::GetBaseURLFromExtensionId(extension_id); |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +void ChromeResourceDispatcherHostDelegate::OnStreamCreated( |
| + content::ResourceContext* resource_context, |
| + int render_process_id, |
| + int render_view_id, |
| + const GURL& stream_url, |
| + const std::string& mime_type, |
| + const GURL& original_url) { |
| + ProfileIOData* io_data = |
| + ProfileIOData::FromResourceContext(resource_context); |
| + bool profile_is_incognito = io_data->is_incognito(); |
| + const scoped_refptr<const ExtensionInfoMap> extension_info_map( |
| + io_data->GetExtensionInfoMap()); |
| + std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist(); |
| + // Go through the white-listed extensions and try to use them to intercept |
| + // the URL request. |
| + for (size_t i = 0; i < whitelist.size(); ++i) { |
| + const char* extension_id = whitelist[i].c_str(); |
| + const Extension* extension = |
| + extension_info_map->extensions().GetByID(extension_id); |
| + // The white-listed extension may not be installed, so we have to NULL check |
| + // |extension|. |
| + if (!extension || |
| + (profile_is_incognito && |
| + !extension_info_map->IsIncognitoEnabled(extension_id))) { |
| + continue; |
| + } |
| + |
| + if (CanHandleMimeType(extension, mime_type)) { |
| + GURL extension_url(Extension::GetBaseURLFromExtensionId(extension_id)); |
| + |
| + // The event must be dispatched on the UI thread. |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&DispatchEventOnUIThread, mime_type, stream_url, |
| + original_url, render_process_id, render_view_id, |
| + extension->id())); |
| + return; |
| + } |
| + } |
| +} |
| + |
| void ChromeResourceDispatcherHostDelegate::OnResponseStarted( |
| net::URLRequest* request, |
| content::ResourceContext* resource_context, |