| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.
h" | 5 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.
h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/guid.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/component_updater/component_updater_service.h" | 14 #include "chrome/browser/component_updater/component_updater_service.h" |
| 14 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h" | 15 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h" |
| 15 #include "chrome/browser/content_settings/host_content_settings_map.h" | 16 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 16 #include "chrome/browser/download/download_request_limiter.h" | 17 #include "chrome/browser/download/download_request_limiter.h" |
| 17 #include "chrome/browser/download/download_resource_throttle.h" | 18 #include "chrome/browser/download/download_resource_throttle.h" |
| 18 #include "chrome/browser/extensions/api/streams_private/streams_private_api.h" | 19 #include "chrome/browser/extensions/api/streams_private/streams_private_api.h" |
| 19 #include "chrome/browser/extensions/extension_renderer_state.h" | 20 #include "chrome/browser/extensions/extension_renderer_state.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 if (prerender_contents) | 149 if (prerender_contents) |
| 149 prerender_contents->AddNetworkBytes(bytes); | 150 prerender_contents->AddNetworkBytes(bytes); |
| 150 | 151 |
| 151 prerender::PrerenderManager* prerender_manager = | 152 prerender::PrerenderManager* prerender_manager = |
| 152 GetPrerenderManager(render_process_id, render_view_id); | 153 GetPrerenderManager(render_process_id, render_view_id); |
| 153 if (prerender_manager) | 154 if (prerender_manager) |
| 154 prerender_manager->AddProfileNetworkBytesIfEnabled(bytes); | 155 prerender_manager->AddProfileNetworkBytesIfEnabled(bytes); |
| 155 } | 156 } |
| 156 | 157 |
| 157 #if !defined(OS_ANDROID) | 158 #if !defined(OS_ANDROID) |
| 158 // Goes through the extension's file browser handlers and checks if there is one | |
| 159 // that can handle the |mime_type|. | |
| 160 // |extension| must not be NULL. | |
| 161 bool ExtensionCanHandleMimeType(const Extension* extension, | |
| 162 const std::string& mime_type) { | |
| 163 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension); | |
| 164 if (!handler) | |
| 165 return false; | |
| 166 | |
| 167 return handler->CanHandleMIMEType(mime_type); | |
| 168 } | |
| 169 | |
| 170 void SendExecuteMimeTypeHandlerEvent(scoped_ptr<content::StreamHandle> stream, | 159 void SendExecuteMimeTypeHandlerEvent(scoped_ptr<content::StreamHandle> stream, |
| 171 int64 expected_content_size, | 160 int64 expected_content_size, |
| 172 int render_process_id, | 161 int render_process_id, |
| 173 int render_view_id, | 162 int render_view_id, |
| 174 const std::string& extension_id) { | 163 const std::string& extension_id, |
| 164 const std::string& view_id) { |
| 175 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 165 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 176 | 166 |
| 167 content::RenderViewHost* render_view_host = |
| 168 content::RenderViewHost::FromID(render_process_id, render_view_id); |
| 169 if (!render_view_host) |
| 170 return; |
| 171 |
| 177 content::WebContents* web_contents = | 172 content::WebContents* web_contents = |
| 178 tab_util::GetWebContentsByID(render_process_id, render_view_id); | 173 tab_util::GetWebContentsByID(render_process_id, render_view_id); |
| 179 if (!web_contents) | 174 if (!web_contents) |
| 180 return; | 175 return; |
| 181 | 176 |
| 182 // If the request was for a prerender, abort the prerender and do not | 177 // If the request was for a prerender, abort the prerender and do not |
| 183 // continue. | 178 // continue. |
| 184 prerender::PrerenderContents* prerender_contents = | 179 prerender::PrerenderContents* prerender_contents = |
| 185 prerender::PrerenderContents::FromWebContents(web_contents); | 180 prerender::PrerenderContents::FromWebContents(web_contents); |
| 186 if (prerender_contents) { | 181 if (prerender_contents) { |
| 187 prerender_contents->Destroy(prerender::FINAL_STATUS_DOWNLOAD); | 182 prerender_contents->Destroy(prerender::FINAL_STATUS_DOWNLOAD); |
| 188 return; | 183 return; |
| 189 } | 184 } |
| 190 | 185 |
| 191 Profile* profile = | 186 Profile* profile = |
| 192 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 187 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 193 | 188 |
| 194 StreamsPrivateAPI* streams_private = StreamsPrivateAPI::Get(profile); | 189 StreamsPrivateAPI* streams_private = StreamsPrivateAPI::Get(profile); |
| 195 if (!streams_private) | 190 if (!streams_private) |
| 196 return; | 191 return; |
| 197 streams_private->ExecuteMimeTypeHandler( | 192 streams_private->ExecuteMimeTypeHandler( |
| 198 extension_id, web_contents, stream.Pass(), expected_content_size); | 193 extension_id, web_contents, stream.Pass(), view_id, |
| 194 expected_content_size); |
| 199 } | 195 } |
| 200 | 196 |
| 201 void LaunchURL(const GURL& url, int render_process_id, int render_view_id, | 197 void LaunchURL(const GURL& url, int render_process_id, int render_view_id, |
| 202 bool user_gesture) { | 198 bool user_gesture) { |
| 203 // If there is no longer a WebContents, the request may have raced with tab | 199 // If there is no longer a WebContents, the request may have raced with tab |
| 204 // closing. Don't fire the external request. (It may have been a prerender.) | 200 // closing. Don't fire the external request. (It may have been a prerender.) |
| 205 content::WebContents* web_contents = | 201 content::WebContents* web_contents = |
| 206 tab_util::GetWebContentsByID(render_process_id, render_view_id); | 202 tab_util::GetWebContentsByID(render_process_id, render_view_id); |
| 207 if (!web_contents) | 203 if (!web_contents) |
| 208 return; | 204 return; |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 const GURL& url, const std::string& mime_type) { | 534 const GURL& url, const std::string& mime_type) { |
| 539 // Special-case user scripts to get downloaded instead of viewed. | 535 // Special-case user scripts to get downloaded instead of viewed. |
| 540 return extensions::UserScript::IsURLUserScript(url, mime_type); | 536 return extensions::UserScript::IsURLUserScript(url, mime_type); |
| 541 } | 537 } |
| 542 | 538 |
| 543 bool ChromeResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream( | 539 bool ChromeResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream( |
| 544 content::ResourceContext* resource_context, | 540 content::ResourceContext* resource_context, |
| 545 const GURL& url, | 541 const GURL& url, |
| 546 const std::string& mime_type, | 542 const std::string& mime_type, |
| 547 GURL* origin, | 543 GURL* origin, |
| 548 std::string* target_id) { | 544 std::string* target_id, |
| 545 std::string* payload, |
| 546 std::string* view_id) { |
| 549 #if !defined(OS_ANDROID) | 547 #if !defined(OS_ANDROID) |
| 550 ProfileIOData* io_data = | 548 ProfileIOData* io_data = |
| 551 ProfileIOData::FromResourceContext(resource_context); | 549 ProfileIOData::FromResourceContext(resource_context); |
| 552 bool profile_is_off_the_record = io_data->IsOffTheRecord(); | 550 bool profile_is_off_the_record = io_data->IsOffTheRecord(); |
| 553 const scoped_refptr<const extensions::InfoMap> extension_info_map( | 551 const scoped_refptr<const extensions::InfoMap> extension_info_map( |
| 554 io_data->GetExtensionInfoMap()); | 552 io_data->GetExtensionInfoMap()); |
| 555 std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist(); | 553 std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist(); |
| 556 // Go through the white-listed extensions and try to use them to intercept | 554 // Go through the white-listed extensions and try to use them to intercept |
| 557 // the URL request. | 555 // the URL request. |
| 558 for (size_t i = 0; i < whitelist.size(); ++i) { | 556 for (size_t i = 0; i < whitelist.size(); ++i) { |
| 559 const char* extension_id = whitelist[i].c_str(); | 557 const char* extension_id = whitelist[i].c_str(); |
| 560 const Extension* extension = | 558 const Extension* extension = |
| 561 extension_info_map->extensions().GetByID(extension_id); | 559 extension_info_map->extensions().GetByID(extension_id); |
| 562 // The white-listed extension may not be installed, so we have to NULL check | 560 // The white-listed extension may not be installed, so we have to NULL check |
| 563 // |extension|. | 561 // |extension|. |
| 564 if (!extension || | 562 if (!extension || |
| 565 (profile_is_off_the_record && | 563 (profile_is_off_the_record && |
| 566 !extension_info_map->IsIncognitoEnabled(extension_id))) { | 564 !extension_info_map->IsIncognitoEnabled(extension_id))) { |
| 567 continue; | 565 continue; |
| 568 } | 566 } |
| 569 | 567 |
| 570 if (ExtensionCanHandleMimeType(extension, mime_type)) { | 568 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension); |
| 569 if (handler && handler->CanHandleMIMEType(mime_type)) { |
| 571 *origin = Extension::GetBaseURLFromExtensionId(extension_id); | 570 *origin = Extension::GetBaseURLFromExtensionId(extension_id); |
| 572 *target_id = extension_id; | 571 *target_id = extension_id; |
| 572 if (!handler->handler_url().empty()) { |
| 573 *view_id = base::GenerateGUID(); |
| 574 *payload = origin->spec() + handler->handler_url() + "?id=" + *view_id; |
| 575 } |
| 573 return true; | 576 return true; |
| 574 } | 577 } |
| 575 } | 578 } |
| 576 #endif | 579 #endif |
| 577 return false; | 580 return false; |
| 578 } | 581 } |
| 579 | 582 |
| 580 void ChromeResourceDispatcherHostDelegate::OnStreamCreated( | 583 void ChromeResourceDispatcherHostDelegate::OnStreamCreated( |
| 581 content::ResourceContext* resource_context, | 584 content::ResourceContext* resource_context, |
| 582 int render_process_id, | 585 int render_process_id, |
| 583 int render_view_id, | 586 int render_view_id, |
| 584 const std::string& target_id, | 587 const std::string& target_id, |
| 588 const std::string& view_id, |
| 585 scoped_ptr<content::StreamHandle> stream, | 589 scoped_ptr<content::StreamHandle> stream, |
| 586 int64 expected_content_size) { | 590 int64 expected_content_size) { |
| 587 #if !defined(OS_ANDROID) | 591 #if !defined(OS_ANDROID) |
| 588 content::BrowserThread::PostTask( | 592 content::BrowserThread::PostTask( |
| 589 content::BrowserThread::UI, FROM_HERE, | 593 content::BrowserThread::UI, FROM_HERE, |
| 590 base::Bind(&SendExecuteMimeTypeHandlerEvent, base::Passed(&stream), | 594 base::Bind(&SendExecuteMimeTypeHandlerEvent, base::Passed(&stream), |
| 591 expected_content_size, render_process_id, render_view_id, | 595 expected_content_size, render_process_id, render_view_id, |
| 592 target_id)); | 596 target_id, view_id)); |
| 593 #endif | 597 #endif |
| 594 } | 598 } |
| 595 | 599 |
| 596 void ChromeResourceDispatcherHostDelegate::OnResponseStarted( | 600 void ChromeResourceDispatcherHostDelegate::OnResponseStarted( |
| 597 net::URLRequest* request, | 601 net::URLRequest* request, |
| 598 content::ResourceContext* resource_context, | 602 content::ResourceContext* resource_context, |
| 599 content::ResourceResponse* response, | 603 content::ResourceResponse* response, |
| 600 IPC::Sender* sender) { | 604 IPC::Sender* sender) { |
| 601 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | 605 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 602 | 606 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 url_request->GetTotalReceivedBytes())); | 700 url_request->GetTotalReceivedBytes())); |
| 697 } | 701 } |
| 698 } | 702 } |
| 699 | 703 |
| 700 // static | 704 // static |
| 701 void ChromeResourceDispatcherHostDelegate:: | 705 void ChromeResourceDispatcherHostDelegate:: |
| 702 SetExternalProtocolHandlerDelegateForTesting( | 706 SetExternalProtocolHandlerDelegateForTesting( |
| 703 ExternalProtocolHandler::Delegate* delegate) { | 707 ExternalProtocolHandler::Delegate* delegate) { |
| 704 g_external_protocol_handler_delegate = delegate; | 708 g_external_protocol_handler_delegate = delegate; |
| 705 } | 709 } |
| OLD | NEW |