summaryrefslogtreecommitdiffstats
path: root/components/enhanced_bookmarks/bookmark_server_search_service.cc
blob: e1ab78eeef443f3b134520d18854662bb8e1e4b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/enhanced_bookmarks/bookmark_server_search_service.h"

#include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
#include "components/enhanced_bookmarks/enhanced_bookmark_utils.h"
#include "components/enhanced_bookmarks/proto/search.pb.h"
#include "net/base/url_util.h"
#include "net/url_request/url_fetcher.h"

namespace {
const char kSearchUrl[] = "https://www.google.com/stars/search";
const int kSearchCacheMaxSize = 50;
}  // namespace

namespace enhanced_bookmarks {

BookmarkServerSearchService::BookmarkServerSearchService(
    scoped_refptr<net::URLRequestContextGetter> request_context_getter,
    ProfileOAuth2TokenService* token_service,
    SigninManagerBase* signin_manager,
    EnhancedBookmarkModel* enhanced_bookmark_model)
    : BookmarkServerService(request_context_getter,
                            token_service,
                            signin_manager,
                            enhanced_bookmark_model),
      cache_(kSearchCacheMaxSize) {
}

BookmarkServerSearchService::~BookmarkServerSearchService() {
}

void BookmarkServerSearchService::Search(const std::string& query) {
  DCHECK(query.length());
  if (current_query_ == query)
    return;

  // If result is already stored in cache, immediately notify observers.
  if (cache_.Get(current_query_) != cache_.end()) {
    Cancel();
    Notify();
    return;
  }
  current_query_ = query;
  TriggerTokenRequest(true);
}

scoped_ptr<std::vector<const BookmarkNode*>>
BookmarkServerSearchService::ResultForQuery(const std::string& query) {
  DCHECK(query.length());
  scoped_ptr<std::vector<const BookmarkNode*>> result;

  const auto& it = cache_.Get(query);
  if (it == cache_.end())
    return result;

  result.reset(new std::vector<const BookmarkNode*>());

  for (const std::string& clip_id : it->second) {
    const BookmarkNode* node = BookmarkForRemoteId(clip_id);
    if (node)
      result->push_back(node);
  }
  return result;
}

scoped_ptr<net::URLFetcher> BookmarkServerSearchService::CreateFetcher() {
  // Add the necessary arguments to the URI.
  GURL url(kSearchUrl);
  url = net::AppendQueryParameter(url, "output", "proto");
  url = net::AppendQueryParameter(url, "q", current_query_);
  url = net::AppendQueryParameter(url, "v", model_->GetVersionString());

  // Build the URLFetcher to perform the request.
  scoped_ptr<net::URLFetcher> url_fetcher(
      net::URLFetcher::Create(url, net::URLFetcher::GET, this));

  return url_fetcher;
}

bool BookmarkServerSearchService::ProcessResponse(const std::string& response,
                                                  bool* should_notify) {
  DCHECK(*should_notify);
  DCHECK(current_query_.length());
  image::collections::CorpusSearchResult response_proto;
  bool result = response_proto.ParseFromString(response);
  if (!result)
    return false;  // Not formatted properly.

  std::vector<std::string> clip_ids;
  for (const image::collections::CorpusSearchResult_ClipResult& clip_result :
       response_proto.results()) {
    const std::string& clip_id = clip_result.clip_id();
    if (!clip_id.length())
      continue;
    clip_ids.push_back(clip_id);
  }
  cache_.Put(current_query_, clip_ids);
  current_query_.clear();
  return true;
}

void BookmarkServerSearchService::CleanAfterFailure() {
  cache_.Clear();
  current_query_.clear();
}

void BookmarkServerSearchService::EnhancedBookmarkAdded(
    const BookmarkNode* node) {
  cache_.Clear();
}

void BookmarkServerSearchService::EnhancedBookmarkAllUserNodesRemoved() {
  cache_.Clear();
}

void BookmarkServerSearchService::EnhancedBookmarkRemoteIdChanged(
    const BookmarkNode* node,
    const std::string& old_remote_id,
    const std::string& remote_id) {
  cache_.Clear();
}
}  // namespace enhanced_bookmarks