blob: d755f0890e4394b493ee50dbdb59e33d544afcee [file] [log] [blame]
Christopher Wiley583d64b2015-03-24 14:30:17 -07001// Copyright 2015 The Chromium OS 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 BUFFET_BUFFET_CONFIG_H_
6#define BUFFET_BUFFET_CONFIG_H_
7
8#include <string>
Vitaly Bukaee7a3af2015-05-14 16:57:23 -07009#include <vector>
Christopher Wiley583d64b2015-03-24 14:30:17 -070010
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070011#include <base/callback.h>
Christopher Wiley583d64b2015-03-24 14:30:17 -070012#include <base/files/file_path.h>
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070013#include <chromeos/errors/error.h>
Christopher Wiley583d64b2015-03-24 14:30:17 -070014#include <chromeos/key_value_store.h>
15
16namespace buffet {
17
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070018class StorageInterface;
19
20// Handles reading buffet config and state files.
21class BuffetConfig final {
Christopher Wiley583d64b2015-03-24 14:30:17 -070022 public:
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070023 using OnChangedCallback = base::Callback<void(const BuffetConfig&)>;
24
25 explicit BuffetConfig(std::unique_ptr<StorageInterface> storage);
26
27 explicit BuffetConfig(const base::FilePath& state_path);
28
29 void AddOnChangedCallback(const OnChangedCallback& callback) {
30 on_changed_.push_back(callback);
31 // Force to read current state.
32 callback.Run(*this);
33 }
Christopher Wiley583d64b2015-03-24 14:30:17 -070034
35 void Load(const base::FilePath& config_path);
36 void Load(const chromeos::KeyValueStore& store);
37
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070038 // Allows editing of config. Makes sure that callbacks were called and changes
39 // were saved.
40 // User can commit changes by calling Commit method or by destroying the
41 // object.
42 class Transaction final {
43 public:
44 explicit Transaction(BuffetConfig* config) : config_(config) {
45 CHECK(config_);
46 }
47
48 ~Transaction();
49
Vitaly Bukaff81db62015-05-14 21:25:45 -070050 void set_client_id(const std::string& id) { config_->client_id_ = id; }
51 void set_client_secret(const std::string& secret) {
52 config_->client_secret_ = secret;
53 }
54 void set_api_key(const std::string& key) { config_->api_key_ = key; }
55 void set_oauth_url(const std::string& url) { config_->oauth_url_ = url; }
56 void set_service_url(const std::string& url) {
57 config_->service_url_ = url;
58 }
Vitaly Bukaee7a3af2015-05-14 16:57:23 -070059 bool set_name(const std::string& name);
60 void set_description(const std::string& description) {
61 config_->description_ = description;
62 }
63 void set_location(const std::string& location) {
64 config_->location_ = location;
65 }
66 bool set_local_anonymous_access_role(const std::string& role);
67 void set_local_discovery_enabled(bool enabled) {
68 config_->local_discovery_enabled_ = enabled;
69 }
70 void set_local_pairing_enabled(bool enabled) {
71 config_->local_pairing_enabled_ = enabled;
72 }
73 void set_device_id(const std::string& id) { config_->device_id_ = id; }
74 void set_refresh_token(const std::string& token) {
75 config_->refresh_token_ = token;
76 }
77 void set_robot_account(const std::string& account) {
78 config_->robot_account_ = account;
79 }
80
81 void Commit();
82
83 private:
84 friend class BuffetConfig;
85 void LoadState();
86 BuffetConfig* config_;
87 bool save_{true};
88 };
89
Vitaly Buka867b0882015-04-16 10:03:26 -070090 const std::string& client_id() const { return client_id_; }
91 const std::string& client_secret() const { return client_secret_; }
92 const std::string& api_key() const { return api_key_; }
93 const std::string& oauth_url() const { return oauth_url_; }
94 const std::string& service_url() const { return service_url_; }
95 const std::string& oem_name() const { return oem_name_; }
96 const std::string& model_name() const { return model_name_; }
97 const std::string& model_id() const { return model_id_; }
98 const std::string& device_kind() const { return device_kind_; }
Christopher Wiley34eae042015-03-18 10:25:08 -070099 uint64_t polling_period_ms() const { return polling_period_ms_; }
Alex Vakulenkod05725f2015-05-27 15:48:19 -0700100 uint64_t backup_polling_period_ms() const {
101 return backup_polling_period_ms_;
102 }
Christopher Wiley583d64b2015-03-24 14:30:17 -0700103
Vitaly Buka867b0882015-04-16 10:03:26 -0700104 const std::string& name() const { return name_; }
105 const std::string& description() const { return description_; }
106 const std::string& location() const { return location_; }
Vitaly Buka4a3a9a22015-05-13 16:06:01 -0700107 std::string local_anonymous_access_role() const {
108 return local_anonymous_access_role_;
109 }
110 bool local_pairing_enabled() const { return local_pairing_enabled_; }
111 bool local_discovery_enabled() const { return local_discovery_enabled_; }
Vitaly Buka867b0882015-04-16 10:03:26 -0700112
Vitaly Bukaee7a3af2015-05-14 16:57:23 -0700113 std::string device_id() const { return device_id_; }
114 std::string refresh_token() const { return refresh_token_; }
115 std::string robot_account() const { return robot_account_; }
Vitaly Buka867b0882015-04-16 10:03:26 -0700116
Christopher Wiley583d64b2015-03-24 14:30:17 -0700117 private:
Vitaly Bukaee7a3af2015-05-14 16:57:23 -0700118 bool Save();
119
Christopher Wiley583d64b2015-03-24 14:30:17 -0700120 std::string client_id_{"58855907228.apps.googleusercontent.com"};
121 std::string client_secret_{"eHSAREAHrIqPsHBxCE9zPPBi"};
122 std::string api_key_{"AIzaSyDSq46gG-AxUnC3zoqD9COIPrjolFsMfMA"};
123 std::string oauth_url_{"https://accounts.google.com/o/oauth2/"};
124 std::string service_url_{"https://www.googleapis.com/clouddevices/v1/"};
Alex Vakulenko468f7f32015-04-08 10:42:45 -0700125 std::string name_{"Developer device"};
Vitaly Buka867b0882015-04-16 10:03:26 -0700126 std::string description_;
127 std::string location_;
Vitaly Buka4a3a9a22015-05-13 16:06:01 -0700128 std::string local_anonymous_access_role_{"viewer"};
129 bool local_discovery_enabled_{true};
130 bool local_pairing_enabled_{true};
Vitaly Buka867b0882015-04-16 10:03:26 -0700131 std::string oem_name_{"Chromium"};
132 std::string model_name_{"Brillo"};
133 std::string model_id_{"AAAAA"};
Alex Vakulenko1fb92172015-04-20 10:59:20 -0700134 std::string device_kind_{"vendor"};
Alex Vakulenkod05725f2015-05-27 15:48:19 -0700135 uint64_t polling_period_ms_{7000}; // 7 seconds.
136 uint64_t backup_polling_period_ms_{30 * 60 * 1000}; // 30 minutes.
Christopher Wiley583d64b2015-03-24 14:30:17 -0700137
Vitaly Bukaee7a3af2015-05-14 16:57:23 -0700138 std::string device_id_;
139 std::string refresh_token_;
140 std::string robot_account_;
141
142 // Serialization interface to save and load buffet state.
143 std::unique_ptr<StorageInterface> storage_;
144
145 std::vector<OnChangedCallback> on_changed_;
146
Christopher Wiley583d64b2015-03-24 14:30:17 -0700147 DISALLOW_COPY_AND_ASSIGN(BuffetConfig);
148};
149
150} // namespace buffet
151
152#endif // BUFFET_BUFFET_CONFIG_H_