1 // Copyright (c) 2012 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 
5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_ECHO_PRIVATE_API_H_
6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_ECHO_PRIVATE_API_H_
7 
8 #include <string>
9 
10 #include "base/compiler_specific.h"
11 #include "chrome/browser/chromeos/ui/echo_dialog_listener.h"
12 #include "extensions/browser/extension_function.h"
13 
14 class PrefRegistrySimple;
15 
16 namespace chromeos {
17 
18 class EchoDialogView;
19 
20 // Namespace to register the EchoCheckedOffers field in Local State.
21 namespace echo_offer {
22 
23 void RegisterPrefs(PrefRegistrySimple* registry);
24 
25 }  // namespace echo_offer
26 
27 }  // namespace chromeos
28 
29 class EchoPrivateGetRegistrationCodeFunction : public ExtensionFunction {
30  public:
31   EchoPrivateGetRegistrationCodeFunction();
32 
33  protected:
34   ~EchoPrivateGetRegistrationCodeFunction() override;
35   ResponseAction Run() override;
36 
37  private:
38   ResponseValue GetRegistrationCode(const std::string& type);
39   DECLARE_EXTENSION_FUNCTION("echoPrivate.getRegistrationCode",
40                              ECHOPRIVATE_GETREGISTRATIONCODE)
41 };
42 
43 class EchoPrivateGetOobeTimestampFunction : public ExtensionFunction {
44  public:
45   EchoPrivateGetOobeTimestampFunction();
46 
47  protected:
48   ~EchoPrivateGetOobeTimestampFunction() override;
49   ResponseAction Run() override;
50 
51  private:
52   std::unique_ptr<base::Value> GetOobeTimestampOnFileSequence();
53   void RespondWithResult(std::unique_ptr<base::Value> result);
54 
55   DECLARE_EXTENSION_FUNCTION("echoPrivate.getOobeTimestamp",
56                              ECHOPRIVATE_GETOOBETIMESTAMP)
57 };
58 
59 class EchoPrivateSetOfferInfoFunction : public ExtensionFunction {
60  public:
61   EchoPrivateSetOfferInfoFunction();
62 
63  protected:
64   ~EchoPrivateSetOfferInfoFunction() override;
65   ResponseAction Run() override;
66 
67  private:
68   DECLARE_EXTENSION_FUNCTION("echoPrivate.setOfferInfo",
69                              ECHOPRIVATE_SETOFFERINFO)
70 };
71 
72 class EchoPrivateGetOfferInfoFunction : public ExtensionFunction {
73  public:
74   EchoPrivateGetOfferInfoFunction();
75 
76  protected:
77   ~EchoPrivateGetOfferInfoFunction() override;
78   ResponseAction Run() override;
79 
80  private:
81   DECLARE_EXTENSION_FUNCTION("echoPrivate.getOfferInfo",
82                              ECHOPRIVATE_GETOFFERINFO)
83 };
84 
85 // The function first checks if offers redeeming is allowed by the device
86 // policy. It should then show a dialog that, depending on the check result,
87 // either asks user's consent to verify the device's eligibility for the offer,
88 // or informs the user that the offers redeeming is disabled.
89 // It returns whether the user consent was given.
90 class EchoPrivateGetUserConsentFunction : public ExtensionFunction,
91                                           public chromeos::EchoDialogListener {
92  public:
93   // Type for the dialog shown callback used in tests.
94   using DialogShownTestCallback =
95       base::RepeatingCallback<void(chromeos::EchoDialogView* dialog)>;
96 
97   EchoPrivateGetUserConsentFunction();
98 
99   // Creates the function with non-null dialog_shown_callback_.
100   // To be used in tests.
101   static scoped_refptr<EchoPrivateGetUserConsentFunction> CreateForTest(
102       const DialogShownTestCallback& dialog_shown_callback);
103 
104  protected:
105   ~EchoPrivateGetUserConsentFunction() override;
106 
107   ResponseAction Run() override;
108 
109  private:
110   // chromeos::EchoDialogListener overrides.
111   void OnAccept() override;
112   void OnCancel() override;
113   void OnMoreInfoLinkClicked() override;
114 
115   // Checks whether "allow redeem ChromeOS registration offers" setting is
116   // disabled in cros settings. It may be asynchronous if the needed settings
117   // provider is not yet trusted.
118   // Upon completion |OnRedeemOffersAllowed| is called.
119   void CheckRedeemOffersAllowed();
120   void OnRedeemOffersAllowedChecked(bool is_allowed);
121 
122   // Sets result and calls SendResponse.
123   void Finalize(bool consent);
124 
125   // Result of |CheckRedeemOffersAllowed()|.
126   bool redeem_offers_allowed_;
127   // Callback used in tests. Called after an echo dialog is shown.
128   DialogShownTestCallback dialog_shown_callback_;
129 
130   DECLARE_EXTENSION_FUNCTION("echoPrivate.getUserConsent",
131                              ECHOPRIVATE_GETUSERCONSENT)
132 };
133 
134 #endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_ECHO_PRIVATE_API_H_
135