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"
8 #include "google_apis/drive/drive_api_error_codes.h"
9 #include "google_apis/drive/drive_api_requests.h"
10 #include "google_apis/drive/request_sender.h"
12 namespace google_apis
{
14 FilesListRequestRunner::FilesListRequestRunner(
15 RequestSender
* request_sender
,
16 const google_apis::DriveApiUrlGenerator
& url_generator
)
17 : request_sender_(request_sender
),
18 url_generator_(url_generator
),
19 weak_ptr_factory_(this) {
22 FilesListRequestRunner::~FilesListRequestRunner() {
25 CancelCallback
FilesListRequestRunner::CreateAndStartWithSizeBackoff(
28 const std::string
& fields
,
29 const FileListCallback
& callback
) {
30 base::Closure
* const cancel_callback
= new base::Closure
;
31 drive::FilesListRequest
* const request
= new drive::FilesListRequest(
32 request_sender_
, url_generator_
,
33 base::Bind(&FilesListRequestRunner::OnCompleted
,
34 weak_ptr_factory_
.GetWeakPtr(), max_results
, q
, fields
,
35 callback
, base::Owned(cancel_callback
)));
36 request
->set_max_results(max_results
);
38 request
->set_fields(fields
);
39 *cancel_callback
= request_sender_
->StartRequestWithAuthRetry(request
);
41 // The cancellation callback is owned by the completion callback, so it must
42 // not be used after |callback| is called.
43 return base::Bind(&FilesListRequestRunner::OnCancel
,
44 weak_ptr_factory_
.GetWeakPtr(),
45 base::Unretained(cancel_callback
));
48 void FilesListRequestRunner::OnCancel(base::Closure
* cancel_callback
) {
49 DCHECK(cancel_callback
);
50 DCHECK(!cancel_callback
->is_null());
51 cancel_callback
->Run();
54 void FilesListRequestRunner::OnCompleted(int max_results
,
56 const std::string
& fields
,
57 const FileListCallback
& callback
,
58 CancelCallback
* cancel_callback
,
59 DriveApiErrorCode error
,
60 scoped_ptr
<FileList
> entry
) {
61 if (!request_completed_callback_for_testing_
.is_null())
62 request_completed_callback_for_testing_
.Run();
64 if (error
== google_apis::DRIVE_RESPONSE_TOO_LARGE
&& max_results
> 1) {
65 CreateAndStartWithSizeBackoff(max_results
/ 2, q
, fields
, callback
);
69 callback
.Run(error
, entry
.Pass());
72 void FilesListRequestRunner::SetRequestCompletedCallbackForTesting(
73 const base::Closure
& callback
) {
74 request_completed_callback_for_testing_
= callback
;
77 } // namespace google_apis