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