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/bookmarks/browser/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 namespace { | |
14 const std::string kSearchUrl( | |
15 "https://www.google.com/stars/search"); | |
16 } // namespace | |
17 | |
18 namespace enhanced_bookmarks { | |
19 | |
20 BookmarkServerSearchService::BookmarkServerSearchService( | |
21 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | |
22 ProfileOAuth2TokenService* token_service, | |
23 SigninManagerBase* signin_manager, | |
24 BookmarkModel* bookmark_model) | |
25 : BookmarkServerService(request_context_getter, | |
26 token_service, | |
27 signin_manager, | |
28 bookmark_model) { | |
29 } | |
30 | |
31 BookmarkServerSearchService::~BookmarkServerSearchService() { | |
32 } | |
33 | |
34 void BookmarkServerSearchService::Search(const std::string& query) { | |
35 DCHECK(query.length()); | |
36 current_query_ = query; | |
37 TriggerTokenRequest(true); | |
38 } | |
39 | |
40 std::vector<const BookmarkNode*> BookmarkServerSearchService::ResultForQuery( | |
41 const std::string& query) { | |
42 DCHECK(query.length()); | |
43 std::vector<const BookmarkNode*> result; | |
44 | |
45 std::map<std::string, std::vector<std::string> >::iterator it = | |
46 searches_.find(query); | |
47 if (it == searches_.end()) | |
48 return result; | |
49 | |
50 for (std::vector<std::string>::iterator clip_it = it->second.begin(); | |
51 clip_it != it->second.end(); | |
52 ++clip_it) { | |
53 const BookmarkNode* node = BookmarkForRemoteId(*clip_it); | |
54 if (node) | |
55 result.push_back(node); | |
56 } | |
57 return result; | |
58 } | |
59 | |
60 net::URLFetcher* BookmarkServerSearchService::CreateFetcherWithToken( | |
61 const std::string& access_token) { | |
62 // Add the necessary arguments to the URI. | |
63 GURL url(kSearchUrl); | |
Mark
2014/09/05 18:23:14
I'm assuming the bearer is being set magically som
Yaron
2014/09/05 18:26:52
BookmarkServerService::OnGetTokenSuccess adds it a
noyau (Ping after 24h)
2014/09/08 09:46:48
Ah, good catch. This argument is a remnant of anci
| |
64 url = net::AppendQueryParameter(url, "output", "proto"); | |
65 url = net::AppendQueryParameter(url, "q", current_query_); | |
Mark
2014/09/05 18:23:14
Make sure to add a v=[version id] here.
noyau (Ping after 24h)
2014/09/08 09:46:48
Yup, once the code to get a revision lands. I'll p
| |
66 | |
67 // Build the URLFetcher to perform the request. | |
68 net::URLFetcher* url_fetcher = | |
69 net::URLFetcher::Create(url, net::URLFetcher::GET, this); | |
70 | |
71 return url_fetcher; | |
72 } | |
73 | |
74 bool BookmarkServerSearchService::ProcessResponse(const std::string& response, | |
75 bool* should_notify) { | |
76 DCHECK(*should_notify); | |
Yaron
2014/09/05 04:37:52
this doesn't match the header. It can't actually e
noyau (Ping after 24h)
2014/09/08 09:46:48
For this class should_notify is always true, and t
| |
77 DCHECK(current_query_.length()); | |
78 image::collections::CorpusSearchResult response_proto; | |
79 bool result = response_proto.ParseFromString(response); | |
80 if (!result) | |
81 return false; // Not formatted properly. | |
82 | |
83 std::vector<std::string> clip_ids; | |
84 for (google::protobuf::RepeatedPtrField< | |
85 image::collections::CorpusSearchResult_ClipResult>::const_iterator | |
86 it = response_proto.results().begin(); | |
87 it != response_proto.results().end(); | |
88 ++it) { | |
89 const std::string& clip_id = it->clip_id(); | |
90 if (!clip_id.length()) | |
Mark
2014/09/05 18:23:14
You also get snippets back as to why clips matched
noyau (Ping after 24h)
2014/09/08 09:46:48
Acknowledged.
| |
91 continue; | |
92 clip_ids.push_back(clip_id); | |
93 } | |
94 searches_[current_query_] = clip_ids; | |
95 current_query_.clear(); | |
96 return true; | |
97 } | |
98 | |
99 void BookmarkServerSearchService::CleanAfterFailure() { | |
100 searches_.clear(); | |
101 } | |
102 | |
103 void BookmarkServerSearchService::BookmarkNodeAdded(BookmarkModel* model, | |
Yaron
2014/09/05 04:37:52
Why is a bookmark removal ignorable? I guess the U
noyau (Ping after 24h)
2014/09/08 09:46:48
This is not ignored, this is catched by the superc
| |
104 const BookmarkNode* parent, | |
105 int index) { | |
106 BookmarkServerService::BookmarkNodeAdded(model, parent, index); | |
107 searches_.clear(); | |
108 } | |
109 | |
110 void BookmarkServerSearchService::BookmarkMetaInfoChanged( | |
111 BookmarkModel* model, | |
112 const BookmarkNode* node) { | |
113 BookmarkServerService::BookmarkMetaInfoChanged(model, node); | |
114 searches_.clear(); | |
115 } | |
116 } // namespace enhanced_bookmarks | |
OLD | NEW |