OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/history/web_history_service.h" | 5 #include "chrome/browser/history/web_history_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // The callback to execute when the query is complete. | 199 // The callback to execute when the query is complete. |
200 CompletionCallback callback_; | 200 CompletionCallback callback_; |
201 | 201 |
202 // True if the request was started and has not yet completed, otherwise false. | 202 // True if the request was started and has not yet completed, otherwise false. |
203 bool is_pending_; | 203 bool is_pending_; |
204 }; | 204 }; |
205 | 205 |
206 // Extracts a JSON-encoded HTTP response into a DictionaryValue. | 206 // Extracts a JSON-encoded HTTP response into a DictionaryValue. |
207 // If |request|'s HTTP response code indicates failure, or if the response | 207 // If |request|'s HTTP response code indicates failure, or if the response |
208 // body is not JSON, a null pointer is returned. | 208 // body is not JSON, a null pointer is returned. |
209 scoped_ptr<DictionaryValue> ReadResponse(RequestImpl* request) { | 209 scoped_ptr<base::DictionaryValue> ReadResponse(RequestImpl* request) { |
210 scoped_ptr<DictionaryValue> result; | 210 scoped_ptr<base::DictionaryValue> result; |
211 if (request->response_code() == net::HTTP_OK) { | 211 if (request->response_code() == net::HTTP_OK) { |
212 Value* value = base::JSONReader::Read(request->response_body()); | 212 base::Value* value = base::JSONReader::Read(request->response_body()); |
213 if (value && value->IsType(base::Value::TYPE_DICTIONARY)) | 213 if (value && value->IsType(base::Value::TYPE_DICTIONARY)) |
214 result.reset(static_cast<DictionaryValue*>(value)); | 214 result.reset(static_cast<base::DictionaryValue*>(value)); |
215 else | 215 else |
216 DLOG(WARNING) << "Non-JSON response received from history server."; | 216 DLOG(WARNING) << "Non-JSON response received from history server."; |
217 } | 217 } |
218 return result.Pass(); | 218 return result.Pass(); |
219 } | 219 } |
220 | 220 |
221 // Converts a time into a string for use as a parameter in a request to the | 221 // Converts a time into a string for use as a parameter in a request to the |
222 // history server. | 222 // history server. |
223 std::string ServerTimeString(base::Time time) { | 223 std::string ServerTimeString(base::Time time) { |
224 if (time < base::Time::UnixEpoch()) { | 224 if (time < base::Time::UnixEpoch()) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 262 |
263 if (!version_info.empty()) | 263 if (!version_info.empty()) |
264 url = net::AppendQueryParameter(url, "kvi", version_info); | 264 url = net::AppendQueryParameter(url, "kvi", version_info); |
265 | 265 |
266 return url; | 266 return url; |
267 } | 267 } |
268 | 268 |
269 // Creates a DictionaryValue to hold the parameters for a deletion. | 269 // Creates a DictionaryValue to hold the parameters for a deletion. |
270 // Ownership is passed to the caller. | 270 // Ownership is passed to the caller. |
271 // |url| may be empty, indicating a time-range deletion. | 271 // |url| may be empty, indicating a time-range deletion. |
272 DictionaryValue* CreateDeletion( | 272 base::DictionaryValue* CreateDeletion( |
273 const std::string& min_time, | 273 const std::string& min_time, |
274 const std::string& max_time, | 274 const std::string& max_time, |
275 const GURL& url) { | 275 const GURL& url) { |
276 DictionaryValue* deletion = new DictionaryValue; | 276 base::DictionaryValue* deletion = new base::DictionaryValue; |
277 deletion->SetString("type", "CHROME_HISTORY"); | 277 deletion->SetString("type", "CHROME_HISTORY"); |
278 if (url.is_valid()) | 278 if (url.is_valid()) |
279 deletion->SetString("url", url.spec()); | 279 deletion->SetString("url", url.spec()); |
280 deletion->SetString("min_timestamp_usec", min_time); | 280 deletion->SetString("min_timestamp_usec", min_time); |
281 deletion->SetString("max_timestamp_usec", max_time); | 281 deletion->SetString("max_timestamp_usec", max_time); |
282 return deletion; | 282 return deletion; |
283 } | 283 } |
284 | 284 |
285 } // namespace | 285 } // namespace |
286 | 286 |
(...skipping 22 matching lines...) Expand all Loading... |
309 GURL url = GetQueryUrl(text_query, options, server_version_info_); | 309 GURL url = GetQueryUrl(text_query, options, server_version_info_); |
310 scoped_ptr<RequestImpl> request( | 310 scoped_ptr<RequestImpl> request( |
311 new RequestImpl(profile_, url, completion_callback)); | 311 new RequestImpl(profile_, url, completion_callback)); |
312 request->Start(); | 312 request->Start(); |
313 return request.PassAs<Request>(); | 313 return request.PassAs<Request>(); |
314 } | 314 } |
315 | 315 |
316 scoped_ptr<WebHistoryService::Request> WebHistoryService::ExpireHistory( | 316 scoped_ptr<WebHistoryService::Request> WebHistoryService::ExpireHistory( |
317 const std::vector<ExpireHistoryArgs>& expire_list, | 317 const std::vector<ExpireHistoryArgs>& expire_list, |
318 const ExpireWebHistoryCallback& callback) { | 318 const ExpireWebHistoryCallback& callback) { |
319 DictionaryValue delete_request; | 319 base::DictionaryValue delete_request; |
320 scoped_ptr<ListValue> deletions(new ListValue); | 320 scoped_ptr<base::ListValue> deletions(new base::ListValue); |
321 base::Time now = base::Time::Now(); | 321 base::Time now = base::Time::Now(); |
322 | 322 |
323 for (std::vector<ExpireHistoryArgs>::const_iterator it = expire_list.begin(); | 323 for (std::vector<ExpireHistoryArgs>::const_iterator it = expire_list.begin(); |
324 it != expire_list.end(); ++it) { | 324 it != expire_list.end(); ++it) { |
325 // Convert the times to server timestamps. | 325 // Convert the times to server timestamps. |
326 std::string min_timestamp = ServerTimeString(it->begin_time); | 326 std::string min_timestamp = ServerTimeString(it->begin_time); |
327 // TODO(dubroy): Use sane time (crbug.com/146090) here when it's available. | 327 // TODO(dubroy): Use sane time (crbug.com/146090) here when it's available. |
328 base::Time end_time = it->end_time; | 328 base::Time end_time = it->end_time; |
329 if (end_time.is_null() || end_time > now) | 329 if (end_time.is_null() || end_time > now) |
330 end_time = now; | 330 end_time = now; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 expire_list.back().begin_time = begin_time; | 373 expire_list.back().begin_time = begin_time; |
374 expire_list.back().end_time = end_time; | 374 expire_list.back().end_time = end_time; |
375 return ExpireHistory(expire_list, callback); | 375 return ExpireHistory(expire_list, callback); |
376 } | 376 } |
377 | 377 |
378 // static | 378 // static |
379 void WebHistoryService::QueryHistoryCompletionCallback( | 379 void WebHistoryService::QueryHistoryCompletionCallback( |
380 const WebHistoryService::QueryWebHistoryCallback& callback, | 380 const WebHistoryService::QueryWebHistoryCallback& callback, |
381 WebHistoryService::Request* request, | 381 WebHistoryService::Request* request, |
382 bool success) { | 382 bool success) { |
383 scoped_ptr<DictionaryValue> response_value; | 383 scoped_ptr<base::DictionaryValue> response_value; |
384 if (success) | 384 if (success) |
385 response_value = ReadResponse(static_cast<RequestImpl*>(request)); | 385 response_value = ReadResponse(static_cast<RequestImpl*>(request)); |
386 callback.Run(request, response_value.get()); | 386 callback.Run(request, response_value.get()); |
387 } | 387 } |
388 | 388 |
389 void WebHistoryService::ExpireHistoryCompletionCallback( | 389 void WebHistoryService::ExpireHistoryCompletionCallback( |
390 const WebHistoryService::ExpireWebHistoryCallback& callback, | 390 const WebHistoryService::ExpireWebHistoryCallback& callback, |
391 WebHistoryService::Request* request, | 391 WebHistoryService::Request* request, |
392 bool success) { | 392 bool success) { |
393 scoped_ptr<DictionaryValue> response_value; | 393 scoped_ptr<base::DictionaryValue> response_value; |
394 if (success) { | 394 if (success) { |
395 response_value = ReadResponse(static_cast<RequestImpl*>(request)); | 395 response_value = ReadResponse(static_cast<RequestImpl*>(request)); |
396 if (response_value) | 396 if (response_value) |
397 response_value->GetString("version_info", &server_version_info_); | 397 response_value->GetString("version_info", &server_version_info_); |
398 } | 398 } |
399 callback.Run(request, response_value.get() && success); | 399 callback.Run(request, response_value.get() && success); |
400 } | 400 } |
401 | 401 |
402 } // namespace history | 402 } // namespace history |
OLD | NEW |