Chromium Code Reviews| 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/prerender/prerender_manager.h" | 5 #include "chrome/browser/prerender/prerender_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <functional> | 10 #include <functional> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <utility> | 12 #include <utility> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
| 17 #include "base/location.h" | 17 #include "base/location.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 21 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
| 22 #include "base/metrics/histogram_macros.h" | 22 #include "base/metrics/histogram_macros.h" |
| 23 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
| 24 #include "base/strings/string_util.h" | |
| 24 #include "base/sys_info.h" | 25 #include "base/sys_info.h" |
| 25 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
| 26 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 27 #include "base/timer/elapsed_timer.h" | 28 #include "base/timer/elapsed_timer.h" |
| 28 #include "base/values.h" | 29 #include "base/values.h" |
| 29 #include "chrome/browser/chrome_notification_types.h" | 30 #include "chrome/browser/chrome_notification_types.h" |
| 30 #include "chrome/browser/net/prediction_options.h" | 31 #include "chrome/browser/net/prediction_options.h" |
| 31 #include "chrome/browser/prerender/prerender_contents.h" | 32 #include "chrome/browser/prerender/prerender_contents.h" |
| 32 #include "chrome/browser/prerender/prerender_field_trial.h" | 33 #include "chrome/browser/prerender/prerender_field_trial.h" |
| 33 #include "chrome/browser/prerender/prerender_final_status.h" | 34 #include "chrome/browser/prerender/prerender_final_status.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 53 #include "content/public/browser/render_frame_host.h" | 54 #include "content/public/browser/render_frame_host.h" |
| 54 #include "content/public/browser/render_process_host.h" | 55 #include "content/public/browser/render_process_host.h" |
| 55 #include "content/public/browser/render_view_host.h" | 56 #include "content/public/browser/render_view_host.h" |
| 56 #include "content/public/browser/resource_request_details.h" | 57 #include "content/public/browser/resource_request_details.h" |
| 57 #include "content/public/browser/session_storage_namespace.h" | 58 #include "content/public/browser/session_storage_namespace.h" |
| 58 #include "content/public/browser/site_instance.h" | 59 #include "content/public/browser/site_instance.h" |
| 59 #include "content/public/browser/web_contents.h" | 60 #include "content/public/browser/web_contents.h" |
| 60 #include "content/public/browser/web_contents_delegate.h" | 61 #include "content/public/browser/web_contents_delegate.h" |
| 61 #include "content/public/common/url_constants.h" | 62 #include "content/public/common/url_constants.h" |
| 62 #include "extensions/common/constants.h" | 63 #include "extensions/common/constants.h" |
| 64 #include "net/http/http_request_headers.h" | |
| 63 #include "ui/gfx/geometry/rect.h" | 65 #include "ui/gfx/geometry/rect.h" |
| 64 | 66 |
| 65 using content::BrowserThread; | 67 using content::BrowserThread; |
| 66 using content::RenderViewHost; | 68 using content::RenderViewHost; |
| 67 using content::SessionStorageNamespace; | 69 using content::SessionStorageNamespace; |
| 68 using content::WebContents; | 70 using content::WebContents; |
| 69 using namespace chrome_browser_net; | 71 using namespace chrome_browser_net; |
| 70 | 72 |
| 71 namespace prerender { | 73 namespace prerender { |
| 72 | 74 |
| 73 namespace { | 75 namespace { |
| 74 | 76 |
| 75 // Time interval at which periodic cleanups are performed. | 77 // Time interval at which periodic cleanups are performed. |
| 76 const int kPeriodicCleanupIntervalMs = 1000; | 78 const int kPeriodicCleanupIntervalMs = 1000; |
| 77 | 79 |
| 78 // Length of prerender history, for display in chrome://net-internals | 80 // Length of prerender history, for display in chrome://net-internals |
| 79 const int kHistoryLength = 100; | 81 const int kHistoryLength = 100; |
| 80 | 82 |
| 83 // Check if |extra_headers| requested via chrome::NavigateParams::extra_headers | |
| 84 // are the same as what the HTTP server saw when serving prerendered contents. | |
| 85 // PrerenderContents::StartPrerendering doesn't specify any extra headers when | |
| 86 // calling content::NavigationController::LoadURLWithParams, but in reality | |
| 87 // Blink will always add an Upgrade-Insecure-Requests http request header, so | |
| 88 // that HTTP request for prerendered contents always includes this header. | |
| 89 // Because of this, it is okay to show prerendered contents even if | |
| 90 // |extra_headers| contains "Upgrade-Insecure-Requests" header. | |
|
mmenke
2016/10/06 20:52:02
Would it make more sense to just add "Upgrade-Inse
Łukasz Anforowicz
2016/10/06 21:23:34
Okay - let me try to see how this works out. I ga
| |
| 91 bool AreExtraHeadersCompatibleWithPrerenderedContents( | |
| 92 const std::string& extra_headers) { | |
| 93 net::HttpRequestHeaders parsed_headers; | |
| 94 parsed_headers.AddHeadersFromString(extra_headers); | |
| 95 | |
| 96 net::HttpRequestHeaders::Iterator it(parsed_headers); | |
| 97 while (it.GetNext()) { | |
| 98 if (!base::EqualsCaseInsensitiveASCII(it.name(), | |
| 99 "upgrade-insecure-requests")) | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 return true; | |
| 104 } | |
|
mmenke
2016/10/06 20:52:02
Also, should we have a prerender browser test for
Łukasz Anforowicz
2016/10/06 21:23:34
The existing prerender browser tests already exerc
| |
| 105 | |
| 81 } // namespace | 106 } // namespace |
| 82 | 107 |
| 83 class PrerenderManager::OnCloseWebContentsDeleter | 108 class PrerenderManager::OnCloseWebContentsDeleter |
| 84 : public content::WebContentsDelegate, | 109 : public content::WebContentsDelegate, |
| 85 public base::SupportsWeakPtr< | 110 public base::SupportsWeakPtr< |
| 86 PrerenderManager::OnCloseWebContentsDeleter> { | 111 PrerenderManager::OnCloseWebContentsDeleter> { |
| 87 public: | 112 public: |
| 88 OnCloseWebContentsDeleter(PrerenderManager* manager, | 113 OnCloseWebContentsDeleter(PrerenderManager* manager, |
| 89 std::unique_ptr<WebContents> tab) | 114 std::unique_ptr<WebContents> tab) |
| 90 : manager_(manager), tab_(std::move(tab)), suppressed_dialog_(false) { | 115 : manager_(manager), tab_(std::move(tab)), suppressed_dialog_(false) { |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 } | 316 } |
| 292 } | 317 } |
| 293 | 318 |
| 294 bool PrerenderManager::MaybeUsePrerenderedPage(const GURL& url, | 319 bool PrerenderManager::MaybeUsePrerenderedPage(const GURL& url, |
| 295 chrome::NavigateParams* params) { | 320 chrome::NavigateParams* params) { |
| 296 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 321 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 297 | 322 |
| 298 WebContents* web_contents = params->target_contents; | 323 WebContents* web_contents = params->target_contents; |
| 299 DCHECK(!IsWebContentsPrerendering(web_contents, nullptr)); | 324 DCHECK(!IsWebContentsPrerendering(web_contents, nullptr)); |
| 300 | 325 |
| 301 // Don't prerender if the navigation involves some special parameters. | 326 // Don't prerender if the navigation involves some special parameters that |
| 302 if (params->uses_post || !params->extra_headers.empty()) | 327 // are different from what was used by PrerenderContents::StartPrerendering |
| 328 // (which always uses GET method and doesn't specify any extra headers when | |
| 329 // calling content::NavigationController::LoadURLWithParams). | |
| 330 if (params->uses_post || | |
| 331 !AreExtraHeadersCompatibleWithPrerenderedContents(params->extra_headers)) | |
| 303 return false; | 332 return false; |
|
mmenke
2016/10/06 20:52:02
nit: Add braces when if condition takes up more t
Łukasz Anforowicz
2016/10/06 21:23:34
Done.
| |
| 304 | 333 |
| 305 DeleteOldEntries(); | 334 DeleteOldEntries(); |
| 306 to_delete_prerenders_.clear(); | 335 to_delete_prerenders_.clear(); |
| 307 | 336 |
| 308 // First, try to find prerender data with the correct session storage | 337 // First, try to find prerender data with the correct session storage |
| 309 // namespace. | 338 // namespace. |
| 310 // TODO(ajwong): This doesn't handle isolated apps correctly. | 339 // TODO(ajwong): This doesn't handle isolated apps correctly. |
| 311 PrerenderData* prerender_data = FindPrerenderData( | 340 PrerenderData* prerender_data = FindPrerenderData( |
| 312 url, | 341 url, |
| 313 web_contents->GetController().GetDefaultSessionStorageNamespace()); | 342 web_contents->GetController().GetDefaultSessionStorageNamespace()); |
| (...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1343 DCHECK_EQ(1u, erased); | 1372 DCHECK_EQ(1u, erased); |
| 1344 } | 1373 } |
| 1345 | 1374 |
| 1346 void PrerenderManager::SetPrerenderContentsFactoryForTest( | 1375 void PrerenderManager::SetPrerenderContentsFactoryForTest( |
| 1347 PrerenderContents::Factory* prerender_contents_factory) { | 1376 PrerenderContents::Factory* prerender_contents_factory) { |
| 1348 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1377 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1349 prerender_contents_factory_.reset(prerender_contents_factory); | 1378 prerender_contents_factory_.reset(prerender_contents_factory); |
| 1350 } | 1379 } |
| 1351 | 1380 |
| 1352 } // namespace prerender | 1381 } // namespace prerender |
| OLD | NEW |