| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/sessions/chrome_serialized_navigation_driver.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "build/build_config.h" |
| 9 #include "chrome/common/url_constants.h" |
| 10 #include "components/sessions/core/serialized_navigation_entry.h" |
| 11 #include "content/public/common/referrer.h" |
| 12 |
| 13 #if defined(OS_ANDROID) |
| 14 #include "content/public/common/content_features.h" |
| 15 #include "content/public/common/page_state.h" |
| 16 #endif |
| 17 |
| 18 namespace { |
| 19 |
| 20 bool IsUberOrUberReplacementURL(const GURL& url) { |
| 21 return url.SchemeIs(content::kChromeUIScheme) && |
| 22 (url.host_piece() == chrome::kChromeUIHistoryHost || |
| 23 url.host_piece() == chrome::kChromeUIUberHost); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 ChromeSerializedNavigationDriver::~ChromeSerializedNavigationDriver() {} |
| 29 |
| 30 // static |
| 31 ChromeSerializedNavigationDriver* |
| 32 ChromeSerializedNavigationDriver::GetInstance() { |
| 33 return base::Singleton< |
| 34 ChromeSerializedNavigationDriver, |
| 35 base::LeakySingletonTraits<ChromeSerializedNavigationDriver>>::get(); |
| 36 } |
| 37 |
| 38 void ChromeSerializedNavigationDriver::Sanitize( |
| 39 sessions::SerializedNavigationEntry* navigation) const { |
| 40 content::Referrer old_referrer( |
| 41 navigation->referrer_url(), |
| 42 static_cast<blink::WebReferrerPolicy>(navigation->referrer_policy())); |
| 43 content::Referrer new_referrer = content::Referrer::SanitizeForRequest( |
| 44 navigation->virtual_url(), old_referrer); |
| 45 |
| 46 // Clear any Uber UI page state so that these pages are reloaded rather than |
| 47 // restored from page state. This fixes session restore when WebUI URLs |
| 48 // change. |
| 49 if (IsUberOrUberReplacementURL(navigation->virtual_url()) && |
| 50 IsUberOrUberReplacementURL(navigation->original_request_url())) { |
| 51 navigation->set_encoded_page_state(std::string()); |
| 52 } |
| 53 |
| 54 // No need to compare the policy, as it doesn't change during |
| 55 // sanitization. If there has been a change, the referrer needs to be |
| 56 // stripped from the page state as well. |
| 57 if (navigation->referrer_url() != new_referrer.url) { |
| 58 auto* driver = sessions::SerializedNavigationDriver::Get(); |
| 59 navigation->set_referrer_url(GURL()); |
| 60 navigation->set_referrer_policy(driver->GetDefaultReferrerPolicy()); |
| 61 navigation->set_encoded_page_state( |
| 62 driver->StripReferrerFromPageState(navigation->encoded_page_state())); |
| 63 } |
| 64 |
| 65 #if defined(OS_ANDROID) |
| 66 // Rewrite the old new tab and welcome page URLs to the new NTP URL. |
| 67 if (navigation->virtual_url().SchemeIs(content::kChromeUIScheme) && |
| 68 (navigation->virtual_url().host_piece() == chrome::kChromeUIWelcomeHost || |
| 69 navigation->virtual_url().host_piece() == chrome::kChromeUINewTabHost)) { |
| 70 navigation->set_virtual_url(GURL(chrome::kChromeUINativeNewTabURL)); |
| 71 navigation->set_original_request_url(navigation->virtual_url()); |
| 72 navigation->set_encoded_page_state( |
| 73 content::PageState::CreateFromURL(navigation->virtual_url()) |
| 74 .ToEncodedData()); |
| 75 } |
| 76 |
| 77 if (base::FeatureList::IsEnabled(features::kNativeAndroidHistoryManager) && |
| 78 navigation->virtual_url().SchemeIs(content::kChromeUIScheme) && |
| 79 (navigation->virtual_url().host_piece() == chrome::kChromeUIHistoryHost || |
| 80 navigation->virtual_url().host_piece() == |
| 81 chrome::kChromeUIHistoryFrameHost)) { |
| 82 // Rewrite the old history Web UI to the new android native history. |
| 83 navigation->set_virtual_url(GURL(chrome::kChromeUINativeHistoryURL)); |
| 84 navigation->set_original_request_url(navigation->virtual_url()); |
| 85 navigation->set_encoded_page_state( |
| 86 content::PageState::CreateFromURL(navigation->virtual_url()) |
| 87 .ToEncodedData()); |
| 88 } else if (!base::FeatureList::IsEnabled( |
| 89 features::kNativeAndroidHistoryManager) && |
| 90 navigation->virtual_url().SchemeIs( |
| 91 chrome::kChromeUINativeScheme) && |
| 92 navigation->virtual_url().host_piece() == |
| 93 chrome::kChromeUIHistoryHost) { |
| 94 // If the android native history UI has been disabled, redirect |
| 95 // chrome-native://history to the old web UI. |
| 96 navigation->set_virtual_url(GURL(chrome::kChromeUIHistoryURL)); |
| 97 navigation->set_original_request_url(navigation->virtual_url()); |
| 98 navigation->set_encoded_page_state(std::string()); |
| 99 } |
| 100 #endif // defined(OS_ANDROID) |
| 101 } |
| 102 |
| 103 ChromeSerializedNavigationDriver::ChromeSerializedNavigationDriver() {} |
| OLD | NEW |