1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_HISTORY_WEB_HISTORY_SERVICE_H_
6#define CHROME_BROWSER_HISTORY_WEB_HISTORY_SERVICE_H_
7
8#include <set>
9
10#include "base/memory/weak_ptr.h"
11#include "chrome/browser/profiles/profile.h"
12#include "components/history/core/browser/history_types.h"
13#include "components/keyed_service/core/keyed_service.h"
14
15namespace base {
16class DictionaryValue;
17}
18
19namespace net {
20class URLFetcher;
21}
22
23namespace history {
24
25// Provides an API for querying Google servers for a signed-in user's
26// synced history visits. It is roughly analogous to HistoryService, and
27// supports a similar API.
28class WebHistoryService : public KeyedService {
29 public:
30  // Handles all the work of making an API request. This class encapsulates
31  // the entire state of the request. When an instance is destroyed, all
32  // aspects of the request are cancelled.
33  class Request {
34   public:
35    virtual ~Request();
36
37    // Returns true if the request is "pending" (i.e., it has been started, but
38    // is not yet been complete).
39    virtual bool is_pending() = 0;
40
41   protected:
42    Request();
43  };
44
45  // Callback with the result of a call to QueryHistory(). Currently, the
46  // DictionaryValue is just the parsed JSON response from the server.
47  // TODO(dubroy): Extract the DictionaryValue into a structured results object.
48  typedef base::Callback<void(Request*, const base::DictionaryValue*)>
49      QueryWebHistoryCallback;
50
51  typedef base::Callback<void(bool success)> ExpireWebHistoryCallback;
52
53  explicit WebHistoryService(Profile* profile);
54  virtual ~WebHistoryService();
55
56  // Searches synced history for visits matching |text_query|. The timeframe to
57  // search, along with other options, is specified in |options|. If
58  // |text_query| is empty, all visits in the timeframe will be returned.
59  // This method is the equivalent of HistoryService::QueryHistory.
60  // The caller takes ownership of the returned Request. If it is destroyed, the
61  // request is cancelled.
62  scoped_ptr<Request> QueryHistory(
63      const base::string16& text_query,
64      const QueryOptions& options,
65      const QueryWebHistoryCallback& callback);
66
67  // Removes all visits to specified URLs in specific time ranges.
68  // This is the of equivalent HistoryService::ExpireHistory().
69  void ExpireHistory(const std::vector<ExpireHistoryArgs>& expire_list,
70                     const ExpireWebHistoryCallback& callback);
71
72  // Removes all visits to specified URLs in the given time range.
73  // This is the of equivalent HistoryService::ExpireHistoryBetween().
74  void ExpireHistoryBetween(const std::set<GURL>& restrict_urls,
75                            base::Time begin_time,
76                            base::Time end_time,
77                            const ExpireWebHistoryCallback& callback);
78
79 private:
80  // Called by |request| when a web history query has completed. Unpacks the
81  // response and calls |callback|, which is the original callback that was
82  // passed to QueryHistory().
83  static void QueryHistoryCompletionCallback(
84      const WebHistoryService::QueryWebHistoryCallback& callback,
85      WebHistoryService::Request* request,
86      bool success);
87
88  // Called by |request| when a request to delete history from the server has
89  // completed. Unpacks the response and calls |callback|, which is the original
90  // callback that was passed to ExpireHistory().
91  void ExpireHistoryCompletionCallback(
92      const WebHistoryService::ExpireWebHistoryCallback& callback,
93      WebHistoryService::Request* request,
94      bool success);
95
96  Profile* profile_;
97
98  // Stores the version_info token received from the server in response to
99  // a mutation operation (e.g., deleting history). This is used to ensure that
100  // subsequent reads see a version of the data that includes the mutation.
101  std::string server_version_info_;
102
103  // Pending expiration requests to be canceled if not complete by profile
104  // shutdown.
105  std::set<Request*> pending_expire_requests_;
106
107  base::WeakPtrFactory<WebHistoryService> weak_ptr_factory_;
108
109  DISALLOW_COPY_AND_ASSIGN(WebHistoryService);
110};
111
112}  // namespace history
113
114#endif  // CHROME_BROWSER_HISTORY_WEB_HISTORY_SERVICE_H_
115