trial_group_checker.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2019 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 ASH_COMPONENTS_TRIAL_GROUP_TRIAL_GROUP_CHECKER_H_
  5. #define ASH_COMPONENTS_TRIAL_GROUP_TRIAL_GROUP_CHECKER_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/component_export.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "url/gurl.h"
  11. namespace network {
  12. class SharedURLLoaderFactory;
  13. class SimpleURLLoader;
  14. } // namespace network
  15. namespace ash {
  16. namespace trial_group {
  17. // TrialGroupChecker determines whether the user is in a particular dogfood
  18. // trial by asking the external Dogpack server. The caller should only make
  19. // this request for users that have a dogfood finch experiment flag set.
  20. // |group_id| contains the integer corresponding to the dogfood trial. Only one
  21. // |group_id| per instance. To check another |group_id| use another instance.
  22. class COMPONENT_EXPORT(TRIAL_GROUP_CHECKER) TrialGroupChecker {
  23. public:
  24. enum GroupId {
  25. INVALID_GROUP = 0,
  26. ATLAS_DOGFOOD_GROUP = 1,
  27. TESTING_GROUP = 2,
  28. };
  29. enum Status {
  30. OK, // Everything went as planned.
  31. PREVIOUS_CALL_RUNNING, // Aborted due to previous call still running.
  32. };
  33. explicit TrialGroupChecker(GroupId group_id);
  34. TrialGroupChecker(const TrialGroupChecker&) = delete;
  35. TrialGroupChecker& operator=(const TrialGroupChecker&) = delete;
  36. ~TrialGroupChecker();
  37. // Checks user's membership and passes the result to a callback. The
  38. // TrialGroupChecker instance must live until after |callback| has finished
  39. // executing. LookUpMembership must not be called again until |callback| from
  40. // the previous call has completed.
  41. Status LookUpMembership(
  42. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  43. base::OnceCallback<void(bool is_member)> callback);
  44. // SetServerUrl is only used for testing.
  45. void SetServerUrl(GURL server_url);
  46. private:
  47. void OnRequestComplete(std::unique_ptr<std::string> response_body);
  48. // The url of the Dogpack server.
  49. GURL server_url_;
  50. // The id of the Google Group.
  51. int group_id_;
  52. // The callback provided by the caller.
  53. base::OnceCallback<void(bool is_member)> callback_;
  54. // Loader that sends the HTTP request to the Dogpack server.
  55. std::unique_ptr<network::SimpleURLLoader> loader_;
  56. base::WeakPtrFactory<TrialGroupChecker> weak_factory_{this};
  57. };
  58. } // namespace trial_group
  59. } // namespace ash
  60. #endif // ASH_COMPONENTS_TRIAL_GROUP_TRIAL_GROUP_CHECKER_H_