Only use web app style header for hosted apps.
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_server_search_service.cc
blobe1ab78eeef443f3b134520d18854662bb8e1e4b7
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 namespace {
14 const char kSearchUrl[] = "https://www.google.com/stars/search";
15 const int kSearchCacheMaxSize = 50;
16 } // namespace
18 namespace enhanced_bookmarks {
20 BookmarkServerSearchService::BookmarkServerSearchService(
21 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
22 ProfileOAuth2TokenService* token_service,
23 SigninManagerBase* signin_manager,
24 EnhancedBookmarkModel* enhanced_bookmark_model)
25 : BookmarkServerService(request_context_getter,
26 token_service,
27 signin_manager,
28 enhanced_bookmark_model),
29 cache_(kSearchCacheMaxSize) {
32 BookmarkServerSearchService::~BookmarkServerSearchService() {
35 void BookmarkServerSearchService::Search(const std::string& query) {
36 DCHECK(query.length());
37 if (current_query_ == query)
38 return;
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;
46 current_query_ = query;
47 TriggerTokenRequest(true);
50 scoped_ptr<std::vector<const BookmarkNode*>>
51 BookmarkServerSearchService::ResultForQuery(const std::string& query) {
52 DCHECK(query.length());
53 scoped_ptr<std::vector<const BookmarkNode*>> result;
55 const auto& it = cache_.Get(query);
56 if (it == cache_.end())
57 return result;
59 result.reset(new std::vector<const BookmarkNode*>());
61 for (const std::string& clip_id : it->second) {
62 const BookmarkNode* node = BookmarkForRemoteId(clip_id);
63 if (node)
64 result->push_back(node);
66 return result;
69 scoped_ptr<net::URLFetcher> BookmarkServerSearchService::CreateFetcher() {
70 // Add the necessary arguments to the URI.
71 GURL url(kSearchUrl);
72 url = net::AppendQueryParameter(url, "output", "proto");
73 url = net::AppendQueryParameter(url, "q", current_query_);
74 url = net::AppendQueryParameter(url, "v", model_->GetVersionString());
76 // Build the URLFetcher to perform the request.
77 scoped_ptr<net::URLFetcher> url_fetcher(
78 net::URLFetcher::Create(url, net::URLFetcher::GET, this));
80 return url_fetcher;
83 bool BookmarkServerSearchService::ProcessResponse(const std::string& response,
84 bool* should_notify) {
85 DCHECK(*should_notify);
86 DCHECK(current_query_.length());
87 image::collections::CorpusSearchResult response_proto;
88 bool result = response_proto.ParseFromString(response);
89 if (!result)
90 return false; // Not formatted properly.
92 std::vector<std::string> clip_ids;
93 for (const image::collections::CorpusSearchResult_ClipResult& clip_result :
94 response_proto.results()) {
95 const std::string& clip_id = clip_result.clip_id();
96 if (!clip_id.length())
97 continue;
98 clip_ids.push_back(clip_id);
100 cache_.Put(current_query_, clip_ids);
101 current_query_.clear();
102 return true;
105 void BookmarkServerSearchService::CleanAfterFailure() {
106 cache_.Clear();
107 current_query_.clear();
110 void BookmarkServerSearchService::EnhancedBookmarkAdded(
111 const BookmarkNode* node) {
112 cache_.Clear();
115 void BookmarkServerSearchService::EnhancedBookmarkAllUserNodesRemoved() {
116 cache_.Clear();
119 void BookmarkServerSearchService::EnhancedBookmarkRemoteIdChanged(
120 const BookmarkNode* node,
121 const std::string& old_remote_id,
122 const std::string& remote_id) {
123 cache_.Clear();
125 } // namespace enhanced_bookmarks