quirks_client.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2016 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 COMPONENTS_QUIRKS_QUIRKS_CLIENT_H_
  5. #define COMPONENTS_QUIRKS_QUIRKS_CLIENT_H_
  6. #include <memory>
  7. #include "base/files/file_path.h"
  8. #include "base/threading/thread_checker.h"
  9. #include "base/timer/timer.h"
  10. #include "net/base/backoff_entry.h"
  11. namespace network {
  12. class SimpleURLLoader;
  13. }
  14. namespace quirks {
  15. class QuirksManager;
  16. // See declaration in quirks_manager.h.
  17. using RequestFinishedCallback =
  18. base::OnceCallback<void(const base::FilePath&, bool)>;
  19. // Handles downloading icc and other display data files from Quirks Server.
  20. class QuirksClient {
  21. public:
  22. QuirksClient(int64_t product_id,
  23. const std::string& display_name,
  24. RequestFinishedCallback on_request_finished,
  25. QuirksManager* manager);
  26. QuirksClient(const QuirksClient&) = delete;
  27. QuirksClient& operator=(const QuirksClient&) = delete;
  28. ~QuirksClient();
  29. void StartDownload();
  30. int64_t product_id() const { return product_id_; }
  31. private:
  32. void OnDownloadComplete(std::unique_ptr<std::string> response_body);
  33. // Send callback and tell manager to delete |this|.
  34. void Shutdown(bool success);
  35. // Schedules a retry.
  36. void Retry();
  37. // Translates json with base64-encoded data (|result|) into raw |data|.
  38. bool ParseResult(const std::string& result, std::string* data);
  39. // ID of display to request from Quirks Server.
  40. const int64_t product_id_;
  41. // Human-readable name to send to Quirks Server.
  42. const std::string display_name_;
  43. // Callback supplied by caller.
  44. RequestFinishedCallback on_request_finished_;
  45. // Weak pointer owned by manager, guaranteed to outlive this client object.
  46. QuirksManager* manager_;
  47. // Full path to icc file.
  48. const base::FilePath icc_path_;
  49. // The class is expected to run on UI thread.
  50. base::ThreadChecker thread_checker_;
  51. // This loader is used to download icc file.
  52. std::unique_ptr<network::SimpleURLLoader> url_loader_;
  53. // Pending retry.
  54. base::OneShotTimer request_scheduled_;
  55. // Controls exponential backoff of time between server checks.
  56. net::BackoffEntry backoff_entry_;
  57. // Factory for callbacks.
  58. base::WeakPtrFactory<QuirksClient> weak_ptr_factory_{this};
  59. };
  60. } // namespace quirks
  61. #endif // COMPONENTS_QUIRKS_QUIRKS_CLIENT_H_