Cast: Add an option to turn on non-blocking IO on Windows
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_server_search_service.cc
blob063fa38895fb63ca5a5895910f833d7d5ad80cc9
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.
5 #include "components/enhanced_bookmarks/bookmark_server_search_service.h"
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"
13 using bookmarks::BookmarkNode;
15 namespace {
16 const char kSearchUrl[] = "https://www.google.com/stars/search";
17 const int kSearchCacheMaxSize = 50;
18 } // namespace
20 namespace enhanced_bookmarks {
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) {
34 BookmarkServerSearchService::~BookmarkServerSearchService() {
37 void BookmarkServerSearchService::Search(const std::string& query) {
38 DCHECK(query.length());
39 if (current_query_ == query)
40 return;
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;
48 current_query_ = query;
49 TriggerTokenRequest(true);
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;
57 const auto& it = cache_.Get(query);
58 if (it == cache_.end())
59 return result;
61 result.reset(new std::vector<const BookmarkNode*>());
63 for (const std::string& clip_id : it->second) {
64 const BookmarkNode* node = BookmarkForRemoteId(clip_id);
65 if (node)
66 result->push_back(node);
68 return result;
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());
78 // Build the URLFetcher to perform the request.
79 scoped_ptr<net::URLFetcher> url_fetcher(
80 net::URLFetcher::Create(url, net::URLFetcher::GET, this));
82 return url_fetcher;
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.
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);
102 cache_.Put(current_query_, clip_ids);
103 current_query_.clear();
104 return true;
107 void BookmarkServerSearchService::CleanAfterFailure() {
108 cache_.Clear();
109 current_query_.clear();
112 void BookmarkServerSearchService::EnhancedBookmarkAdded(
113 const BookmarkNode* node) {
114 cache_.Clear();
117 void BookmarkServerSearchService::EnhancedBookmarkAllUserNodesRemoved() {
118 cache_.Clear();
121 void BookmarkServerSearchService::EnhancedBookmarkRemoteIdChanged(
122 const BookmarkNode* node,
123 const std::string& old_remote_id,
124 const std::string& remote_id) {
125 cache_.Clear();
127 } // namespace enhanced_bookmarks