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 kMRUCacheMaxSize = 50; | |
Kibeom Kim (inactive)
2014/10/29 07:51:47
I think it's better to indicate what this variable
Ian Wen
2014/10/29 21:54:36
Done.
| |
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 searches_ = new base::MRUCache<std::string, std::vector<std::string> >( | |
Kibeom Kim (inactive)
2014/10/29 07:51:47
nit: I think now we can use >> instead of > >
noyau (Ping after 24h)
2014/10/29 12:49:48
Better, don't use new and delete at all, make it a
Ian Wen
2014/10/29 21:54:36
LOL "Bestest". Made it not a pointer. Done.
| |
30 kMRUCacheMaxSize); | |
28 } | 31 } |
29 | 32 |
30 BookmarkServerSearchService::~BookmarkServerSearchService() { | 33 BookmarkServerSearchService::~BookmarkServerSearchService() { |
34 delete searches_; | |
31 } | 35 } |
32 | 36 |
33 void BookmarkServerSearchService::Search(const std::string& query) { | 37 void BookmarkServerSearchService::Search(const std::string& query) { |
34 DCHECK(query.length()); | 38 DCHECK(query.length()); |
noyau (Ping after 24h)
2014/10/29 12:49:48
Add
if (query == current_query_)
return;
to pr
Ian Wen
2014/10/29 21:54:36
Done.
| |
35 current_query_ = query; | 39 current_query_ = query; |
noyau (Ping after 24h)
2014/10/29 12:49:48
Move after the if, only set it if a request is to
Ian Wen
2014/10/29 21:54:36
Done.
| |
40 | |
41 // If result is already stored in cache, immediately notify observers. | |
42 if (searches_->Get(current_query_) != searches_->end()) { | |
43 Cancel(); | |
44 Notify(); | |
45 return; | |
46 } | |
36 TriggerTokenRequest(true); | 47 TriggerTokenRequest(true); |
37 } | 48 } |
38 | 49 |
39 std::vector<const BookmarkNode*> BookmarkServerSearchService::ResultForQuery( | 50 std::vector<const BookmarkNode*> BookmarkServerSearchService::ResultForQuery( |
40 const std::string& query) { | 51 const std::string& query) { |
41 DCHECK(query.length()); | 52 DCHECK(query.length()); |
42 std::vector<const BookmarkNode*> result; | 53 std::vector<const BookmarkNode*> result; |
43 | 54 |
44 std::map<std::string, std::vector<std::string> >::iterator it = | 55 base::MRUCache<std::string, std::vector<std::string> >::iterator it = |
noyau (Ping after 24h)
2014/10/29 12:49:48
use auto? At least remove the space between the tw
Ian Wen
2014/10/29 21:54:36
Done.
| |
45 searches_.find(query); | 56 searches_->Get(query); |
46 if (it == searches_.end()) | 57 if (it == searches_->end()) |
47 return result; | 58 return result; |
48 | 59 |
49 for (std::vector<std::string>::iterator clip_it = it->second.begin(); | 60 for (std::vector<std::string>::iterator clip_it = it->second.begin(); |
50 clip_it != it->second.end(); | 61 clip_it != it->second.end(); |
51 ++clip_it) { | 62 ++clip_it) { |
noyau (Ping after 24h)
2014/10/29 12:49:48
for (const std::string& clip_id : it->second)
Ian Wen
2014/10/29 21:54:36
Done.
| |
52 const BookmarkNode* node = BookmarkForRemoteId(*clip_it); | 63 const BookmarkNode* node = BookmarkForRemoteId(*clip_it); |
53 if (node) | 64 if (node) |
54 result.push_back(node); | 65 result.push_back(node); |
55 } | 66 } |
56 return result; | 67 return result; |
57 } | 68 } |
58 | 69 |
70 std::vector<const BookmarkNode*> BookmarkServerSearchService::ResultForQuery() { | |
71 return ResultForQuery(current_query_); | |
72 } | |
73 | |
74 std::string BookmarkServerSearchService::GetCurrentQuery() { | |
75 return current_query_; | |
76 } | |
77 | |
59 scoped_ptr<net::URLFetcher> BookmarkServerSearchService::CreateFetcher() { | 78 scoped_ptr<net::URLFetcher> BookmarkServerSearchService::CreateFetcher() { |
60 // Add the necessary arguments to the URI. | 79 // Add the necessary arguments to the URI. |
61 GURL url(kSearchUrl); | 80 GURL url(kSearchUrl); |
62 url = net::AppendQueryParameter(url, "output", "proto"); | 81 url = net::AppendQueryParameter(url, "output", "proto"); |
63 url = net::AppendQueryParameter(url, "q", current_query_); | 82 url = net::AppendQueryParameter(url, "q", current_query_); |
64 url = net::AppendQueryParameter(url, "v", model_->GetVersionString()); | 83 url = net::AppendQueryParameter(url, "v", model_->GetVersionString()); |
65 | 84 |
66 // Build the URLFetcher to perform the request. | 85 // Build the URLFetcher to perform the request. |
67 scoped_ptr<net::URLFetcher> url_fetcher( | 86 scoped_ptr<net::URLFetcher> url_fetcher( |
68 net::URLFetcher::Create(url, net::URLFetcher::GET, this)); | 87 net::URLFetcher::Create(url, net::URLFetcher::GET, this)); |
69 | 88 |
70 return url_fetcher; | 89 return url_fetcher; |
71 } | 90 } |
72 | 91 |
73 bool BookmarkServerSearchService::ProcessResponse(const std::string& response, | 92 bool BookmarkServerSearchService::ProcessResponse(const std::string& response, |
74 bool* should_notify) { | 93 bool* should_notify) { |
75 DCHECK(*should_notify); | 94 DCHECK(*should_notify); |
76 DCHECK(current_query_.length()); | 95 DCHECK(current_query_.length()); |
77 image::collections::CorpusSearchResult response_proto; | 96 image::collections::CorpusSearchResult response_proto; |
78 bool result = response_proto.ParseFromString(response); | 97 bool result = response_proto.ParseFromString(response); |
79 if (!result) | 98 if (!result) |
80 return false; // Not formatted properly. | 99 return false; // Not formatted properly. |
81 | 100 |
82 std::vector<std::string> clip_ids; | 101 std::vector<std::string> clip_ids; |
83 for (google::protobuf::RepeatedPtrField< | 102 for (google::protobuf::RepeatedPtrField< |
84 image::collections::CorpusSearchResult_ClipResult>::const_iterator | 103 image::collections::CorpusSearchResult_ClipResult>::const_iterator |
85 it = response_proto.results().begin(); | 104 it = response_proto.results().begin(); |
86 it != response_proto.results().end(); | 105 it != response_proto.results().end(); |
87 ++it) { | 106 ++it) { |
88 const std::string& clip_id = it->clip_id(); | 107 const std::string& clip_id = it->clip_id(); |
noyau (Ping after 24h)
2014/10/29 12:49:48
Replace this whole mess by
for (const std::string
Ian Wen
2014/10/29 21:54:36
Done.
| |
89 if (!clip_id.length()) | 108 if (!clip_id.length()) |
90 continue; | 109 continue; |
91 clip_ids.push_back(clip_id); | 110 clip_ids.push_back(clip_id); |
92 } | 111 } |
93 searches_[current_query_] = clip_ids; | 112 searches_->Put(current_query_, clip_ids); |
94 current_query_.clear(); | |
noyau (Ping after 24h)
2014/10/29 12:49:48
Again, I'm not convinced the lifecycle of current_
Ian Wen
2014/10/29 21:54:36
Done. Put it back.
| |
95 return true; | 113 return true; |
96 } | 114 } |
97 | 115 |
98 void BookmarkServerSearchService::CleanAfterFailure() { | 116 void BookmarkServerSearchService::CleanAfterFailure() { |
99 searches_.clear(); | 117 searches_->Clear(); |
noyau (Ping after 24h)
2014/10/29 12:49:48
Bug: This should clear the current_query as well.
Ian Wen
2014/10/29 21:54:36
Done.
| |
100 } | 118 } |
101 | 119 |
102 void BookmarkServerSearchService::EnhancedBookmarkAdded( | 120 void BookmarkServerSearchService::EnhancedBookmarkAdded( |
103 const BookmarkNode* node) { | 121 const BookmarkNode* node) { |
104 searches_.clear(); | 122 searches_->Clear(); |
noyau (Ping after 24h)
2014/10/29 12:49:48
What if there is a query in progress at that point
| |
105 } | 123 } |
106 | 124 |
107 void BookmarkServerSearchService::EnhancedBookmarkAllUserNodesRemoved() { | 125 void BookmarkServerSearchService::EnhancedBookmarkAllUserNodesRemoved() { |
108 searches_.clear(); | 126 searches_->Clear(); |
109 } | 127 } |
110 | 128 |
111 void BookmarkServerSearchService::EnhancedBookmarkRemoteIdChanged( | 129 void BookmarkServerSearchService::EnhancedBookmarkRemoteIdChanged( |
112 const BookmarkNode* node, | 130 const BookmarkNode* node, |
113 const std::string& old_remote_id, | 131 const std::string& old_remote_id, |
114 const std::string& remote_id) { | 132 const std::string& remote_id) { |
115 searches_.clear(); | 133 searches_->Clear(); |
116 } | 134 } |
117 } // namespace enhanced_bookmarks | 135 } // namespace enhanced_bookmarks |
OLD | NEW |