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