| 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/enhanced_bookmarks/enhanced_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 using bookmarks::BookmarkNode; | |
| 14 | |
| 15 namespace { | |
| 16 const char kSearchUrl[] = "https://www.google.com/stars/search"; | |
| 17 const int kSearchCacheMaxSize = 50; | |
| 18 } // namespace | |
| 19 | |
| 20 namespace enhanced_bookmarks { | |
| 21 | |
| 22 BookmarkServerSearchService::BookmarkServerSearchService( | |
| 23 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | |
| 24 ProfileOAuth2TokenService* token_service, | |
| 25 SigninManagerBase* signin_manager, | |
| 26 EnhancedBookmarkModel* enhanced_bookmark_model) | |
| 27 : BookmarkServerService(request_context_getter, | |
| 28 token_service, | |
| 29 signin_manager, | |
| 30 enhanced_bookmark_model), | |
| 31 cache_(kSearchCacheMaxSize) { | |
| 32 } | |
| 33 | |
| 34 BookmarkServerSearchService::~BookmarkServerSearchService() { | |
| 35 } | |
| 36 | |
| 37 void BookmarkServerSearchService::Search(const std::string& query) { | |
| 38 DCHECK(query.length()); | |
| 39 if (current_query_ == query) | |
| 40 return; | |
| 41 | |
| 42 // If result is already stored in cache, immediately notify observers. | |
| 43 if (cache_.Get(current_query_) != cache_.end()) { | |
| 44 Cancel(); | |
| 45 Notify(); | |
| 46 return; | |
| 47 } | |
| 48 current_query_ = query; | |
| 49 TriggerTokenRequest(true); | |
| 50 } | |
| 51 | |
| 52 scoped_ptr<std::vector<const BookmarkNode*>> | |
| 53 BookmarkServerSearchService::ResultForQuery(const std::string& query) { | |
| 54 DCHECK(query.length()); | |
| 55 scoped_ptr<std::vector<const BookmarkNode*>> result; | |
| 56 | |
| 57 const auto& it = cache_.Get(query); | |
| 58 if (it == cache_.end()) | |
| 59 return result; | |
| 60 | |
| 61 result.reset(new std::vector<const BookmarkNode*>()); | |
| 62 | |
| 63 for (const std::string& clip_id : it->second) { | |
| 64 const BookmarkNode* node = BookmarkForRemoteId(clip_id); | |
| 65 if (node) | |
| 66 result->push_back(node); | |
| 67 } | |
| 68 return result; | |
| 69 } | |
| 70 | |
| 71 scoped_ptr<net::URLFetcher> BookmarkServerSearchService::CreateFetcher() { | |
| 72 // Add the necessary arguments to the URI. | |
| 73 GURL url(kSearchUrl); | |
| 74 url = net::AppendQueryParameter(url, "output", "proto"); | |
| 75 url = net::AppendQueryParameter(url, "q", current_query_); | |
| 76 url = net::AppendQueryParameter(url, "v", model_->GetVersionString()); | |
| 77 | |
| 78 // Build the URLFetcher to perform the request. | |
| 79 scoped_ptr<net::URLFetcher> url_fetcher = | |
| 80 net::URLFetcher::Create(url, net::URLFetcher::GET, this); | |
| 81 | |
| 82 return url_fetcher; | |
| 83 } | |
| 84 | |
| 85 bool BookmarkServerSearchService::ProcessResponse(const std::string& response, | |
| 86 bool* should_notify) { | |
| 87 DCHECK(*should_notify); | |
| 88 DCHECK(current_query_.length()); | |
| 89 image::collections::CorpusSearchResult response_proto; | |
| 90 bool result = response_proto.ParseFromString(response); | |
| 91 if (!result) | |
| 92 return false; // Not formatted properly. | |
| 93 | |
| 94 std::vector<std::string> clip_ids; | |
| 95 for (const image::collections::CorpusSearchResult_ClipResult& clip_result : | |
| 96 response_proto.results()) { | |
| 97 const std::string& clip_id = clip_result.clip_id(); | |
| 98 if (!clip_id.length()) | |
| 99 continue; | |
| 100 clip_ids.push_back(clip_id); | |
| 101 } | |
| 102 cache_.Put(current_query_, clip_ids); | |
| 103 current_query_.clear(); | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 void BookmarkServerSearchService::CleanAfterFailure() { | |
| 108 cache_.Clear(); | |
| 109 current_query_.clear(); | |
| 110 } | |
| 111 | |
| 112 void BookmarkServerSearchService::EnhancedBookmarkAdded( | |
| 113 const BookmarkNode* node) { | |
| 114 cache_.Clear(); | |
| 115 } | |
| 116 | |
| 117 void BookmarkServerSearchService::EnhancedBookmarkAllUserNodesRemoved() { | |
| 118 cache_.Clear(); | |
| 119 } | |
| 120 | |
| 121 void BookmarkServerSearchService::EnhancedBookmarkRemoteIdChanged( | |
| 122 const BookmarkNode* node, | |
| 123 const std::string& old_remote_id, | |
| 124 const std::string& remote_id) { | |
| 125 cache_.Clear(); | |
| 126 } | |
| 127 } // namespace enhanced_bookmarks | |
| OLD | NEW |