OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/enhanced_bookmarks/bookmark_server_search_service.h" |
| 6 |
| 7 #include "components/bookmarks/browser/bookmark_model.h" |
| 8 #include "components/enhanced_bookmarks/enhanced_bookmark_utils.h" |
| 9 #include "components/enhanced_bookmarks/proto/search.pb.h" |
| 10 #include "net/base/url_util.h" |
| 11 #include "net/url_request/url_fetcher.h" |
| 12 |
| 13 namespace { |
| 14 const std::string kSearchUrl( |
| 15 "https://www.google.com/stars/search"); |
| 16 } // namespace |
| 17 |
| 18 namespace enhanced_bookmarks { |
| 19 |
| 20 BookmarkServerSearchService::BookmarkServerSearchService( |
| 21 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 22 ProfileOAuth2TokenService* token_service, |
| 23 SigninManagerBase* signin_manager, |
| 24 BookmarkModel* bookmark_model) |
| 25 : BookmarkServerService(request_context_getter, |
| 26 token_service, |
| 27 signin_manager, |
| 28 bookmark_model) { |
| 29 } |
| 30 |
| 31 BookmarkServerSearchService::~BookmarkServerSearchService() { |
| 32 } |
| 33 |
| 34 void BookmarkServerSearchService::Search(const std::string& query) { |
| 35 DCHECK(query.length()); |
| 36 current_query_ = query; |
| 37 TriggerTokenRequest(true); |
| 38 } |
| 39 |
| 40 std::vector<const BookmarkNode*> BookmarkServerSearchService::ResultForQuery( |
| 41 const std::string& query) { |
| 42 DCHECK(query.length()); |
| 43 std::vector<const BookmarkNode*> result; |
| 44 |
| 45 std::map<std::string, std::vector<std::string> >::iterator it = |
| 46 searches_.find(query); |
| 47 if (it == searches_.end()) |
| 48 return result; |
| 49 |
| 50 for (std::vector<std::string>::iterator clip_it = it->second.begin(); |
| 51 clip_it != it->second.end(); |
| 52 ++clip_it) { |
| 53 const BookmarkNode* node = BookmarkForRemoteId(*clip_it); |
| 54 if (node) |
| 55 result.push_back(node); |
| 56 } |
| 57 return result; |
| 58 } |
| 59 |
| 60 net::URLFetcher* BookmarkServerSearchService::CreateFetcher() { |
| 61 // Add the necessary arguments to the URI. |
| 62 GURL url(kSearchUrl); |
| 63 url = net::AppendQueryParameter(url, "output", "proto"); |
| 64 url = net::AppendQueryParameter(url, "q", current_query_); |
| 65 |
| 66 // Build the URLFetcher to perform the request. |
| 67 net::URLFetcher* url_fetcher = |
| 68 net::URLFetcher::Create(url, net::URLFetcher::GET, this); |
| 69 |
| 70 return url_fetcher; |
| 71 } |
| 72 |
| 73 bool BookmarkServerSearchService::ProcessResponse(const std::string& response, |
| 74 bool* should_notify) { |
| 75 DCHECK(*should_notify); |
| 76 DCHECK(current_query_.length()); |
| 77 image::collections::CorpusSearchResult response_proto; |
| 78 bool result = response_proto.ParseFromString(response); |
| 79 if (!result) |
| 80 return false; // Not formatted properly. |
| 81 |
| 82 std::vector<std::string> clip_ids; |
| 83 for (google::protobuf::RepeatedPtrField< |
| 84 image::collections::CorpusSearchResult_ClipResult>::const_iterator |
| 85 it = response_proto.results().begin(); |
| 86 it != response_proto.results().end(); |
| 87 ++it) { |
| 88 const std::string& clip_id = it->clip_id(); |
| 89 if (!clip_id.length()) |
| 90 continue; |
| 91 clip_ids.push_back(clip_id); |
| 92 } |
| 93 searches_[current_query_] = clip_ids; |
| 94 current_query_.clear(); |
| 95 return true; |
| 96 } |
| 97 |
| 98 void BookmarkServerSearchService::CleanAfterFailure() { |
| 99 searches_.clear(); |
| 100 } |
| 101 |
| 102 void BookmarkServerSearchService::BookmarkNodeAdded(BookmarkModel* model, |
| 103 const BookmarkNode* parent, |
| 104 int index) { |
| 105 BookmarkServerService::BookmarkNodeAdded(model, parent, index); |
| 106 searches_.clear(); |
| 107 } |
| 108 |
| 109 void BookmarkServerSearchService::BookmarkMetaInfoChanged( |
| 110 BookmarkModel* model, |
| 111 const BookmarkNode* node) { |
| 112 BookmarkServerService::BookmarkMetaInfoChanged(model, node); |
| 113 searches_.clear(); |
| 114 } |
| 115 } // namespace enhanced_bookmarks |
OLD | NEW |