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