Index: components/enhanced_bookmarks/bookmark_server_search_service.cc |
diff --git a/components/enhanced_bookmarks/bookmark_server_search_service.cc b/components/enhanced_bookmarks/bookmark_server_search_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e019e4b24b607f4f97f1235bd28f7a99f6c46503 |
--- /dev/null |
+++ b/components/enhanced_bookmarks/bookmark_server_search_service.cc |
@@ -0,0 +1,116 @@ |
+// 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/bookmark_server_search_service.h" |
+ |
+#include "components/bookmarks/browser/bookmark_model.h" |
+#include "components/enhanced_bookmarks/enhanced_bookmark_utils.h" |
+#include "components/enhanced_bookmarks/proto/search.pb.h" |
+#include "net/base/url_util.h" |
+#include "net/url_request/url_fetcher.h" |
+ |
+namespace { |
+const std::string kSearchUrl( |
+ "https://www.google.com/stars/search"); |
+} // namespace |
+ |
+namespace enhanced_bookmarks { |
+ |
+BookmarkServerSearchService::BookmarkServerSearchService( |
+ scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
+ ProfileOAuth2TokenService* token_service, |
+ SigninManagerBase* signin_manager, |
+ BookmarkModel* bookmark_model) |
+ : BookmarkServerService(request_context_getter, |
+ token_service, |
+ signin_manager, |
+ bookmark_model) { |
+} |
+ |
+BookmarkServerSearchService::~BookmarkServerSearchService() { |
+} |
+ |
+void BookmarkServerSearchService::Search(const std::string& query) { |
+ DCHECK(query.length()); |
+ current_query_ = query; |
+ TriggerTokenRequest(true); |
+} |
+ |
+std::vector<const BookmarkNode*> BookmarkServerSearchService::ResultForQuery( |
+ const std::string& query) { |
+ DCHECK(query.length()); |
+ std::vector<const BookmarkNode*> result; |
+ |
+ std::map<std::string, std::vector<std::string> >::iterator it = |
+ searches_.find(query); |
+ if (it == searches_.end()) |
+ return result; |
+ |
+ for (std::vector<std::string>::iterator clip_it = it->second.begin(); |
+ clip_it != it->second.end(); |
+ ++clip_it) { |
+ const BookmarkNode* node = BookmarkForRemoteId(*clip_it); |
+ if (node) |
+ result.push_back(node); |
+ } |
+ return result; |
+} |
+ |
+net::URLFetcher* BookmarkServerSearchService::CreateFetcherWithToken( |
+ const std::string& access_token) { |
+ // Add the necessary arguments to the URI. |
+ GURL url(kSearchUrl); |
Mark
2014/09/05 18:23:14
I'm assuming the bearer is being set magically som
Yaron
2014/09/05 18:26:52
BookmarkServerService::OnGetTokenSuccess adds it a
noyau (Ping after 24h)
2014/09/08 09:46:48
Ah, good catch. This argument is a remnant of anci
|
+ url = net::AppendQueryParameter(url, "output", "proto"); |
+ url = net::AppendQueryParameter(url, "q", current_query_); |
Mark
2014/09/05 18:23:14
Make sure to add a v=[version id] here.
noyau (Ping after 24h)
2014/09/08 09:46:48
Yup, once the code to get a revision lands. I'll p
|
+ |
+ // Build the URLFetcher to perform the request. |
+ net::URLFetcher* url_fetcher = |
+ net::URLFetcher::Create(url, net::URLFetcher::GET, this); |
+ |
+ return url_fetcher; |
+} |
+ |
+bool BookmarkServerSearchService::ProcessResponse(const std::string& response, |
+ bool* should_notify) { |
+ DCHECK(*should_notify); |
Yaron
2014/09/05 04:37:52
this doesn't match the header. It can't actually e
noyau (Ping after 24h)
2014/09/08 09:46:48
For this class should_notify is always true, and t
|
+ DCHECK(current_query_.length()); |
+ image::collections::CorpusSearchResult response_proto; |
+ bool result = response_proto.ParseFromString(response); |
+ if (!result) |
+ return false; // Not formatted properly. |
+ |
+ std::vector<std::string> clip_ids; |
+ for (google::protobuf::RepeatedPtrField< |
+ image::collections::CorpusSearchResult_ClipResult>::const_iterator |
+ it = response_proto.results().begin(); |
+ it != response_proto.results().end(); |
+ ++it) { |
+ const std::string& clip_id = it->clip_id(); |
+ if (!clip_id.length()) |
Mark
2014/09/05 18:23:14
You also get snippets back as to why clips matched
noyau (Ping after 24h)
2014/09/08 09:46:48
Acknowledged.
|
+ continue; |
+ clip_ids.push_back(clip_id); |
+ } |
+ searches_[current_query_] = clip_ids; |
+ current_query_.clear(); |
+ return true; |
+} |
+ |
+void BookmarkServerSearchService::CleanAfterFailure() { |
+ searches_.clear(); |
+} |
+ |
+void BookmarkServerSearchService::BookmarkNodeAdded(BookmarkModel* model, |
Yaron
2014/09/05 04:37:52
Why is a bookmark removal ignorable? I guess the U
noyau (Ping after 24h)
2014/09/08 09:46:48
This is not ignored, this is catched by the superc
|
+ const BookmarkNode* parent, |
+ int index) { |
+ BookmarkServerService::BookmarkNodeAdded(model, parent, index); |
+ searches_.clear(); |
+} |
+ |
+void BookmarkServerSearchService::BookmarkMetaInfoChanged( |
+ BookmarkModel* model, |
+ const BookmarkNode* node) { |
+ BookmarkServerService::BookmarkMetaInfoChanged(model, node); |
+ searches_.clear(); |
+} |
+} // namespace enhanced_bookmarks |