Commit e244e1ae authored by noyau@chromium.org's avatar noyau@chromium.org

Bring up of the metadata accessors for enhanced bookmarks.

BUG=None

Review URL: https://codereview.chromium.org/336263003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278656 0039d316-1c4b-4281-b951-d872f2087c98
parent cd0ebe9f
...@@ -101,6 +101,7 @@ ...@@ -101,6 +101,7 @@
'domain_reliability/util_unittest.cc', 'domain_reliability/util_unittest.cc',
'enhanced_bookmarks/image_store_ios_unittest.mm', 'enhanced_bookmarks/image_store_ios_unittest.mm',
'enhanced_bookmarks/image_store_unittest.cc', 'enhanced_bookmarks/image_store_unittest.cc',
'enhanced_bookmarks/metadata_accessor_unittest.cc',
'feedback/feedback_common_unittest.cc', 'feedback/feedback_common_unittest.cc',
'feedback/feedback_data_unittest.cc', 'feedback/feedback_data_unittest.cc',
'feedback/feedback_uploader_unittest.cc', 'feedback/feedback_uploader_unittest.cc',
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
'../sql/sql.gyp:sql', '../sql/sql.gyp:sql',
'../ui/gfx/gfx.gyp:gfx', '../ui/gfx/gfx.gyp:gfx',
'../url/url.gyp:url_lib', '../url/url.gyp:url_lib',
'enhanced_bookmarks_proto',
], ],
'sources': [ 'sources': [
'enhanced_bookmarks/image_store.cc', 'enhanced_bookmarks/image_store.cc',
...@@ -22,6 +23,8 @@ ...@@ -22,6 +23,8 @@
'enhanced_bookmarks/image_store_util.cc', 'enhanced_bookmarks/image_store_util.cc',
'enhanced_bookmarks/image_store_util.h', 'enhanced_bookmarks/image_store_util.h',
'enhanced_bookmarks/image_store_util_ios.mm', 'enhanced_bookmarks/image_store_util_ios.mm',
'enhanced_bookmarks/metadata_accessor.cc',
'enhanced_bookmarks/metadata_accessor.h',
'enhanced_bookmarks/persistent_image_store.cc', 'enhanced_bookmarks/persistent_image_store.cc',
'enhanced_bookmarks/persistent_image_store.h', 'enhanced_bookmarks/persistent_image_store.h',
], ],
...@@ -48,5 +51,17 @@ ...@@ -48,5 +51,17 @@
'enhanced_bookmarks/test_image_store.h', 'enhanced_bookmarks/test_image_store.h',
], ],
}, },
{
'target_name': 'enhanced_bookmarks_proto',
'type': 'static_library',
'sources': [
'enhanced_bookmarks/proto/metadata.proto',
],
'variables': {
'proto_in_dir': './enhanced_bookmarks/proto',
'proto_out_dir': 'components/enhanced_bookmarks/proto',
},
'includes': [ '../build/protoc.gypi' ],
},
], ],
} }
// Copyright 2014 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 "components/enhanced_bookmarks/metadata_accessor.h"
#include <iomanip>
#include "base/base64.h"
#include "base/rand_util.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/enhanced_bookmarks/proto/metadata.pb.h"
#include "ui/base/models/tree_node_iterator.h"
using namespace image::collections;
namespace {
// 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 decoded;
bool result = base::Base64Decode((*it).second, &decoded);
if (!result)
return "";
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,
GURL* out_url,
int* width,
int* height) {
if (!info.has_url() || !info.has_width() || !info.has_height())
return false;
GURL url(info.url());
if (!url.is_valid())
return false;
*out_url = url;
*width = info.width();
*height = info.height();
return true;
}
} // namespace
namespace enhanced_bookmarks {
const char* kPageDataKey = "stars.pageData";
const char* kImageDataKey = "stars.imageData";
const char* kIdDataKey = "stars.id";
const char* kNoteKey = "stars.note";
std::string RemoteIdFromBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node) {
const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
if (!map)
return SetRemoteIdOnBookmark(bookmark_model, node);
BookmarkNode::MetaInfoMap::const_iterator it = map->find(kIdDataKey);
if (it == map->end())
return SetRemoteIdOnBookmark(bookmark_model, node);
DCHECK(it->second.length());
return it->second;
}
void SetDescriptionForBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node,
const std::string& description) {
bookmark_model->SetNodeMetaInfo(node, kNoteKey, description);
}
std::string DescriptionFromBookmark(const BookmarkNode* node) {
const BookmarkNode::MetaInfoMap* map = node->GetMetaInfoMap();
if (!map)
return "";
// 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;
// If none are present, return the snippet.
return SnippetFromBookmark(node);
}
bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node,
const GURL& url,
int width,
int height) {
DCHECK(url.is_valid());
std::string decoded(DataForMetaInfoField(node, kImageDataKey));
ImageData data;
// Try to populate the imageData with the existing data.
if (decoded != "") {
// If the parsing fails, something is wrong. Immediately fail.
bool result = data.ParseFromString(decoded);
if (!result)
return false;
}
scoped_ptr<ImageData_ImageInfo> info(new ImageData_ImageInfo);
info->set_url(url.spec());
info->set_width(width);
info->set_height(height);
data.set_allocated_original_info(info.release());
std::string output;
bool result = data.SerializePartialToString(&output);
if (!result)
return false;
std::string encoded;
base::Base64Encode(output, &encoded);
bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
// Ensure that the bookmark has a stars.id, to trigger the server processing.
RemoteIdFromBookmark(bookmark_model, node);
return true;
}
bool OriginalImageFromBookmark(const BookmarkNode* node,
GURL* url,
int* width,
int* height) {
std::string decoded(DataForMetaInfoField(node, kImageDataKey));
if (decoded == "")
return false;
ImageData data;
bool result = data.ParseFromString(decoded);
if (!result)
return false;
if (!data.has_original_info())
return false;
return PopulateImageData(data.original_info(), url, width, height);
}
bool ThumbnailImageFromBookmark(const BookmarkNode* node,
GURL* url,
int* width,
int* height) {
std::string decoded(DataForMetaInfoField(node, kImageDataKey));
if (decoded == "")
return false;
ImageData data;
bool result = data.ParseFromString(decoded);
if (!result)
return false;
if (!data.has_thumbnail_info())
return false;
return PopulateImageData(data.thumbnail_info(), url, width, height);
}
std::string SnippetFromBookmark(const BookmarkNode* node) {
std::string decoded(DataForMetaInfoField(node, kPageDataKey));
if (decoded == "")
return decoded;
PageData data;
bool result = data.ParseFromString(decoded);
if (!result)
return "";
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) {
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;
// Try to populate the imageData with the existing data.
if (decoded != "") {
// If the parsing fails, something is wrong. Immediately fail.
bool result = data.ParseFromString(decoded);
if (!result)
return false;
}
if (image_url.is_empty()) {
data.release_original_info();
} else {
// Regardless of whether an image info exists, we make a new one.
// Intentially make a raw pointer.
ImageData_ImageInfo* info = new ImageData_ImageInfo;
info->set_url(image_url.spec());
info->set_width(image_width);
info->set_height(image_height);
// This method consumes the raw pointer.
data.set_allocated_original_info(info);
}
if (thumbnail_url.is_empty()) {
data.release_thumbnail_info();
} else {
// Regardless of whether an image info exists, we make a new one.
// Intentially make a raw pointer.
ImageData_ImageInfo* info = new ImageData_ImageInfo;
info->set_url(thumbnail_url.spec());
info->set_width(thumbnail_width);
info->set_height(thumbnail_height);
// This method consumes the raw pointer.
data.set_allocated_thumbnail_info(info);
}
std::string output;
bool result = data.SerializePartialToString(&output);
if (!result)
return false;
std::string encoded;
base::Base64Encode(output, &encoded);
bookmark_model->SetNodeMetaInfo(node, kImageDataKey, encoded);
return true;
}
} // namespace enhanced_bookmarks
// Copyright 2014 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.
#ifndef COMPONENTS_ENHANCED_BOOKMARKS_METADATA_ACCESSOR_H_
#define COMPONENTS_ENHANCED_BOOKMARKS_METADATA_ACCESSOR_H_
#include <set>
#include <string>
#include <vector>
class BookmarkModel;
class BookmarkNode;
class GURL;
// The functions in this file store and retrieve structured data encoded in the
// bookmark metadata. This information suplements the data in the bookmark with
// images and descriptions related to the url.
namespace enhanced_bookmarks {
typedef std::vector<const BookmarkNode*> NodeVector;
typedef std::set<const BookmarkNode*> NodeSet;
// The keys used to store the data in the bookmarks metadata dictionary.
extern const char* kPageDataKey;
extern const char* kImageDataKey;
extern const char* kIdDataKey;
extern const char* kNoteKey;
// Returns the remoteId for a bookmark. If the bookmark doesn't have one already
// this function will create and set one.
std::string RemoteIdFromBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node);
// Sets the description of a bookmark.
void SetDescriptionForBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node,
const std::string& description);
// Returns the description of a bookmark.
std::string DescriptionFromBookmark(const BookmarkNode* node);
// Sets the URL of an image representative of the page.
// Expects the URL to be valid and not empty.
// Returns true if the metainfo is successfully populated.
bool SetOriginalImageForBookmark(BookmarkModel* bookmark_model,
const BookmarkNode* node,
const GURL& url,
int width,
int height);
// Returns the url and dimensions of the original scraped image.
// Returns true if the out variables are populated, false otherwise.
bool OriginalImageFromBookmark(const BookmarkNode* node,
GURL* url,
int* width,
int* height);
// Returns the url and dimensions of the server provided thumbnail image.
// Returns true if the out variables are populated, false otherwise.
bool ThumbnailImageFromBookmark(const BookmarkNode* node,
GURL* url,
int* width,
int* height);
// Returns a brief server provided synopsis of the bookmarked page.
// Returns the empty string if the snippet could not be extracted.
std::string SnippetFromBookmark(const BookmarkNode* node);
// Used for testing, simulates the process that creates the thumnails. Will
// remove existing entries for empty urls or set them if the url is not empty.
// expects valid or empty urls. Returns true if the metainfo is successfully
// populated.
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);
} // namespace enhanced_bookmarks
#endif // COMPONENTS_ENHANCED_BOOKMARKS_METADATA_ACCESSOR_H_
This diff is collapsed.
// Copyright 2014 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.
//
syntax = "proto2";
option optimize_for = LITE_RUNTIME;
package image.collections;
message ImageData {
// Encrypted 64-bit image doc id, if it has been crawled,
// e.g. "kY7_4LKgNqDrbM:"
optional string doc_id = 1;
message ImageInfo {
// The (normalized) URL this image can be found at.
optional string url = 1;
// The dimensions in pixels.
optional int32 width = 2;
optional int32 height = 3;
}
// Information about the original collected image.
optional ImageInfo original_info = 2;
// Information about the server hosted thumbnail.
optional ImageInfo thumbnail_info = 3;
}
message PageData {
// The title of the web page.
optional string title = 1;
// A snippet of text from the web page, either computed by us or chosen by
// the user.
optional string snippet = 2;
// The (normalized) URL of the web page.
optional string url = 3;
// The /url redirect signed URL for the web page. This could be appended to
// "www.google.com" to create a URL redirect.
optional string signed_url = 5;
// The doc id of the page, if in the index. Uses the same encrypted docid
// format as ImageData.
optional string doc_id = 4;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment