files_list_request_runner.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.
  4. #ifndef GOOGLE_APIS_DRIVE_FILES_LIST_REQUEST_RUNNER_H_
  5. #define GOOGLE_APIS_DRIVE_FILES_LIST_REQUEST_RUNNER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "google_apis/drive/drive_api_requests.h"
  12. #include "google_apis/drive/drive_api_url_generator.h"
  13. #include "google_apis/drive/drive_common_callbacks.h"
  14. namespace google_apis {
  15. class RequestSender;
  16. // Runs file list requests (the FileListRequest class) with a backoff retry
  17. // logic in case of the DRIVE_RESPONSE_TOO_LARGE error code.
  18. class FilesListRequestRunner {
  19. public:
  20. FilesListRequestRunner(
  21. RequestSender* request_sender,
  22. const google_apis::DriveApiUrlGenerator& url_generator);
  23. // Creates a FilesListRequest instance and starts the request with a backoff
  24. // retry in case of DRIVE_RESPONSE_TOO_LARGE error code.
  25. CancelCallbackOnce CreateAndStartWithSizeBackoff(
  26. int max_results,
  27. FilesListCorpora corpora,
  28. const std::string& team_drive_id,
  29. const std::string& q,
  30. const std::string& fields,
  31. FileListCallback callback);
  32. FilesListRequestRunner(const FilesListRequestRunner&) = delete;
  33. FilesListRequestRunner& operator=(const FilesListRequestRunner&) = delete;
  34. ~FilesListRequestRunner();
  35. void SetRequestCompletedCallbackForTesting(base::OnceClosure callback);
  36. private:
  37. // Called when the cancelling callback returned by
  38. // CreateAndStartWithSizeBackoff is invoked. Once called cancels the current
  39. // request.
  40. void OnCancel(CancelCallbackOnce* cancel_callback);
  41. // Called when a single request is completed with either a success or an
  42. // error. In case of DRIVE_RESPONSE_TOO_LARGE it will retry the request with
  43. // half of the requests.
  44. void OnCompleted(int max_results,
  45. FilesListCorpora corpora,
  46. const std::string& team_drive_id,
  47. const std::string& q,
  48. const std::string& fields,
  49. FileListCallback callback,
  50. CancelCallbackOnce* cancel_callback,
  51. ApiErrorCode error,
  52. std::unique_ptr<FileList> entry);
  53. raw_ptr<RequestSender> request_sender_; // Not owned.
  54. const google_apis::DriveApiUrlGenerator url_generator_; // Not owned.
  55. base::OnceClosure request_completed_callback_for_testing_;
  56. // Note: This should remain the last member so it'll be destroyed and
  57. // invalidate its weak pointers before any other members are destroyed.
  58. base::WeakPtrFactory<FilesListRequestRunner> weak_ptr_factory_{this};
  59. };
  60. } // namespace google_apis
  61. #endif // GOOGLE_APIS_DRIVE_FILES_LIST_REQUEST_RUNNER_H_