Port Android relocation packer to chromium build
[chromium-blink-merge.git] / components / sessions / ios / ios_serialized_navigation_driver.cc
blobcbc06a585a8fdccc04a54d8c7c8336890b40b659
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
3 // found in the LICENSE file.
5 #include "components/sessions/ios/ios_serialized_navigation_driver.h"
7 #include "base/memory/singleton.h"
8 #include "components/sessions/serialized_navigation_entry.h"
9 #include "ios/web/public/referrer.h"
11 namespace sessions {
13 namespace {
14 const int kObsoleteReferrerPolicyAlways = 0;
15 const int kObsoleteReferrerPolicyDefault = 1;
16 const int kObsoleteReferrerPolicyNever = 2;
17 const int kObsoleteReferrerPolicyOrigin = 3;
18 } // namespace
20 // static
21 SerializedNavigationDriver* SerializedNavigationDriver::Get() {
22 return IOSSerializedNavigationDriver::GetInstance();
25 // static
26 IOSSerializedNavigationDriver*
27 IOSSerializedNavigationDriver::GetInstance() {
28 return Singleton<IOSSerializedNavigationDriver,
29 LeakySingletonTraits<IOSSerializedNavigationDriver>>::get();
32 IOSSerializedNavigationDriver::IOSSerializedNavigationDriver() {
35 IOSSerializedNavigationDriver::~IOSSerializedNavigationDriver() {
38 int IOSSerializedNavigationDriver::GetDefaultReferrerPolicy() const {
39 return web::ReferrerPolicyDefault;
42 bool IOSSerializedNavigationDriver::MapReferrerPolicyToOldValues(
43 int referrer_policy,
44 int* mapped_referrer_policy) const {
45 switch (referrer_policy) {
46 case web::ReferrerPolicyAlways:
47 case web::ReferrerPolicyDefault:
48 // "always" and "default" are the same value in all versions.
49 *mapped_referrer_policy = referrer_policy;
50 return true;
52 case web::ReferrerPolicyOrigin:
53 // "origin" exists in the old encoding.
54 *mapped_referrer_policy = kObsoleteReferrerPolicyOrigin;
55 return true;
57 default:
58 // Everything else is mapped to never.
59 *mapped_referrer_policy = kObsoleteReferrerPolicyNever;
60 return false;
64 bool IOSSerializedNavigationDriver::MapReferrerPolicyToNewValues(
65 int referrer_policy,
66 int* mapped_referrer_policy) const {
67 switch (referrer_policy) {
68 case kObsoleteReferrerPolicyAlways:
69 case kObsoleteReferrerPolicyDefault:
70 // "always" and "default" are the same value in all versions.
71 *mapped_referrer_policy = referrer_policy;
72 return true;
74 default:
75 // Since we don't know what encoding was used, we map the rest to "never".
76 *mapped_referrer_policy = web::ReferrerPolicyNever;
77 return false;
81 std::string
82 IOSSerializedNavigationDriver::GetSanitizedPageStateForPickle(
83 const SerializedNavigationEntry* navigation) const {
84 return std::string();
87 void IOSSerializedNavigationDriver::Sanitize(
88 SerializedNavigationEntry* navigation) const {
89 web::Referrer referrer(
90 navigation->referrer_url_,
91 static_cast<web::ReferrerPolicy>(navigation->referrer_policy_));
93 if (!navigation->virtual_url_.SchemeIsHTTPOrHTTPS() ||
94 !referrer.url.SchemeIsHTTPOrHTTPS()) {
95 referrer.url = GURL();
96 } else {
97 if (referrer.policy < 0 || referrer.policy > web::ReferrerPolicyLast) {
98 NOTREACHED();
99 referrer.policy = web::ReferrerPolicyNever;
101 bool is_downgrade = referrer.url.SchemeIsSecure() &&
102 !navigation->virtual_url_.SchemeIsSecure();
103 switch (referrer.policy) {
104 case web::ReferrerPolicyDefault:
105 if (is_downgrade)
106 referrer.url = GURL();
107 break;
108 case web::ReferrerPolicyNoReferrerWhenDowngrade:
109 if (is_downgrade)
110 referrer.url = GURL();
111 case web::ReferrerPolicyAlways:
112 break;
113 case web::ReferrerPolicyNever:
114 referrer.url = GURL();
115 break;
116 case web::ReferrerPolicyOrigin:
117 referrer.url = referrer.url.GetOrigin();
118 break;
119 case web::ReferrerPolicyOriginWhenCrossOrigin:
120 if (navigation->virtual_url_.GetOrigin() != referrer.url.GetOrigin())
121 referrer.url = referrer.url.GetOrigin();
122 break;
126 // Reset the referrer if it has changed.
127 if (navigation->referrer_url_ != referrer.url) {
128 navigation->referrer_url_ = GURL();
129 navigation->referrer_policy_ = GetDefaultReferrerPolicy();
133 std::string IOSSerializedNavigationDriver::StripReferrerFromPageState(
134 const std::string& page_state) const {
135 return std::string();
138 } // namespace sessions