OLD | NEW |
| (Empty) |
1 // 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. | |
4 | |
5 #include <string> | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/logging.h" | |
9 #include "base/string_number_conversions.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/page_state.h" | |
12 #include "chrome/common/json_value_serializer.h" | |
13 #include "googleurl/src/gurl.h" | |
14 #include "net/base/escape.h" | |
15 | |
16 void PageState::InitWithURL(const GURL& url) { | |
17 // Reset our state | |
18 state_.reset(new DictionaryValue); | |
19 | |
20 std::string query_string = url.query(); | |
21 if (query_string.empty()) | |
22 return; | |
23 | |
24 url_parse::Component queryComp, keyComp, valueComp; | |
25 queryComp.len = static_cast<int>(query_string.size()); | |
26 while (url_parse::ExtractQueryKeyValue(query_string.c_str(), &queryComp, | |
27 &keyComp, &valueComp)) { | |
28 if (keyComp.is_nonempty()) { | |
29 std::string escaped = query_string.substr(valueComp.begin, | |
30 valueComp.len); | |
31 // We know that the query string is UTF-8 since it's an internal URL. | |
32 std::wstring value = UTF8ToWide( | |
33 UnescapeURLComponent(escaped, UnescapeRule::REPLACE_PLUS_WITH_SPACE)); | |
34 state_->Set(UTF8ToWide(query_string.substr(keyComp.begin, keyComp.len)), | |
35 new StringValue(value)); | |
36 } | |
37 } | |
38 } | |
39 | |
40 void PageState::InitWithBytes(const std::string& bytes) { | |
41 // Reset our state. We create a new empty one just in case | |
42 // deserialization fails | |
43 state_.reset(new DictionaryValue); | |
44 | |
45 JSONStringValueSerializer serializer(bytes); | |
46 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); | |
47 | |
48 if (!root.get()) { | |
49 NOTREACHED(); | |
50 return; | |
51 } | |
52 | |
53 if (root->GetType() == Value::TYPE_DICTIONARY) | |
54 state_.reset(static_cast<DictionaryValue*>(root.release())); | |
55 } | |
56 | |
57 void PageState::GetByteRepresentation(std::string* out) const { | |
58 JSONStringValueSerializer serializer(out); | |
59 if (!serializer.Serialize(*state_)) | |
60 NOTREACHED(); | |
61 } | |
62 | |
63 void PageState::SetProperty(const std::wstring& key, | |
64 const std::wstring& value) { | |
65 state_->Set(key, new StringValue(value)); | |
66 } | |
67 | |
68 bool PageState::GetProperty(const std::wstring& key, | |
69 std::wstring* value) const { | |
70 if (state_->HasKey(key)) { | |
71 Value* v; | |
72 state_->Get(key, &v); | |
73 if (v->GetType() == Value::TYPE_STRING) { | |
74 StringValue* sv = reinterpret_cast<StringValue*>(v); | |
75 sv->GetAsString(value); | |
76 return true; | |
77 } | |
78 } | |
79 return false; | |
80 } | |
81 | |
82 void PageState::SetInt64Property(const std::wstring& key, int64 value) { | |
83 SetProperty(key, UTF8ToWide(base::Int64ToString(value))); | |
84 } | |
85 | |
86 bool PageState::GetInt64Property(const std::wstring& key, int64* value) const { | |
87 std::wstring v; | |
88 if (GetProperty(key, &v)) { | |
89 return base::StringToInt64(WideToUTF8(v), value); | |
90 } | |
91 return false; | |
92 } | |
93 | |
94 void PageState::SetIntProperty(const std::wstring& key, int value) { | |
95 SetProperty(key, UTF8ToWide(base::IntToString(value))); | |
96 } | |
97 | |
98 bool PageState::GetIntProperty(const std::wstring& key, int* value) const { | |
99 std::wstring v; | |
100 if (GetProperty(key, &v)) { | |
101 return base::StringToInt(WideToUTF8(v), value); | |
102 } | |
103 return false; | |
104 } | |
105 | |
106 PageState* PageState::Copy() const { | |
107 PageState* copy = new PageState(); | |
108 if (state_.get()) | |
109 copy->state_.reset(static_cast<DictionaryValue*>(state_->DeepCopy())); | |
110 return copy; | |
111 } | |
OLD | NEW |