| Index: components/enhanced_bookmarks/enhanced_bookmark_model.cc
|
| diff --git a/components/enhanced_bookmarks/metadata_accessor.cc b/components/enhanced_bookmarks/enhanced_bookmark_model.cc
|
| similarity index 39%
|
| copy from components/enhanced_bookmarks/metadata_accessor.cc
|
| copy to components/enhanced_bookmarks/enhanced_bookmark_model.cc
|
| index cfcc9be5efb2bd2c59f596e159e7e63225e7194b..a20e38bac31b494cd8b63e2ff329fd38d645477e 100644
|
| --- a/components/enhanced_bookmarks/metadata_accessor.cc
|
| +++ b/components/enhanced_bookmarks/enhanced_bookmark_model.cc
|
| @@ -2,54 +2,48 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "components/enhanced_bookmarks/metadata_accessor.h"
|
| +#include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
|
|
|
| #include <iomanip>
|
| +#include <sstream>
|
|
|
| #include "base/base64.h"
|
| +#include "base/logging.h"
|
| #include "base/rand_util.h"
|
| #include "components/bookmarks/browser/bookmark_model.h"
|
| +#include "components/bookmarks/browser/bookmark_node.h"
|
| #include "components/enhanced_bookmarks/proto/metadata.pb.h"
|
| -#include "ui/base/models/tree_node_iterator.h"
|
| -
|
| -using namespace image::collections;
|
| +#include "url/gurl.h"
|
|
|
| namespace {
|
| +const char* kBookmarkBarId = "f_bookmarks_bar";
|
| +
|
| +const char* kIdKey = "stars.id";
|
| +const char* kImageDataKey = "stars.imageData";
|
| +const char* kNoteKey = "stars.note";
|
| +const char* kPageDataKey = "stars.pageData";
|
| +const char* kUserEditKey = "stars.userEdit";
|
| +const char* kVersionKey = "stars.version";
|
| +
|
| +const char* kFolderPrefix = "ebf_";
|
| +const char* kBookmarkPrefix = "ebc_";
|
|
|
| // Helper method for working with bookmark metainfo.
|
| std::string DataForMetaInfoField(const BookmarkNode* node,
|
| const std::string& field) {
|
| - const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
|
| - if (!map)
|
| - return "";
|
| -
|
| - BookmarkNode::MetaInfoMap::const_iterator it = map->find(field);
|
| - if (it == map->end())
|
| - return "";
|
| + std::string value;
|
| + if (!node->GetMetaInfo(field, &value))
|
| + return std::string();
|
|
|
| std::string decoded;
|
| - bool result = base::Base64Decode((*it).second, &decoded);
|
| - if (!result)
|
| - return "";
|
| + if (!base::Base64Decode(value, &decoded))
|
| + return std::string();
|
|
|
| return decoded;
|
| }
|
|
|
| -// Sets a new remote id on a bookmark.
|
| -std::string SetRemoteIdOnBookmark(BookmarkModel* bookmark_model,
|
| - const BookmarkNode* node) {
|
| - // Generate 16 digit hex string random id.
|
| - std::stringstream random_id;
|
| - random_id << std::hex << std::setfill('0') << std::setw(16);
|
| - random_id << base::RandUint64() << base::RandUint64();
|
| - std::string random_id_str = random_id.str();
|
| - bookmark_model->SetNodeMetaInfo(
|
| - node, enhanced_bookmarks::kIdDataKey, random_id_str);
|
| - return random_id_str;
|
| -}
|
| -
|
| // Helper method for working with ImageData_ImageInfo.
|
| -bool PopulateImageData(const ImageData_ImageInfo& info,
|
| +bool PopulateImageData(const image::collections::ImageData_ImageInfo& info,
|
| GURL* out_url,
|
| int* width,
|
| int* height) {
|
| @@ -66,58 +60,112 @@ bool PopulateImageData(const ImageData_ImageInfo& info,
|
| return true;
|
| }
|
|
|
| +// Generate a random remote id, with a prefix that depends on whether the node
|
| +// is a folder or a bookmark.
|
| +std::string GenerateRemoteId(bool is_folder) {
|
| + std::stringstream random_id;
|
| + // Add prefix depending on whether the node is a folder or not.
|
| + if (is_folder)
|
| + random_id << kFolderPrefix;
|
| + else
|
| + random_id << kBookmarkPrefix;
|
| +
|
| + // Generate 32 digit hex string random suffix.
|
| + random_id << std::hex << std::setfill('0') << std::setw(16);
|
| + random_id << base::RandUint64() << base::RandUint64();
|
| + return random_id.str();
|
| +}
|
| } // namespace
|
|
|
| namespace enhanced_bookmarks {
|
|
|
| -const char* kPageDataKey = "stars.pageData";
|
| -const char* kImageDataKey = "stars.imageData";
|
| -const char* kIdDataKey = "stars.id";
|
| -const char* kNoteKey = "stars.note";
|
| +EnhancedBookmarkModel::EnhancedBookmarkModel(BookmarkModel* bookmark_model,
|
| + const std::string& version)
|
| + : bookmark_model_(bookmark_model), version_(version) {
|
| +}
|
| +
|
| +EnhancedBookmarkModel::~EnhancedBookmarkModel() {
|
| +}
|
|
|
| -std::string RemoteIdFromBookmark(BookmarkModel* bookmark_model,
|
| - const BookmarkNode* node) {
|
| - const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
|
| - if (!map)
|
| - return SetRemoteIdOnBookmark(bookmark_model, node);
|
| +// Moves |node| to |new_parent| and inserts it at the given |index|.
|
| +void EnhancedBookmarkModel::Move(const BookmarkNode* node,
|
| + const BookmarkNode* new_parent,
|
| + int index) {
|
| + // TODO(rfevang): Update meta info placement fields.
|
| + bookmark_model_->Move(node, new_parent, index);
|
| +}
|
|
|
| - BookmarkNode::MetaInfoMap::const_iterator it = map->find(kIdDataKey);
|
| - if (it == map->end())
|
| - return SetRemoteIdOnBookmark(bookmark_model, node);
|
| +// Adds a new folder node at the specified position.
|
| +const BookmarkNode* EnhancedBookmarkModel::AddFolder(
|
| + const BookmarkNode* parent,
|
| + int index,
|
| + const base::string16& title) {
|
| + BookmarkNode::MetaInfoMap meta_info;
|
| + meta_info[kIdKey] = GenerateRemoteId(true);
|
| +
|
| + // TODO(rfevang): Set meta info placement fields.
|
| + return bookmark_model_->AddFolderWithMetaInfo(
|
| + parent, index, title, &meta_info);
|
| +}
|
|
|
| - DCHECK(it->second.length());
|
| - return it->second;
|
| +// Adds a url at the specified position.
|
| +const BookmarkNode* EnhancedBookmarkModel::AddURL(
|
| + const BookmarkNode* parent,
|
| + int index,
|
| + const base::string16& title,
|
| + const GURL& url,
|
| + const base::Time& creation_time) {
|
| + BookmarkNode::MetaInfoMap meta_info;
|
| + meta_info[kIdKey] = GenerateRemoteId(false);
|
| +
|
| + // TODO(rfevang): Set meta info placement fields.
|
| + return bookmark_model_->AddURLWithCreationTimeAndMetaInfo(
|
| + parent, index, title, url, creation_time, &meta_info);
|
| }
|
|
|
| -void SetDescriptionForBookmark(BookmarkModel* bookmark_model,
|
| - const BookmarkNode* node,
|
| - const std::string& description) {
|
| - bookmark_model->SetNodeMetaInfo(node, kNoteKey, description);
|
| +std::string EnhancedBookmarkModel::GetRemoteId(const BookmarkNode* node) {
|
| + if (node == bookmark_model_->bookmark_bar_node())
|
| + return kBookmarkBarId;
|
| +
|
| + // Permanent nodes other than the bookmarks bar don't have ids.
|
| + DCHECK(!bookmark_model_->is_permanent_node(node));
|
| +
|
| + std::string id;
|
| + if (!node->GetMetaInfo(kIdKey, &id) || id.empty())
|
| + return SetRemoteId(node);
|
| + return id;
|
| }
|
|
|
| -std::string DescriptionFromBookmark(const BookmarkNode* node) {
|
| - const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
|
| - if (!map)
|
| - return "";
|
| +std::string EnhancedBookmarkModel::SetRemoteId(const BookmarkNode* node) {
|
| + std::string remote_id = GenerateRemoteId(node->is_folder());
|
| + SetMetaInfo(node, kIdKey, remote_id, false);
|
| + return remote_id;
|
| +}
|
|
|
| +void EnhancedBookmarkModel::SetDescription(const BookmarkNode* node,
|
| + const std::string& description) {
|
| + SetMetaInfo(node, kNoteKey, description, true);
|
| +}
|
| +
|
| +std::string EnhancedBookmarkModel::GetDescription(const BookmarkNode* node) {
|
| // First, look for a custom note set by the user.
|
| - BookmarkNode::MetaInfoMap::const_iterator it = map->find(kNoteKey);
|
| - if (it != map->end() && it->second != "")
|
| - return it->second;
|
| + std::string description;
|
| + if (node->GetMetaInfo(kNoteKey, &description) && !description.empty())
|
| + return description;
|
|
|
| // If none are present, return the snippet.
|
| - return SnippetFromBookmark(node);
|
| + return GetSnippet(node);
|
| }
|
|
|
| -bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model,
|
| - const BookmarkNode* node,
|
| - const GURL& url,
|
| - int width,
|
| - int height) {
|
| +bool EnhancedBookmarkModel::SetOriginalImage(const BookmarkNode* node,
|
| + const GURL& url,
|
| + int width,
|
| + int height) {
|
| + DCHECK(node->is_url());
|
| DCHECK(url.is_valid());
|
|
|
| std::string decoded(DataForMetaInfoField(node, kImageDataKey));
|
| - ImageData data;
|
| + image::collections::ImageData data;
|
|
|
| // Try to populate the imageData with the existing data.
|
| if (decoded != "") {
|
| @@ -127,7 +175,8 @@ bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model,
|
| return false;
|
| }
|
|
|
| - scoped_ptr<ImageData_ImageInfo> info(new ImageData_ImageInfo);
|
| + scoped_ptr<image::collections::ImageData_ImageInfo> info(
|
| + new image::collections::ImageData_ImageInfo);
|
| info->set_url(url.spec());
|
| info->set_width(width);
|
| info->set_height(height);
|
| @@ -140,21 +189,21 @@ bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model,
|
|
|
| std::string encoded;
|
| base::Base64Encode(output, &encoded);
|
| - bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
|
| + SetMetaInfo(node, kImageDataKey, encoded, true);
|
| // Ensure that the bookmark has a stars.id, to trigger the server processing.
|
| - RemoteIdFromBookmark(bookmark_model, node);
|
| + GetRemoteId(node);
|
| return true;
|
| }
|
|
|
| -bool OriginalImageFromBookmark(const BookmarkNode* node,
|
| - GURL* url,
|
| - int* width,
|
| - int* height) {
|
| +bool EnhancedBookmarkModel::GetOriginalImage(const BookmarkNode* node,
|
| + GURL* url,
|
| + int* width,
|
| + int* height) {
|
| std::string decoded(DataForMetaInfoField(node, kImageDataKey));
|
| if (decoded == "")
|
| return false;
|
|
|
| - ImageData data;
|
| + image::collections::ImageData data;
|
| bool result = data.ParseFromString(decoded);
|
| if (!result)
|
| return false;
|
| @@ -165,15 +214,15 @@ bool OriginalImageFromBookmark(const BookmarkNode* node,
|
| return PopulateImageData(data.original_info(), url, width, height);
|
| }
|
|
|
| -bool ThumbnailImageFromBookmark(const BookmarkNode* node,
|
| - GURL* url,
|
| - int* width,
|
| - int* height) {
|
| +bool EnhancedBookmarkModel::GetThumbnailImage(const BookmarkNode* node,
|
| + GURL* url,
|
| + int* width,
|
| + int* height) {
|
| std::string decoded(DataForMetaInfoField(node, kImageDataKey));
|
| if (decoded == "")
|
| return false;
|
|
|
| - ImageData data;
|
| + image::collections::ImageData data;
|
| bool result = data.ParseFromString(decoded);
|
| if (!result)
|
| return false;
|
| @@ -184,31 +233,64 @@ bool ThumbnailImageFromBookmark(const BookmarkNode* node,
|
| return PopulateImageData(data.thumbnail_info(), url, width, height);
|
| }
|
|
|
| -std::string SnippetFromBookmark(const BookmarkNode* node) {
|
| +std::string EnhancedBookmarkModel::GetSnippet(const BookmarkNode* node) {
|
| std::string decoded(DataForMetaInfoField(node, kPageDataKey));
|
| - if (decoded == "")
|
| + if (decoded.empty())
|
| return decoded;
|
|
|
| - PageData data;
|
| + image::collections::PageData data;
|
| bool result = data.ParseFromString(decoded);
|
| if (!result)
|
| - return "";
|
| + return std::string();
|
|
|
| return data.snippet();
|
| }
|
|
|
| -bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
|
| - const BookmarkNode* node,
|
| - const GURL& image_url,
|
| - int image_width,
|
| - int image_height,
|
| - const GURL& thumbnail_url,
|
| - int thumbnail_width,
|
| - int thumbnail_height) {
|
| +void EnhancedBookmarkModel::SetVersionSuffix(
|
| + const std::string& version_suffix) {
|
| + version_suffix_ = version_suffix;
|
| +}
|
| +
|
| +void EnhancedBookmarkModel::SetMetaInfo(const BookmarkNode* node,
|
| + const std::string& field,
|
| + const std::string& value,
|
| + bool user_edit) {
|
| + DCHECK(!bookmark_model_->is_permanent_node(node));
|
| +
|
| + BookmarkNode::MetaInfoMap meta_info;
|
| + const BookmarkNode::MetaInfoMap* old_meta_info = node->GetMetaInfoMap();
|
| + if (old_meta_info)
|
| + meta_info.insert(old_meta_info->begin(), old_meta_info->end());
|
| +
|
| + // Don't update anything if the value to set is already there.
|
| + BookmarkNode::MetaInfoMap::iterator it = meta_info.find(field);
|
| + if (it != meta_info.end() && it->second == value)
|
| + return;
|
| +
|
| + meta_info[field] = value;
|
| + meta_info[kVersionKey] = GetVersionString();
|
| + meta_info[kUserEditKey] = user_edit ? "true" : "false";
|
| + bookmark_model_->SetNodeMetaInfoMap(node, meta_info);
|
| +}
|
| +
|
| +std::string EnhancedBookmarkModel::GetVersionString() {
|
| + if (version_suffix_.empty())
|
| + return version_;
|
| + return version_ + '/' + version_suffix_;
|
| +}
|
| +
|
| +bool EnhancedBookmarkModel::SetAllImages(const BookmarkNode* node,
|
| + const GURL& image_url,
|
| + int image_width,
|
| + int image_height,
|
| + const GURL& thumbnail_url,
|
| + int thumbnail_width,
|
| + int thumbnail_height) {
|
| + DCHECK(node->is_url());
|
| DCHECK(image_url.is_valid() || image_url.is_empty());
|
| DCHECK(thumbnail_url.is_valid() || thumbnail_url.is_empty());
|
| std::string decoded(DataForMetaInfoField(node, kImageDataKey));
|
| - ImageData data;
|
| + image::collections::ImageData data;
|
|
|
| // Try to populate the imageData with the existing data.
|
| if (decoded != "") {
|
| @@ -223,7 +305,8 @@ bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
|
| } else {
|
| // Regardless of whether an image info exists, we make a new one.
|
| // Intentially make a raw pointer.
|
| - ImageData_ImageInfo* info = new ImageData_ImageInfo;
|
| + image::collections::ImageData_ImageInfo* info =
|
| + new image::collections::ImageData_ImageInfo;
|
| info->set_url(image_url.spec());
|
| info->set_width(image_width);
|
| info->set_height(image_height);
|
| @@ -236,7 +319,8 @@ bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
|
| } else {
|
| // Regardless of whether an image info exists, we make a new one.
|
| // Intentially make a raw pointer.
|
| - ImageData_ImageInfo* info = new ImageData_ImageInfo;
|
| + image::collections::ImageData_ImageInfo* info =
|
| + new image::collections::ImageData_ImageInfo;
|
| info->set_url(thumbnail_url.spec());
|
| info->set_width(thumbnail_width);
|
| info->set_height(thumbnail_height);
|
| @@ -250,7 +334,7 @@ bool SetAllImagesForBookmark(BookmarkModel* bookmark_model,
|
|
|
| std::string encoded;
|
| base::Base64Encode(output, &encoded);
|
| - bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
|
| + bookmark_model_->SetNodeMetaInfo(node, kImageDataKey, encoded);
|
| return true;
|
| }
|
|
|
|
|