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