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

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

Issue 1912: Renames BoomarkBarModel to BookmarkModel. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 3 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_bar_model.h" 9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "googleurl/src/gurl.h" 10 #include "googleurl/src/gurl.h"
11 11
12 #include "generated_resources.h" 12 #include "generated_resources.h"
13 13
14 // Key names. 14 // Key names.
15 static const wchar_t* kRootsKey = L"roots"; 15 static const wchar_t* kRootsKey = L"roots";
16 static const wchar_t* kRootFolderNameKey = L"bookmark_bar"; 16 static const wchar_t* kRootFolderNameKey = L"bookmark_bar";
17 static const wchar_t* kOtherBookmarFolderNameKey = L"other"; 17 static const wchar_t* kOtherBookmarFolderNameKey = L"other";
18 static const wchar_t* kVersionKey = L"version"; 18 static const wchar_t* kVersionKey = L"version";
19 static const wchar_t* kTypeKey = L"type"; 19 static const wchar_t* kTypeKey = L"type";
20 static const wchar_t* kNameKey = L"name"; 20 static const wchar_t* kNameKey = L"name";
21 static const wchar_t* kDateAddedKey = L"date_added"; 21 static const wchar_t* kDateAddedKey = L"date_added";
22 static const wchar_t* kURLKey = L"url"; 22 static const wchar_t* kURLKey = L"url";
23 static const wchar_t* kDateModifiedKey = L"date_modified"; 23 static const wchar_t* kDateModifiedKey = L"date_modified";
24 static const wchar_t* kChildrenKey = L"children"; 24 static const wchar_t* kChildrenKey = L"children";
25 25
26 // Possible values for kTypeKey. 26 // Possible values for kTypeKey.
27 static const wchar_t* kTypeURL = L"url"; 27 static const wchar_t* kTypeURL = L"url";
28 static const wchar_t* kTypeFolder = L"folder"; 28 static const wchar_t* kTypeFolder = L"folder";
29 29
30 // Current version of the file. 30 // Current version of the file.
31 static const int kCurrentVersion = 1; 31 static const int kCurrentVersion = 1;
32 32
33 Value* BookmarkCodec::Encode(BookmarkBarModel* model) { 33 Value* BookmarkCodec::Encode(BookmarkModel* model) {
34 return Encode(model->GetBookmarkBarNode(), model->other_node()); 34 return Encode(model->GetBookmarkBarNode(), model->other_node());
35 } 35 }
36 36
37 Value* BookmarkCodec::Encode(BookmarkBarNode* bookmark_bar_node, 37 Value* BookmarkCodec::Encode(BookmarkNode* bookmark_bar_node,
38 BookmarkBarNode* other_folder_node) { 38 BookmarkNode* other_folder_node) {
39 DictionaryValue* roots = new DictionaryValue(); 39 DictionaryValue* roots = new DictionaryValue();
40 roots->Set(kRootFolderNameKey, EncodeNode(bookmark_bar_node)); 40 roots->Set(kRootFolderNameKey, EncodeNode(bookmark_bar_node));
41 roots->Set(kOtherBookmarFolderNameKey, EncodeNode(other_folder_node)); 41 roots->Set(kOtherBookmarFolderNameKey, EncodeNode(other_folder_node));
42 42
43 DictionaryValue* main = new DictionaryValue(); 43 DictionaryValue* main = new DictionaryValue();
44 main->SetInteger(kVersionKey, kCurrentVersion); 44 main->SetInteger(kVersionKey, kCurrentVersion);
45 main->Set(kRootsKey, roots); 45 main->Set(kRootsKey, roots);
46 return main; 46 return main;
47 } 47 }
48 48
49 bool BookmarkCodec::Decode(BookmarkBarModel* model, const Value& value) { 49 bool BookmarkCodec::Decode(BookmarkModel* model, const Value& value) {
50 if (value.GetType() != Value::TYPE_DICTIONARY) 50 if (value.GetType() != Value::TYPE_DICTIONARY)
51 return false; // Unexpected type. 51 return false; // Unexpected type.
52 52
53 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value); 53 const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value);
54 54
55 int version; 55 int version;
56 if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion) 56 if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion)
57 return false; // Unknown version. 57 return false; // Unknown version.
58 58
59 Value* roots; 59 Value* roots;
(...skipping 21 matching lines...) Expand all
81 // the file. 81 // the file.
82 model->GetBookmarkBarNode()->type_ = history::StarredEntry::BOOKMARK_BAR; 82 model->GetBookmarkBarNode()->type_ = history::StarredEntry::BOOKMARK_BAR;
83 model->other_node()->type_ = history::StarredEntry::OTHER; 83 model->other_node()->type_ = history::StarredEntry::OTHER;
84 model->GetBookmarkBarNode()->SetTitle( 84 model->GetBookmarkBarNode()->SetTitle(
85 l10n_util::GetString(IDS_BOOMARK_BAR_FOLDER_NAME)); 85 l10n_util::GetString(IDS_BOOMARK_BAR_FOLDER_NAME));
86 model->other_node()->SetTitle( 86 model->other_node()->SetTitle(
87 l10n_util::GetString(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME)); 87 l10n_util::GetString(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME));
88 return true; 88 return true;
89 } 89 }
90 90
91 Value* BookmarkCodec::EncodeNode(BookmarkBarNode* node) { 91 Value* BookmarkCodec::EncodeNode(BookmarkNode* node) {
92 DictionaryValue* value = new DictionaryValue(); 92 DictionaryValue* value = new DictionaryValue();
93 value->SetString(kNameKey, node->GetTitle()); 93 value->SetString(kNameKey, node->GetTitle());
94 value->SetString(kDateAddedKey, 94 value->SetString(kDateAddedKey,
95 Int64ToWString(node->date_added().ToInternalValue())); 95 Int64ToWString(node->date_added().ToInternalValue()));
96 if (node->GetType() == history::StarredEntry::URL) { 96 if (node->GetType() == history::StarredEntry::URL) {
97 value->SetString(kTypeKey, kTypeURL); 97 value->SetString(kTypeKey, kTypeURL);
98 value->SetString(kURLKey, 98 value->SetString(kURLKey,
99 UTF8ToWide(node->GetURL().possibly_invalid_spec())); 99 UTF8ToWide(node->GetURL().possibly_invalid_spec()));
100 } else { 100 } else {
101 value->SetString(kTypeKey, kTypeFolder); 101 value->SetString(kTypeKey, kTypeFolder);
102 value->SetString(kDateModifiedKey, 102 value->SetString(kDateModifiedKey,
103 Int64ToWString(node->date_group_modified(). 103 Int64ToWString(node->date_group_modified().
104 ToInternalValue())); 104 ToInternalValue()));
105 105
106 ListValue* child_values = new ListValue(); 106 ListValue* child_values = new ListValue();
107 value->Set(kChildrenKey, child_values); 107 value->Set(kChildrenKey, child_values);
108 for (int i = 0; i < node->GetChildCount(); ++i) 108 for (int i = 0; i < node->GetChildCount(); ++i)
109 child_values->Append(EncodeNode(node->GetChild(i))); 109 child_values->Append(EncodeNode(node->GetChild(i)));
110 } 110 }
111 return value; 111 return value;
112 } 112 }
113 113
114 bool BookmarkCodec::DecodeChildren(BookmarkBarModel* model, 114 bool BookmarkCodec::DecodeChildren(BookmarkModel* model,
115 const ListValue& child_value_list, 115 const ListValue& child_value_list,
116 BookmarkBarNode* parent) { 116 BookmarkNode* parent) {
117 for (size_t i = 0; i < child_value_list.GetSize(); ++i) { 117 for (size_t i = 0; i < child_value_list.GetSize(); ++i) {
118 Value* child_value; 118 Value* child_value;
119 if (!child_value_list.Get(i, &child_value)) 119 if (!child_value_list.Get(i, &child_value))
120 return false; 120 return false;
121 121
122 if (child_value->GetType() != Value::TYPE_DICTIONARY) 122 if (child_value->GetType() != Value::TYPE_DICTIONARY)
123 return false; 123 return false;
124 124
125 if (!DecodeNode(model, *static_cast<DictionaryValue*>(child_value), parent, 125 if (!DecodeNode(model, *static_cast<DictionaryValue*>(child_value), parent,
126 NULL)) { 126 NULL)) {
127 return false; 127 return false;
128 } 128 }
129 } 129 }
130 return true; 130 return true;
131 } 131 }
132 132
133 bool BookmarkCodec::DecodeNode(BookmarkBarModel* model, 133 bool BookmarkCodec::DecodeNode(BookmarkModel* model,
134 const DictionaryValue& value, 134 const DictionaryValue& value,
135 BookmarkBarNode* parent, 135 BookmarkNode* parent,
136 BookmarkBarNode* node) { 136 BookmarkNode* node) {
137 bool created_node = (node == NULL); 137 bool created_node = (node == NULL);
138 std::wstring title; 138 std::wstring title;
139 if (!value.GetString(kNameKey, &title)) 139 if (!value.GetString(kNameKey, &title))
140 return false; 140 return false;
141 141
142 // TODO(sky): this should be more flexible. Don't hoark if we can't parse it 142 // TODO(sky): this should be more flexible. Don't hoark if we can't parse it
143 // all. 143 // all.
144 std::wstring date_added_string; 144 std::wstring date_added_string;
145 if (!value.GetString(kDateAddedKey, &date_added_string)) 145 if (!value.GetString(kDateAddedKey, &date_added_string))
146 return false; 146 return false;
147 147
148 std::wstring type_string; 148 std::wstring type_string;
149 if (!value.GetString(kTypeKey, &type_string)) 149 if (!value.GetString(kTypeKey, &type_string))
150 return false; 150 return false;
151 151
152 if (type_string != kTypeURL && type_string != kTypeFolder) 152 if (type_string != kTypeURL && type_string != kTypeFolder)
153 return false; // Unknown type. 153 return false; // Unknown type.
154 154
155 if (type_string == kTypeURL) { 155 if (type_string == kTypeURL) {
156 std::wstring url_string; 156 std::wstring url_string;
157 if (!value.GetString(kURLKey, &url_string)) 157 if (!value.GetString(kURLKey, &url_string))
158 return false; 158 return false;
159 // TODO(sky): this should ignore the node if not a valid URL. 159 // TODO(sky): this should ignore the node if not a valid URL.
160 if (!node) 160 if (!node)
161 node = new BookmarkBarNode(model, GURL(url_string)); 161 node = new BookmarkNode(model, GURL(url_string));
162 if (parent) 162 if (parent)
163 parent->Add(parent->GetChildCount(), node); 163 parent->Add(parent->GetChildCount(), node);
164 node->type_ = history::StarredEntry::URL; 164 node->type_ = history::StarredEntry::URL;
165 } else { 165 } else {
166 std::wstring last_modified_date; 166 std::wstring last_modified_date;
167 if (!value.GetString(kDateModifiedKey, &last_modified_date)) 167 if (!value.GetString(kDateModifiedKey, &last_modified_date))
168 return false; 168 return false;
169 169
170 Value* child_values; 170 Value* child_values;
171 if (!value.Get(kChildrenKey, &child_values)) 171 if (!value.Get(kChildrenKey, &child_values))
172 return false; 172 return false;
173 173
174 if (child_values->GetType() != Value::TYPE_LIST) 174 if (child_values->GetType() != Value::TYPE_LIST)
175 return false; 175 return false;
176 176
177 if (!node) 177 if (!node)
178 node = new BookmarkBarNode(model, GURL()); 178 node = new BookmarkNode(model, GURL());
179 node->type_ = history::StarredEntry::USER_GROUP; 179 node->type_ = history::StarredEntry::USER_GROUP;
180 node->date_group_modified_ = 180 node->date_group_modified_ =
181 Time::FromInternalValue(StringToInt64(last_modified_date)); 181 Time::FromInternalValue(StringToInt64(last_modified_date));
182 182
183 if (parent) 183 if (parent)
184 parent->Add(parent->GetChildCount(), node); 184 parent->Add(parent->GetChildCount(), node);
185 185
186 if (!DecodeChildren(model, *static_cast<ListValue*>(child_values), node)) 186 if (!DecodeChildren(model, *static_cast<ListValue*>(child_values), node))
187 return false; 187 return false;
188 } 188 }
189 189
190 node->SetTitle(title); 190 node->SetTitle(title);
191 node->date_added_ = 191 node->date_added_ =
192 Time::FromInternalValue(StringToInt64(date_added_string)); 192 Time::FromInternalValue(StringToInt64(date_added_string));
193 return true; 193 return true;
194 } 194 }
195 195
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698