Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / google_apis / drive / files_list_request_runner.cc
blobc3588b745f3001a48077b3c45df24271bb8a5d22
1 // Copyright 2015 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.
5 #include "google_apis/drive/files_list_request_runner.h"
7 #include "base/bind.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/metrics/sparse_histogram.h"
10 #include "google_apis/drive/drive_api_error_codes.h"
11 #include "google_apis/drive/drive_api_requests.h"
12 #include "google_apis/drive/request_sender.h"
14 namespace google_apis {
16 FilesListRequestRunner::FilesListRequestRunner(
17 RequestSender* request_sender,
18 const google_apis::DriveApiUrlGenerator& url_generator)
19 : request_sender_(request_sender),
20 url_generator_(url_generator),
21 weak_ptr_factory_(this) {
24 FilesListRequestRunner::~FilesListRequestRunner() {
27 CancelCallback FilesListRequestRunner::CreateAndStartWithSizeBackoff(
28 int max_results,
29 const std::string& q,
30 const std::string& fields,
31 const FileListCallback& callback) {
32 UMA_HISTOGRAM_COUNTS_1000("Drive.FilesListRequestRunner.MaxResults",
33 max_results);
34 base::Closure* const cancel_callback = new base::Closure;
35 drive::FilesListRequest* const request = new drive::FilesListRequest(
36 request_sender_, url_generator_,
37 base::Bind(&FilesListRequestRunner::OnCompleted,
38 weak_ptr_factory_.GetWeakPtr(), max_results, q, fields,
39 callback, base::Owned(cancel_callback)));
40 request->set_max_results(max_results);
41 request->set_q(q);
42 request->set_fields(fields);
43 *cancel_callback = request_sender_->StartRequestWithAuthRetry(request);
45 // The cancellation callback is owned by the completion callback, so it must
46 // not be used after |callback| is called.
47 return base::Bind(&FilesListRequestRunner::OnCancel,
48 weak_ptr_factory_.GetWeakPtr(),
49 base::Unretained(cancel_callback));
52 void FilesListRequestRunner::OnCancel(base::Closure* cancel_callback) {
53 DCHECK(cancel_callback);
54 DCHECK(!cancel_callback->is_null());
55 cancel_callback->Run();
58 void FilesListRequestRunner::OnCompleted(int max_results,
59 const std::string& q,
60 const std::string& fields,
61 const FileListCallback& callback,
62 CancelCallback* cancel_callback,
63 DriveApiErrorCode error,
64 scoped_ptr<FileList> entry) {
65 if (!request_completed_callback_for_testing_.is_null())
66 request_completed_callback_for_testing_.Run();
68 UMA_HISTOGRAM_SPARSE_SLOWLY(
69 "Drive.FilesListRequestRunner.ApiErrorCode", error);
71 if (error == google_apis::DRIVE_RESPONSE_TOO_LARGE && max_results > 1) {
72 CreateAndStartWithSizeBackoff(max_results / 2, q, fields, callback);
73 return;
76 callback.Run(error, entry.Pass());
79 void FilesListRequestRunner::SetRequestCompletedCallbackForTesting(
80 const base::Closure& callback) {
81 request_completed_callback_for_testing_ = callback;
84 } // namespace google_apis