1 // Copyright (c) 2018 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_LOGIN_OOBE_CONFIGURATION_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_OOBE_CONFIGURATION_H_
7 
8 #include <memory>
9 
10 #include "base/files/file_path.h"
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/values.h"
15 
16 namespace chromeos {
17 
18 // Configuration that might be used to automate passing through
19 // OOBE/enrollment screens
20 class OobeConfiguration {
21  public:
22   class Observer {
23    public:
24     Observer() = default;
25     virtual ~Observer() = default;
26 
27     virtual void OnOobeConfigurationChanged() = 0;
28   };
29 
30   OobeConfiguration();
31   virtual ~OobeConfiguration();
32 
33   const base::Value& GetConfiguration() const;
34   bool CheckCompleted() const;
35 
36   void AddAndFireObserver(Observer* observer);
37   void RemoveObserver(Observer* observer);
38 
39   void ResetConfiguration();
40   void CheckConfiguration();
41 
42   // Returns current OobeConfiguration instance and NULL if it hasn't been
43   // initialized yet.
44   static OobeConfiguration* Get();
45 
set_skip_check_for_testing(bool value)46   static void set_skip_check_for_testing(bool value) {
47     skip_check_for_testing_ = value;
48   }
49 
50  private:
51   void OnConfigurationCheck(bool has_configuration,
52                             const std::string& configuration);
53   void NotifyObservers();
54 
55   void UpdateConfigurationValues();
56 
57   // Pointer to the existing OobeConfiguration instance (if any).
58   // Set in ctor, reset in dtor. Not owned since we need to control the lifetime
59   // externally.
60   // Instance is owned by ChromeSessionManager
61   static OobeConfiguration* instance;
62 
63   // Some tests might require setting some expectations before configuration is
64   // loaded. This field allows tests to skip actual configuration check in
65   // CheckConfiguration() call. Once all expectations are set, test should set
66   // this flag back to false and run CheckConfiguration() to proceed.
67   static bool skip_check_for_testing_;
68 
69   // Tracks if configuration check is completed.
70   bool check_completed_;
71 
72   // Non-null dictionary value with configuration.
73   std::unique_ptr<base::Value> configuration_;
74 
75   // Observers
76   base::ObserverList<Observer>::Unchecked observer_list_;
77 
78   // Factory of callbacks.
79   base::WeakPtrFactory<OobeConfiguration> weak_factory_{this};
80 };
81 
82 }  // namespace chromeos
83 
84 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_OOBE_CONFIGURATION_H_
85