Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: chrome/browser/bookmarks/bookmark_codec.cc

Issue 31014: Port DictionaryValue to use string16 instead of wstring. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 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 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/bookmarks/bookmark_codec.h" 5 #include "chrome/browser/bookmarks/bookmark_codec.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h" 9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/common/l10n_util.h" 10 #include "chrome/common/l10n_util.h"
(...skipping 18 matching lines...) Expand all
29 // Current version of the file. 29 // Current version of the file.
30 static const int kCurrentVersion = 1; 30 static const int kCurrentVersion = 1;
31 31
32 Value* BookmarkCodec::Encode(BookmarkModel* model) { 32 Value* BookmarkCodec::Encode(BookmarkModel* model) {
33 return Encode(model->GetBookmarkBarNode(), model->other_node()); 33 return Encode(model->GetBookmarkBarNode(), model->other_node());
34 } 34 }
35 35
36 Value* BookmarkCodec::Encode(BookmarkNode* bookmark_bar_node, 36 Value* BookmarkCodec::Encode(BookmarkNode* bookmark_bar_node,
37 BookmarkNode* other_folder_node) { 37 BookmarkNode* other_folder_node) {
38 DictionaryValue* roots = new DictionaryValue(); 38 DictionaryValue* roots = new DictionaryValue();
39 roots->Set(kRootFolderNameKey, EncodeNode(bookmark_bar_node)); 39 roots->Set(WideToUTF16Hack(kRootFolderNameKey),
40 roots->Set(kOtherBookmarFolderNameKey, EncodeNode(other_folder_node)); 40 EncodeNode(bookmark_bar_node));
41 roots->Set(WideToUTF16Hack(kOtherBookmarFolderNameKey),
42 EncodeNode(other_folder_node));
41 43
42 DictionaryValue* main = new DictionaryValue(); 44 DictionaryValue* main = new DictionaryValue();
43 main->SetInteger(kVersionKey, kCurrentVersion); 45 main->SetInteger(WideToUTF16Hack(kVersionKey), kCurrentVersion);
44 main->Set(kRootsKey, roots); 46 main->Set(WideToUTF16Hack(kRootsKey), roots);
45 return main; 47 return main;
46 } 48 }
47 49
48 bool BookmarkCodec::Decode(BookmarkModel* model, const Value& value) { 50 bool BookmarkCodec::Decode(BookmarkModel* model, const Value& value) {
49 if (value.GetType() != Value::TYPE_DICTIONARY) 51 if (value.GetType() != Value::TYPE_DICTIONARY)
50 return false; // Unexpected type. 52 return false; // Unexpected type.
51 53
52 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value); 54 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value);
53 55
54 int version; 56 int version;
55 if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion) 57 if (!d_value.GetInteger(WideToUTF16Hack(kVersionKey), &version) ||
58 version != kCurrentVersion)
56 return false; // Unknown version. 59 return false; // Unknown version.
57 60
58 Value* roots; 61 Value* roots;
59 if (!d_value.Get(kRootsKey, &roots)) 62 if (!d_value.Get(WideToUTF16Hack(kRootsKey), &roots))
60 return false; // No roots. 63 return false; // No roots.
61 64
62 if (roots->GetType() != Value::TYPE_DICTIONARY) 65 if (roots->GetType() != Value::TYPE_DICTIONARY)
63 return false; // Invalid type for roots. 66 return false; // Invalid type for roots.
64 67
65 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots); 68 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots);
66 Value* root_folder_value; 69 Value* root_folder_value;
67 Value* other_folder_value; 70 Value* other_folder_value;
68 if (!roots_d_value->Get(kRootFolderNameKey, &root_folder_value) || 71 if (!roots_d_value->Get(WideToUTF16Hack(kRootFolderNameKey),
72 &root_folder_value) ||
69 root_folder_value->GetType() != Value::TYPE_DICTIONARY || 73 root_folder_value->GetType() != Value::TYPE_DICTIONARY ||
70 !roots_d_value->Get(kOtherBookmarFolderNameKey, &other_folder_value) || 74 !roots_d_value->Get(WideToUTF16Hack(kOtherBookmarFolderNameKey),
71 other_folder_value->GetType() != Value::TYPE_DICTIONARY) 75 &other_folder_value) ||
76 other_folder_value->GetType() != Value::TYPE_DICTIONARY) {
72 return false; // Invalid type for root folder and/or other folder. 77 return false; // Invalid type for root folder and/or other folder.
78 }
73 79
74 DecodeNode(model, *static_cast<DictionaryValue*>(root_folder_value), 80 DecodeNode(model, *static_cast<DictionaryValue*>(root_folder_value),
75 NULL, model->GetBookmarkBarNode()); 81 NULL, model->GetBookmarkBarNode());
76 DecodeNode(model, *static_cast<DictionaryValue*>(other_folder_value), 82 DecodeNode(model, *static_cast<DictionaryValue*>(other_folder_value),
77 NULL, model->other_node()); 83 NULL, model->other_node());
78 // Need to reset the type as decoding resets the type to FOLDER. Similarly 84 // Need to reset the type as decoding resets the type to FOLDER. Similarly
79 // we need to reset the title as the title is persisted and restored from 85 // we need to reset the title as the title is persisted and restored from
80 // the file. 86 // the file.
81 model->GetBookmarkBarNode()->type_ = history::StarredEntry::BOOKMARK_BAR; 87 model->GetBookmarkBarNode()->type_ = history::StarredEntry::BOOKMARK_BAR;
82 model->other_node()->type_ = history::StarredEntry::OTHER; 88 model->other_node()->type_ = history::StarredEntry::OTHER;
83 model->GetBookmarkBarNode()->SetTitle( 89 model->GetBookmarkBarNode()->SetTitle(
84 l10n_util::GetString(IDS_BOOMARK_BAR_FOLDER_NAME)); 90 l10n_util::GetString(IDS_BOOMARK_BAR_FOLDER_NAME));
85 model->other_node()->SetTitle( 91 model->other_node()->SetTitle(
86 l10n_util::GetString(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); 92 l10n_util::GetString(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME));
87 return true; 93 return true;
88 } 94 }
89 95
90 Value* BookmarkCodec::EncodeNode(BookmarkNode* node) { 96 Value* BookmarkCodec::EncodeNode(BookmarkNode* node) {
91 DictionaryValue* value = new DictionaryValue(); 97 DictionaryValue* value = new DictionaryValue();
92 value->SetString(kNameKey, node->GetTitle()); 98 value->SetString(WideToUTF16Hack(kNameKey),
93 value->SetString(kDateAddedKey, 99 WideToUTF16Hack(node->GetTitle()));
94 Int64ToWString(node->date_added().ToInternalValue())); 100 value->SetString(WideToUTF16Hack(kDateAddedKey),
101 Int64ToString16(node->date_added().ToInternalValue()));
95 if (node->GetType() == history::StarredEntry::URL) { 102 if (node->GetType() == history::StarredEntry::URL) {
96 value->SetString(kTypeKey, kTypeURL); 103 value->SetString(WideToUTF16Hack(kTypeKey), WideToUTF16Hack(kTypeURL));
97 value->SetString(kURLKey, 104 value->SetString(WideToUTF16Hack(kURLKey),
98 UTF8ToWide(node->GetURL().possibly_invalid_spec())); 105 UTF8ToUTF16(node->GetURL().possibly_invalid_spec()));
99 } else { 106 } else {
100 value->SetString(kTypeKey, kTypeFolder); 107 value->SetString(WideToUTF16Hack(kTypeKey), WideToUTF16Hack(kTypeFolder));
101 value->SetString(kDateModifiedKey, 108 value->SetString(WideToUTF16Hack(kDateModifiedKey),
102 Int64ToWString(node->date_group_modified(). 109 Int64ToString16(node->date_group_modified().
103 ToInternalValue())); 110 ToInternalValue()));
104 111
105 ListValue* child_values = new ListValue(); 112 ListValue* child_values = new ListValue();
106 value->Set(kChildrenKey, child_values); 113 value->Set(WideToUTF16Hack(kChildrenKey), child_values);
107 for (int i = 0; i < node->GetChildCount(); ++i) 114 for (int i = 0; i < node->GetChildCount(); ++i)
108 child_values->Append(EncodeNode(node->GetChild(i))); 115 child_values->Append(EncodeNode(node->GetChild(i)));
109 } 116 }
110 return value; 117 return value;
111 } 118 }
112 119
113 bool BookmarkCodec::DecodeChildren(BookmarkModel* model, 120 bool BookmarkCodec::DecodeChildren(BookmarkModel* model,
114 const ListValue& child_value_list, 121 const ListValue& child_value_list,
115 BookmarkNode* parent) { 122 BookmarkNode* parent) {
116 for (size_t i = 0; i < child_value_list.GetSize(); ++i) { 123 for (size_t i = 0; i < child_value_list.GetSize(); ++i) {
117 Value* child_value; 124 Value* child_value;
118 if (!child_value_list.Get(i, &child_value)) 125 if (!child_value_list.Get(i, &child_value))
119 return false; 126 return false;
120 127
121 if (child_value->GetType() != Value::TYPE_DICTIONARY) 128 if (child_value->GetType() != Value::TYPE_DICTIONARY)
122 return false; 129 return false;
123 130
124 if (!DecodeNode(model, *static_cast<DictionaryValue*>(child_value), parent, 131 if (!DecodeNode(model, *static_cast<DictionaryValue*>(child_value), parent,
125 NULL)) { 132 NULL)) {
126 return false; 133 return false;
127 } 134 }
128 } 135 }
129 return true; 136 return true;
130 } 137 }
131 138
132 bool BookmarkCodec::DecodeNode(BookmarkModel* model, 139 bool BookmarkCodec::DecodeNode(BookmarkModel* model,
133 const DictionaryValue& value, 140 const DictionaryValue& value,
134 BookmarkNode* parent, 141 BookmarkNode* parent,
135 BookmarkNode* node) { 142 BookmarkNode* node) {
136 std::wstring title; 143 string16 title;
137 if (!value.GetString(kNameKey, &title)) 144 if (!value.GetString(WideToUTF16Hack(kNameKey), &title))
138 return false; 145 return false;
139 146
140 // TODO(sky): this should be more flexible. Don't hoark if we can't parse it 147 // TODO(sky): this should be more flexible. Don't hoark if we can't parse it
141 // all. 148 // all.
142 std::wstring date_added_string; 149 string16 date_added_string;
143 if (!value.GetString(kDateAddedKey, &date_added_string)) 150 if (!value.GetString(WideToUTF16Hack(kDateAddedKey), &date_added_string))
144 return false; 151 return false;
145 152
146 std::wstring type_string; 153 string16 type_string;
147 if (!value.GetString(kTypeKey, &type_string)) 154 if (!value.GetString(WideToUTF16Hack(kTypeKey), &type_string))
148 return false; 155 return false;
149 156
150 if (type_string != kTypeURL && type_string != kTypeFolder) 157 if (type_string != WideToUTF16Hack(kTypeURL) &&
158 type_string != WideToUTF16Hack(kTypeFolder))
151 return false; // Unknown type. 159 return false; // Unknown type.
152 160
153 if (type_string == kTypeURL) { 161 if (type_string == WideToUTF16Hack(kTypeURL)) {
154 std::wstring url_string; 162 string16 url_string;
155 if (!value.GetString(kURLKey, &url_string)) 163 if (!value.GetString(WideToUTF16Hack(kURLKey), &url_string))
156 return false; 164 return false;
157 // TODO(sky): this should ignore the node if not a valid URL. 165 // TODO(sky): this should ignore the node if not a valid URL.
158 if (!node) 166 if (!node)
159 node = new BookmarkNode(model, GURL(WideToUTF8(url_string))); 167 node = new BookmarkNode(model, GURL(UTF16ToUTF8(url_string)));
160 if (parent) 168 if (parent)
161 parent->Add(parent->GetChildCount(), node); 169 parent->Add(parent->GetChildCount(), node);
162 node->type_ = history::StarredEntry::URL; 170 node->type_ = history::StarredEntry::URL;
163 } else { 171 } else {
164 std::wstring last_modified_date; 172 string16 last_modified_date;
165 if (!value.GetString(kDateModifiedKey, &last_modified_date)) 173 if (!value.GetString(WideToUTF16Hack(kDateModifiedKey),
174 &last_modified_date))
166 return false; 175 return false;
167 176
168 Value* child_values; 177 Value* child_values;
169 if (!value.Get(kChildrenKey, &child_values)) 178 if (!value.Get(WideToUTF16Hack(kChildrenKey), &child_values))
170 return false; 179 return false;
171 180
172 if (child_values->GetType() != Value::TYPE_LIST) 181 if (child_values->GetType() != Value::TYPE_LIST)
173 return false; 182 return false;
174 183
175 if (!node) 184 if (!node)
176 node = new BookmarkNode(model, GURL()); 185 node = new BookmarkNode(model, GURL());
177 node->type_ = history::StarredEntry::USER_GROUP; 186 node->type_ = history::StarredEntry::USER_GROUP;
178 node->date_group_modified_ = Time::FromInternalValue( 187 node->date_group_modified_ =
179 StringToInt64(WideToUTF16Hack(last_modified_date))); 188 Time::FromInternalValue(StringToInt64(last_modified_date));
180 189
181 if (parent) 190 if (parent)
182 parent->Add(parent->GetChildCount(), node); 191 parent->Add(parent->GetChildCount(), node);
183 192
184 if (!DecodeChildren(model, *static_cast<ListValue*>(child_values), node)) 193 if (!DecodeChildren(model, *static_cast<ListValue*>(child_values), node))
185 return false; 194 return false;
186 } 195 }
187 196
188 node->SetTitle(title); 197 node->SetTitle(UTF16ToWideHack(title));
189 node->date_added_ = Time::FromInternalValue( 198 node->date_added_ =
190 StringToInt64(WideToUTF16Hack(date_added_string))); 199 Time::FromInternalValue(StringToInt64(date_added_string));
191 return true; 200 return true;
192 } 201 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/search_provider.cc ('k') | chrome/browser/bookmarks/bookmark_html_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698