OLD | NEW |
| (Empty) |
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 | |
15 namespace base { | |
16 class DictionaryValue; | |
17 } | |
18 | |
19 namespace net { | |
20 class URLFetcher; | |
21 } | |
22 | |
23 namespace 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. | |
28 class 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 IsPending() = 0; | |
40 | |
41 // Returns the response code received from the server, which will only be | |
42 // valid if the request succeeded. | |
43 virtual int GetResponseCode() = 0; | |
44 | |
45 // Returns the contents of the response body received from the server. | |
46 virtual const std::string& GetResponseBody() = 0; | |
47 | |
48 virtual void SetPostData(const std::string& post_data) = 0; | |
49 | |
50 // Tells the request to begin. | |
51 virtual void Start() = 0; | |
52 | |
53 protected: | |
54 Request(); | |
55 }; | |
56 | |
57 // Callback with the result of a call to QueryHistory(). Currently, the | |
58 // DictionaryValue is just the parsed JSON response from the server. | |
59 // TODO(dubroy): Extract the DictionaryValue into a structured results object. | |
60 typedef base::Callback<void(Request*, const base::DictionaryValue*)> | |
61 QueryWebHistoryCallback; | |
62 | |
63 typedef base::Callback<void(bool success)> ExpireWebHistoryCallback; | |
64 | |
65 typedef base::Callback<void(bool success, bool new_enabled_value)> | |
66 AudioWebHistoryCallback; | |
67 | |
68 typedef base::Callback<void(Request*, bool success)> CompletionCallback; | |
69 | |
70 explicit WebHistoryService(Profile* profile); | |
71 ~WebHistoryService() override; | |
72 | |
73 // Searches synced history for visits matching |text_query|. The timeframe to | |
74 // search, along with other options, is specified in |options|. If | |
75 // |text_query| is empty, all visits in the timeframe will be returned. | |
76 // This method is the equivalent of HistoryService::QueryHistory. | |
77 // The caller takes ownership of the returned Request. If it is destroyed, the | |
78 // request is cancelled. | |
79 scoped_ptr<Request> QueryHistory( | |
80 const base::string16& text_query, | |
81 const QueryOptions& options, | |
82 const QueryWebHistoryCallback& callback); | |
83 | |
84 // Removes all visits to specified URLs in specific time ranges. | |
85 // This is the of equivalent HistoryService::ExpireHistory(). | |
86 void ExpireHistory(const std::vector<ExpireHistoryArgs>& expire_list, | |
87 const ExpireWebHistoryCallback& callback); | |
88 | |
89 // Removes all visits to specified URLs in the given time range. | |
90 // This is the of equivalent HistoryService::ExpireHistoryBetween(). | |
91 void ExpireHistoryBetween(const std::set<GURL>& restrict_urls, | |
92 base::Time begin_time, | |
93 base::Time end_time, | |
94 const ExpireWebHistoryCallback& callback); | |
95 | |
96 // Requests whether audio history recording is enabled. | |
97 virtual void GetAudioHistoryEnabled(const AudioWebHistoryCallback& callback); | |
98 | |
99 // Sets the state of audio history recording to |new_enabled_value|. | |
100 virtual void SetAudioHistoryEnabled(bool new_enabled_value, | |
101 const AudioWebHistoryCallback& callback); | |
102 | |
103 // Used for tests. | |
104 size_t GetNumberOfPendingAudioHistoryRequests(); | |
105 | |
106 protected: | |
107 // This function is pulled out for testing purposes. Caller takes ownership of | |
108 // the new Request. | |
109 virtual Request* CreateRequest(const GURL& url, | |
110 const CompletionCallback& callback); | |
111 | |
112 // Extracts a JSON-encoded HTTP response into a DictionaryValue. | |
113 // If |request|'s HTTP response code indicates failure, or if the response | |
114 // body is not JSON, a null pointer is returned. | |
115 static scoped_ptr<base::DictionaryValue> ReadResponse(Request* request); | |
116 | |
117 // Called by |request| when a web history query has completed. Unpacks the | |
118 // response and calls |callback|, which is the original callback that was | |
119 // passed to QueryHistory(). | |
120 static void QueryHistoryCompletionCallback( | |
121 const WebHistoryService::QueryWebHistoryCallback& callback, | |
122 WebHistoryService::Request* request, | |
123 bool success); | |
124 | |
125 // Called by |request| when a request to delete history from the server has | |
126 // completed. Unpacks the response and calls |callback|, which is the original | |
127 // callback that was passed to ExpireHistory(). | |
128 void ExpireHistoryCompletionCallback( | |
129 const WebHistoryService::ExpireWebHistoryCallback& callback, | |
130 WebHistoryService::Request* request, | |
131 bool success); | |
132 | |
133 // Called by |request| when a request to get or set audio history from the | |
134 // server has completed. Unpacks the response and calls |callback|, which is | |
135 // the original callback that was passed to AudioHistory(). | |
136 void AudioHistoryCompletionCallback( | |
137 const WebHistoryService::AudioWebHistoryCallback& callback, | |
138 WebHistoryService::Request* request, | |
139 bool success); | |
140 | |
141 private: | |
142 friend class WebHistoryServiceTest; | |
143 | |
144 Profile* profile_; | |
145 | |
146 // Stores the version_info token received from the server in response to | |
147 // a mutation operation (e.g., deleting history). This is used to ensure that | |
148 // subsequent reads see a version of the data that includes the mutation. | |
149 std::string server_version_info_; | |
150 | |
151 // Pending expiration requests to be canceled if not complete by profile | |
152 // shutdown. | |
153 std::set<Request*> pending_expire_requests_; | |
154 | |
155 // Pending requests to be canceled if not complete by profile shutdown. | |
156 std::set<Request*> pending_audio_history_requests_; | |
157 | |
158 base::WeakPtrFactory<WebHistoryService> weak_ptr_factory_; | |
159 | |
160 DISALLOW_COPY_AND_ASSIGN(WebHistoryService); | |
161 }; | |
162 | |
163 } // namespace history | |
164 | |
165 #endif // CHROME_BROWSER_HISTORY_WEB_HISTORY_SERVICE_H_ | |
OLD | NEW |