blob: c9e253e241ce41563a57a258d53b68d600c27a7c [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#include "base/basictypes.h"
6#include "base/logging.h"
7#include "base/string_util.h"
8#include "chrome/browser/page_state.h"
9#include "chrome/common/json_value_serializer.h"
10#include "googleurl/src/gurl.h"
11#include "net/base/escape.h"
12
13void PageState::InitWithURL(const GURL& url) {
14 // Reset our state
15 state_.reset(new DictionaryValue);
16
17 std::string query_string = url.query();
18 if (query_string.empty())
19 return;
20
21 url_parse::Component queryComp, keyComp, valueComp;
22 queryComp.len = static_cast<int>(query_string.size());
23 while (url_parse::ExtractQueryKeyValue(query_string.c_str(), &queryComp,
24 &keyComp, &valueComp)) {
25 if (keyComp.is_nonempty()) {
26 std::string escaped = query_string.substr(valueComp.begin,
27 valueComp.len);
28 // We know that the query string is UTF-8 since it's an internal URL.
29 std::wstring value = UTF8ToWide(
30 UnescapeURLComponent(escaped, UnescapeRule::REPLACE_PLUS_WITH_SPACE));
[email protected]8e50b602009-03-03 22:59:4331 state_->Set(UTF8ToWide(query_string.substr(keyComp.begin, keyComp.len)),
initial.commit09911bf2008-07-26 23:55:2932 new StringValue(value));
33 }
34 }
35}
36
37void PageState::InitWithBytes(const std::string& bytes) {
38 // Reset our state. We create a new empty one just in case
39 // deserialization fails
40 state_.reset(new DictionaryValue);
41
42 JSONStringValueSerializer serializer(bytes);
[email protected]b4cebf82008-12-29 19:59:0843 scoped_ptr<Value> root(serializer.Deserialize(NULL));
initial.commit09911bf2008-07-26 23:55:2944
[email protected]b4cebf82008-12-29 19:59:0845 if (!root.get()) {
initial.commit09911bf2008-07-26 23:55:2946 NOTREACHED();
[email protected]b4cebf82008-12-29 19:59:0847 return;
initial.commit09911bf2008-07-26 23:55:2948 }
[email protected]b4cebf82008-12-29 19:59:0849
50 if (root->GetType() == Value::TYPE_DICTIONARY)
51 state_.reset(static_cast<DictionaryValue*>(root.release()));
initial.commit09911bf2008-07-26 23:55:2952}
53
54void PageState::GetByteRepresentation(std::string* out) const {
55 JSONStringValueSerializer serializer(out);
56 if (!serializer.Serialize(*state_))
57 NOTREACHED();
58}
59
60void PageState::SetProperty(const std::wstring& key,
61 const std::wstring& value) {
[email protected]8e50b602009-03-03 22:59:4362 state_->Set(key, new StringValue(value));
initial.commit09911bf2008-07-26 23:55:2963}
64
[email protected]d3216442009-03-05 21:07:2765bool PageState::GetProperty(const std::wstring& key,
66 std::wstring* value) const {
initial.commit09911bf2008-07-26 23:55:2967 if (state_->HasKey(key)) {
68 Value* v;
69 state_->Get(key, &v);
70 if (v->GetType() == Value::TYPE_STRING) {
71 StringValue* sv = reinterpret_cast<StringValue*>(v);
72 sv->GetAsString(value);
73 return true;
74 }
75 }
76 return false;
77}
78
79void PageState::SetInt64Property(const std::wstring& key, int64 value) {
[email protected]23afa0f2008-09-19 15:58:5580 SetProperty(key, Int64ToWString(value));
initial.commit09911bf2008-07-26 23:55:2981}
82
83bool PageState::GetInt64Property(const std::wstring& key, int64* value) const {
84 std::wstring v;
85 if (GetProperty(key, &v)) {
[email protected]41fb1d72009-02-28 01:01:5086 return StringToInt64(WideToUTF16Hack(v), value);
initial.commit09911bf2008-07-26 23:55:2987 }
88 return false;
89}
90
91void PageState::SetIntProperty(const std::wstring& key, int value) {
[email protected]23afa0f2008-09-19 15:58:5592 SetProperty(key, IntToWString(value));
initial.commit09911bf2008-07-26 23:55:2993}
94
95bool PageState::GetIntProperty(const std::wstring& key, int* value) const {
96 std::wstring v;
97 if (GetProperty(key, &v)) {
[email protected]41fb1d72009-02-28 01:01:5098 return StringToInt(WideToUTF16Hack(v), value);
initial.commit09911bf2008-07-26 23:55:2999 }
100 return false;
101}
102
103PageState* PageState::Copy() const {
104 PageState* copy = new PageState();
105 if (state_.get())
106 copy->state_.reset(static_cast<DictionaryValue*>(state_->DeepCopy()));
107 return copy;
108}