| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/sessions/session_common_utils.h" | 5 #include "chrome/browser/sessions/session_common_utils.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "build/build_config.h" |
| 10 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "components/sessions/core/serialized_navigation_driver.h" |
| 11 #include "components/sessions/core/session_types.h" | 13 #include "components/sessions/core/session_types.h" |
| 14 #include "content/public/common/referrer.h" |
| 12 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 13 | 16 |
| 17 #if defined(OS_ANDROID) |
| 18 #include "content/public/common/content_features.h" |
| 19 #include "content/public/common/page_state.h" |
| 20 #endif |
| 21 |
| 22 namespace { |
| 23 |
| 24 bool IsUberOrUberReplacementURL(const GURL& url) { |
| 25 return url.SchemeIs(content::kChromeUIScheme) && |
| 26 (url.host_piece() == chrome::kChromeUIHistoryHost || |
| 27 url.host_piece() == chrome::kChromeUIUberHost); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 14 bool ShouldTrackURLForRestore(const GURL& url) { | 32 bool ShouldTrackURLForRestore(const GURL& url) { |
| 15 return url.is_valid() && | 33 return url.is_valid() && |
| 16 !(url.SchemeIs(content::kChromeUIScheme) && | 34 !(url.SchemeIs(content::kChromeUIScheme) && |
| 17 (url.host_piece() == chrome::kChromeUIQuitHost || | 35 (url.host_piece() == chrome::kChromeUIQuitHost || |
| 18 url.host_piece() == chrome::kChromeUIRestartHost)); | 36 url.host_piece() == chrome::kChromeUIRestartHost)); |
| 19 } | 37 } |
| 20 | 38 |
| 21 int GetNavigationIndexToSelect(const sessions::SessionTab& tab) { | 39 int GetNavigationIndexToSelect(const sessions::SessionTab& tab) { |
| 22 DCHECK(!tab.navigations.empty()); | 40 DCHECK(!tab.navigations.empty()); |
| 23 const int selected_index = | 41 const int selected_index = |
| 24 std::max(0, std::min(tab.current_navigation_index, | 42 std::max(0, std::min(tab.current_navigation_index, |
| 25 static_cast<int>(tab.navigations.size() - 1))); | 43 static_cast<int>(tab.navigations.size() - 1))); |
| 26 | 44 |
| 27 // After user sign out, Chrome may navigate to the setting page from the | 45 // After user sign out, Chrome may navigate to the setting page from the |
| 28 // sign out page asynchronously. The browser may be closed before the | 46 // sign out page asynchronously. The browser may be closed before the |
| 29 // navigation callback finished. | 47 // navigation callback finished. |
| 30 std::string setting_page_url = std::string(chrome::kChromeUISettingsURL); | 48 std::string setting_page_url = std::string(chrome::kChromeUISettingsURL); |
| 31 std::string sign_out_page_url = | 49 std::string sign_out_page_url = |
| 32 setting_page_url + std::string(chrome::kSignOutSubPage); | 50 setting_page_url + std::string(chrome::kSignOutSubPage); |
| 33 if (selected_index > 0 && | 51 if (selected_index > 0 && |
| 34 tab.navigations[selected_index].virtual_url().spec() == | 52 tab.navigations[selected_index].virtual_url().spec() == |
| 35 sign_out_page_url && | 53 sign_out_page_url && |
| 36 tab.navigations[selected_index - 1].virtual_url().spec() == | 54 tab.navigations[selected_index - 1].virtual_url().spec() == |
| 37 setting_page_url) { | 55 setting_page_url) { |
| 38 return selected_index - 1; | 56 return selected_index - 1; |
| 39 } | 57 } |
| 40 | 58 |
| 41 return selected_index; | 59 return selected_index; |
| 42 } | 60 } |
| 61 |
| 62 void SanitizeNavigation(sessions::SerializedNavigationEntry* navigation) { |
| 63 content::Referrer old_referrer( |
| 64 navigation->referrer_url(), |
| 65 static_cast<blink::WebReferrerPolicy>(navigation->referrer_policy())); |
| 66 content::Referrer new_referrer = content::Referrer::SanitizeForRequest( |
| 67 navigation->virtual_url(), old_referrer); |
| 68 |
| 69 // Clear any Uber UI page state so that these pages are reloaded rather than |
| 70 // restored from page state. This fixes session restore when WebUI URLs |
| 71 // change. |
| 72 if (IsUberOrUberReplacementURL(navigation->virtual_url()) && |
| 73 IsUberOrUberReplacementURL(navigation->original_request_url())) { |
| 74 navigation->set_encoded_page_state(std::string()); |
| 75 } |
| 76 |
| 77 // No need to compare the policy, as it doesn't change during |
| 78 // sanitization. If there has been a change, the referrer needs to be |
| 79 // stripped from the page state as well. |
| 80 if (navigation->referrer_url() != new_referrer.url) { |
| 81 auto* driver = sessions::SerializedNavigationDriver::Get(); |
| 82 navigation->set_referrer_url(GURL()); |
| 83 navigation->set_referrer_policy(driver->GetDefaultReferrerPolicy()); |
| 84 navigation->set_encoded_page_state( |
| 85 driver->StripReferrerFromPageState(navigation->encoded_page_state())); |
| 86 } |
| 87 |
| 88 #if defined(OS_ANDROID) |
| 89 // Rewrite the old new tab and welcome page URLs to the new NTP URL. |
| 90 if (navigation->virtual_url().SchemeIs(content::kChromeUIScheme) && |
| 91 (navigation->virtual_url().host_piece() == chrome::kChromeUIWelcomeHost || |
| 92 navigation->virtual_url().host_piece() == chrome::kChromeUINewTabHost)) { |
| 93 navigation->set_virtual_url(GURL(chrome::kChromeUINativeNewTabURL)); |
| 94 navigation->set_original_request_url(navigation->virtual_url()); |
| 95 navigation->set_encoded_page_state( |
| 96 content::PageState::CreateFromURL(navigation->virtual_url()) |
| 97 .ToEncodedData()); |
| 98 } |
| 99 |
| 100 if (base::FeatureList::IsEnabled(features::kNativeAndroidHistoryManager) && |
| 101 navigation->virtual_url().SchemeIs(content::kChromeUIScheme) && |
| 102 (navigation->virtual_url().host_piece() == chrome::kChromeUIHistoryHost || |
| 103 navigation->virtual_url().host_piece() == |
| 104 chrome::kChromeUIHistoryFrameHost)) { |
| 105 // Rewrite the old history Web UI to the new android native history. |
| 106 navigation->set_virtual_url(GURL(chrome::kChromeUINativeHistoryURL)); |
| 107 navigation->set_original_request_url(navigation->virtual_url()); |
| 108 navigation->set_encoded_page_state( |
| 109 content::PageState::CreateFromURL(navigation->virtual_url()) |
| 110 .ToEncodedData()); |
| 111 } else if (!base::FeatureList::IsEnabled( |
| 112 features::kNativeAndroidHistoryManager) && |
| 113 navigation->virtual_url().SchemeIs( |
| 114 chrome::kChromeUINativeScheme) && |
| 115 navigation->virtual_url().host_piece() == |
| 116 chrome::kChromeUIHistoryHost) { |
| 117 // If the android native history UI has been disabled, redirect |
| 118 // chrome-native://history to the old web UI. |
| 119 navigation->set_virtual_url(GURL(chrome::kChromeUIHistoryURL)); |
| 120 navigation->set_original_request_url(navigation->virtual_url()); |
| 121 navigation->set_encoded_page_state(std::string()); |
| 122 } |
| 123 #endif // defined(OS_ANDROID) |
| 124 } |
| OLD | NEW |