summaryrefslogtreecommitdiffstats
path: root/content/public/common/page_state.cc
blob: 1f3dd2ff72d54ae7e474d123bc408f158e46e1c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/common/page_state.h"

#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h"
#include "content/common/page_state_serialization.h"
#include "content/public/common/referrer.h"

namespace content {
namespace {

base::NullableString16 ToNullableString16(const std::string& utf8) {
  return base::NullableString16(base::UTF8ToUTF16(utf8), false);
}

base::FilePath ToFilePath(const base::NullableString16& s) {
  return base::FilePath::FromUTF16Unsafe(s.string());
}

void ToFilePathVector(const std::vector<base::NullableString16>& input,
                      std::vector<base::FilePath>* output) {
  output->clear();
  output->reserve(input.size());
  for (size_t i = 0; i < input.size(); ++i)
    output->push_back(ToFilePath(input[i]));
}

PageState ToPageState(const ExplodedPageState& state) {
  std::string encoded_data;
  if (!EncodePageState(state, &encoded_data))
    return PageState();

  return PageState::CreateFromEncodedData(encoded_data);
}

void RecursivelyRemovePasswordData(ExplodedFrameState* state) {
  if (state->http_body.contains_passwords)
    state->http_body = ExplodedHttpBody();
}

void RecursivelyRemoveScrollOffset(ExplodedFrameState* state) {
  state->scroll_offset = gfx::Point();
  state->pinch_viewport_scroll_offset = gfx::PointF();
}

void RecursivelyRemoveReferrer(ExplodedFrameState* state) {
  state->referrer = base::NullableString16();
  state->referrer_policy = blink::WebReferrerPolicyDefault;
  for (std::vector<ExplodedFrameState>::iterator it = state->children.begin();
       it != state->children.end();
       ++it) {
    RecursivelyRemoveReferrer(&*it);
  }
}

bool RecursivelyCheckReferrer(ExplodedFrameState* state) {
  Referrer referrer(GURL(state->referrer.string()), state->referrer_policy);
  GURL url(state->url_string.string());
  if (url.SchemeIsHTTPOrHTTPS() &&
      Referrer::SanitizeForRequest(url, referrer).url != referrer.url) {
    LOG(ERROR) << "Referrer for request to " << url << " is " << referrer.url
               << " but should be "
               << Referrer::SanitizeForRequest(url, referrer).url;
    return false;
  }
  for (std::vector<ExplodedFrameState>::iterator it = state->children.begin();
       it != state->children.end();
       ++it) {
    if (!RecursivelyCheckReferrer(&*it))
      return false;
  }
  return true;
}

}  // namespace

// static
PageState PageState::CreateFromEncodedData(const std::string& data) {
  return PageState(data);
}

// static
PageState PageState::CreateFromURL(const GURL& url) {
  ExplodedPageState state;

  state.top.url_string = ToNullableString16(url.possibly_invalid_spec());

  return ToPageState(state);
}

// static
PageState PageState::CreateForTesting(
    const GURL& url,
    bool body_contains_password_data,
    const char* optional_body_data,
    const base::FilePath* optional_body_file_path) {
  ExplodedPageState state;

  state.top.url_string = ToNullableString16(url.possibly_invalid_spec());

  if (optional_body_data || optional_body_file_path) {
    state.top.http_body.is_null = false;
    if (optional_body_data) {
      ExplodedHttpBodyElement element;
      element.type = blink::WebHTTPBody::Element::TypeData;
      element.data = optional_body_data;
      state.top.http_body.elements.push_back(element);
    }
    if (optional_body_file_path) {
      ExplodedHttpBodyElement element;
      element.type = blink::WebHTTPBody::Element::TypeFile;
      element.file_path =
          ToNullableString16(optional_body_file_path->AsUTF8Unsafe());
      state.top.http_body.elements.push_back(element);
      state.referenced_files.push_back(element.file_path);
    }
    state.top.http_body.contains_passwords =
        body_contains_password_data;
  }

  return ToPageState(state);
}

PageState::PageState() {
}

bool PageState::IsValid() const {
  if (data_.empty())
    return false;

  ExplodedPageState state;
  // This should return false, but tests create invalid page state.
  if (!DecodePageState(data_, &state))
    return true;

  // TODO(jochen): Remove referrer check once http://crbug.com/450589 is fixed.
  return RecursivelyCheckReferrer(&state.top);
}

bool PageState::Equals(const PageState& other) const {
  return data_ == other.data_;
}

const std::string& PageState::ToEncodedData() const {
  return data_;
}

std::vector<base::FilePath> PageState::GetReferencedFiles() const {
  std::vector<base::FilePath> results;

  ExplodedPageState state;
  if (DecodePageState(data_, &state))
    ToFilePathVector(state.referenced_files, &results);

  return results;
}

PageState PageState::RemovePasswordData() const {
  ExplodedPageState state;
  if (!DecodePageState(data_, &state))
    return PageState();  // Oops!

  RecursivelyRemovePasswordData(&state.top);

  return ToPageState(state);
}

PageState PageState::RemoveScrollOffset() const {
  ExplodedPageState state;
  if (!DecodePageState(data_, &state))
    return PageState();  // Oops!

  RecursivelyRemoveScrollOffset(&state.top);

  return ToPageState(state);
}

PageState PageState::RemoveReferrer() const {
  if (data_.empty())
    return *this;

  ExplodedPageState state;
  if (!DecodePageState(data_, &state))
    return PageState();  // Oops!

  RecursivelyRemoveReferrer(&state.top);

  return ToPageState(state);
}

PageState::PageState(const std::string& data)
    : data_(data) {
  // TODO(darin): Enable this DCHECK once tests have been fixed up to not pass
  // bogus encoded data to CreateFromEncodedData.
  //DCHECK(IsValid());
}

}  // namespace content