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