summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmarks/bookmark_codec.cc
diff options
context:
space:
mode:
authorsky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-09 23:08:13 +0000
committersky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-09 23:08:13 +0000
commit68de8b76186059157dc916c499923d46bedb56b1 (patch)
tree870704b95740f2cafc144ced59b6bbf3c77c39d1 /chrome/browser/bookmarks/bookmark_codec.cc
parentd9b168764a9e7ad7dcd2561791041f660a54cdde (diff)
downloadchromium_src-68de8b76186059157dc916c499923d46bedb56b1.zip
chromium_src-68de8b76186059157dc916c499923d46bedb56b1.tar.gz
chromium_src-68de8b76186059157dc916c499923d46bedb56b1.tar.bz2
Moves bookmark related classes into bookmarks directory. There are no
code changes here (other than converting to COPY_AND_BLAH_BLAH and updating include guards). BUG=none TEST=none Review URL: http://codereview.chromium.org/1868 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1944 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/bookmarks/bookmark_codec.cc')
-rw-r--r--chrome/browser/bookmarks/bookmark_codec.cc195
1 files changed, 195 insertions, 0 deletions
diff --git a/chrome/browser/bookmarks/bookmark_codec.cc b/chrome/browser/bookmarks/bookmark_codec.cc
new file mode 100644
index 0000000..93874f3
--- /dev/null
+++ b/chrome/browser/bookmarks/bookmark_codec.cc
@@ -0,0 +1,195 @@
+// Copyright (c) 2006-2008 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 "chrome/browser/bookmarks/bookmark_codec.h"
+
+#include "base/string_util.h"
+#include "base/values.h"
+#include "chrome/browser/bookmarks/bookmark_bar_model.h"
+#include "googleurl/src/gurl.h"
+
+#include "generated_resources.h"
+
+// Key names.
+static const wchar_t* kRootsKey = L"roots";
+static const wchar_t* kRootFolderNameKey = L"bookmark_bar";
+static const wchar_t* kOtherBookmarFolderNameKey = L"other";
+static const wchar_t* kVersionKey = L"version";
+static const wchar_t* kTypeKey = L"type";
+static const wchar_t* kNameKey = L"name";
+static const wchar_t* kDateAddedKey = L"date_added";
+static const wchar_t* kURLKey = L"url";
+static const wchar_t* kDateModifiedKey = L"date_modified";
+static const wchar_t* kChildrenKey = L"children";
+
+// Possible values for kTypeKey.
+static const wchar_t* kTypeURL = L"url";
+static const wchar_t* kTypeFolder = L"folder";
+
+// Current version of the file.
+static const int kCurrentVersion = 1;
+
+Value* BookmarkCodec::Encode(BookmarkBarModel* model) {
+ return Encode(model->GetBookmarkBarNode(), model->other_node());
+}
+
+Value* BookmarkCodec::Encode(BookmarkBarNode* bookmark_bar_node,
+ BookmarkBarNode* other_folder_node) {
+ DictionaryValue* roots = new DictionaryValue();
+ roots->Set(kRootFolderNameKey, EncodeNode(bookmark_bar_node));
+ roots->Set(kOtherBookmarFolderNameKey, EncodeNode(other_folder_node));
+
+ DictionaryValue* main = new DictionaryValue();
+ main->SetInteger(kVersionKey, kCurrentVersion);
+ main->Set(kRootsKey, roots);
+ return main;
+}
+
+bool BookmarkCodec::Decode(BookmarkBarModel* model, const Value& value) {
+ if (value.GetType() != Value::TYPE_DICTIONARY)
+ return false; // Unexpected type.
+
+ const DictionaryValue& d_value = static_cast<const DictionaryValue&>(value);
+
+ int version;
+ if (!d_value.GetInteger(kVersionKey, &version) || version != kCurrentVersion)
+ return false; // Unknown version.
+
+ Value* roots;
+ if (!d_value.Get(kRootsKey, &roots))
+ return false; // No roots.
+
+ if (roots->GetType() != Value::TYPE_DICTIONARY)
+ return false; // Invalid type for roots.
+
+ DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots);
+ Value* root_folder_value;
+ Value* other_folder_value;
+ if (!roots_d_value->Get(kRootFolderNameKey, &root_folder_value) ||
+ root_folder_value->GetType() != Value::TYPE_DICTIONARY ||
+ !roots_d_value->Get(kOtherBookmarFolderNameKey, &other_folder_value) ||
+ other_folder_value->GetType() != Value::TYPE_DICTIONARY)
+ return false; // Invalid type for root folder and/or other folder.
+
+ DecodeNode(model, *static_cast<DictionaryValue*>(root_folder_value),
+ NULL, model->GetBookmarkBarNode());
+ DecodeNode(model, *static_cast<DictionaryValue*>(other_folder_value),
+ NULL, model->other_node());
+ // Need to reset the type as decoding resets the type to FOLDER. Similarly
+ // we need to reset the title as the title is persisted and restored from
+ // the file.
+ model->GetBookmarkBarNode()->type_ = history::StarredEntry::BOOKMARK_BAR;
+ model->other_node()->type_ = history::StarredEntry::OTHER;
+ model->GetBookmarkBarNode()->SetTitle(
+ l10n_util::GetString(IDS_BOOMARK_BAR_FOLDER_NAME));
+ model->other_node()->SetTitle(
+ l10n_util::GetString(IDS_BOOMARK_BAR_OTHER_FOLDER_NAME));
+ return true;
+}
+
+Value* BookmarkCodec::EncodeNode(BookmarkBarNode* node) {
+ DictionaryValue* value = new DictionaryValue();
+ value->SetString(kNameKey, node->GetTitle());
+ value->SetString(kDateAddedKey,
+ Int64ToWString(node->date_added().ToInternalValue()));
+ if (node->GetType() == history::StarredEntry::URL) {
+ value->SetString(kTypeKey, kTypeURL);
+ value->SetString(kURLKey,
+ UTF8ToWide(node->GetURL().possibly_invalid_spec()));
+ } else {
+ value->SetString(kTypeKey, kTypeFolder);
+ value->SetString(kDateModifiedKey,
+ Int64ToWString(node->date_group_modified().
+ ToInternalValue()));
+
+ ListValue* child_values = new ListValue();
+ value->Set(kChildrenKey, child_values);
+ for (int i = 0; i < node->GetChildCount(); ++i)
+ child_values->Append(EncodeNode(node->GetChild(i)));
+ }
+ return value;
+}
+
+bool BookmarkCodec::DecodeChildren(BookmarkBarModel* model,
+ const ListValue& child_value_list,
+ BookmarkBarNode* parent) {
+ for (size_t i = 0; i < child_value_list.GetSize(); ++i) {
+ Value* child_value;
+ if (!child_value_list.Get(i, &child_value))
+ return false;
+
+ if (child_value->GetType() != Value::TYPE_DICTIONARY)
+ return false;
+
+ if (!DecodeNode(model, *static_cast<DictionaryValue*>(child_value), parent,
+ NULL)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool BookmarkCodec::DecodeNode(BookmarkBarModel* model,
+ const DictionaryValue& value,
+ BookmarkBarNode* parent,
+ BookmarkBarNode* node) {
+ bool created_node = (node == NULL);
+ std::wstring title;
+ if (!value.GetString(kNameKey, &title))
+ return false;
+
+ // TODO(sky): this should be more flexible. Don't hoark if we can't parse it
+ // all.
+ std::wstring date_added_string;
+ if (!value.GetString(kDateAddedKey, &date_added_string))
+ return false;
+
+ std::wstring type_string;
+ if (!value.GetString(kTypeKey, &type_string))
+ return false;
+
+ if (type_string != kTypeURL && type_string != kTypeFolder)
+ return false; // Unknown type.
+
+ if (type_string == kTypeURL) {
+ std::wstring url_string;
+ if (!value.GetString(kURLKey, &url_string))
+ return false;
+ // TODO(sky): this should ignore the node if not a valid URL.
+ if (!node)
+ node = new BookmarkBarNode(model, GURL(url_string));
+ if (parent)
+ parent->Add(parent->GetChildCount(), node);
+ node->type_ = history::StarredEntry::URL;
+ } else {
+ std::wstring last_modified_date;
+ if (!value.GetString(kDateModifiedKey, &last_modified_date))
+ return false;
+
+ Value* child_values;
+ if (!value.Get(kChildrenKey, &child_values))
+ return false;
+
+ if (child_values->GetType() != Value::TYPE_LIST)
+ return false;
+
+ if (!node)
+ node = new BookmarkBarNode(model, GURL());
+ node->type_ = history::StarredEntry::USER_GROUP;
+ node->date_group_modified_ =
+ Time::FromInternalValue(StringToInt64(last_modified_date));
+
+ if (parent)
+ parent->Add(parent->GetChildCount(), node);
+
+ if (!DecodeChildren(model, *static_cast<ListValue*>(child_values), node))
+ return false;
+ }
+
+ node->SetTitle(title);
+ node->date_added_ =
+ Time::FromInternalValue(StringToInt64(date_added_string));
+ return true;
+}
+