Move OnSettingsChanged callback into callback of weave::Device

BUG:24267885

Change-Id: I648568ccb31186692b97dd2149cbb9d95ba14771
Reviewed-on: https://weave-review.googlesource.com/1203
Reviewed-by: Vitaly Buka <vitalybuka@google.com>
diff --git a/libweave/examples/ubuntu/file_config_store.cc b/libweave/examples/ubuntu/file_config_store.cc
index d340884..8cc8fc5 100644
--- a/libweave/examples/ubuntu/file_config_store.cc
+++ b/libweave/examples/ubuntu/file_config_store.cc
@@ -59,10 +59,6 @@
   str << settings;
 }
 
-void FileConfigStore::OnSettingsChanged(const Settings& settings) {
-  LOG(INFO) << "OnSettingsChanged";
-}
-
 std::string FileConfigStore::LoadBaseCommandDefs() {
   return R"({
     "base": {
diff --git a/libweave/include/weave/device.h b/libweave/include/weave/device.h
index 39e2f21..edc2075 100644
--- a/libweave/include/weave/device.h
+++ b/libweave/include/weave/device.h
@@ -27,6 +27,10 @@
 
 class Device {
  public:
+  // Callback type for AddSettingsChangedCallback.
+  using SettingsChangedCallback =
+      base::Callback<void(const Settings& settings)>;
+
   virtual ~Device() = default;
 
   virtual void Start(provider::ConfigStore* config_store,
@@ -38,6 +42,13 @@
                      provider::Wifi* wifi,
                      provider::Bluetooth* bluetooth_provider) = 0;
 
+  // Returns reference the current settings.
+  virtual const Settings& GetSettings() = 0;
+
+  // Subscribes to notification settings changes.
+  virtual void AddSettingsChangedCallback(
+      const SettingsChangedCallback& callback) = 0;
+
   virtual Commands* GetCommands() = 0;
   virtual State* GetState() = 0;
   virtual Cloud* GetCloud() = 0;
diff --git a/libweave/include/weave/provider/test/mock_config_store.h b/libweave/include/weave/provider/test/mock_config_store.h
index c433c2e..1a287ee 100644
--- a/libweave/include/weave/provider/test/mock_config_store.h
+++ b/libweave/include/weave/provider/test/mock_config_store.h
@@ -36,12 +36,10 @@
         }));
     EXPECT_CALL(*this, LoadSettings()).WillRepeatedly(Return(""));
     EXPECT_CALL(*this, SaveSettings(_)).WillRepeatedly(Return());
-    EXPECT_CALL(*this, OnSettingsChanged(_)).WillRepeatedly(Return());
   }
   MOCK_METHOD1(LoadDefaults, bool(Settings*));
   MOCK_METHOD0(LoadSettings, std::string());
   MOCK_METHOD1(SaveSettings, void(const std::string&));
-  MOCK_METHOD1(OnSettingsChanged, void(const Settings&));
 
   MOCK_METHOD0(LoadBaseCommandDefs, std::string());
   MOCK_METHOD0(LoadCommandDefs, std::map<std::string, std::string>());
diff --git a/libweave/src/base_api_handler.cc b/libweave/src/base_api_handler.cc
index eb76919..b243f19 100644
--- a/libweave/src/base_api_handler.cc
+++ b/libweave/src/base_api_handler.cc
@@ -25,7 +25,7 @@
     const std::shared_ptr<StateManager>& state_manager,
     const std::shared_ptr<CommandManager>& command_manager)
     : device_info_{device_info}, state_manager_{state_manager} {
-  device_info_->AddOnConfigChangedCallback(base::Bind(
+  device_info_->GetMutableConfig()->AddOnChangedCallback(base::Bind(
       &BaseApiHandler::OnConfigChanged, weak_ptr_factory_.GetWeakPtr()));
 
   const auto& settings = device_info_->GetSettings();
diff --git a/libweave/src/config.cc b/libweave/src/config.cc
index b0ec45d..d014198 100644
--- a/libweave/src/config.cc
+++ b/libweave/src/config.cc
@@ -55,10 +55,6 @@
 
 Config::Config(provider::ConfigStore* config_store)
     : settings_{CreateDefaultSettings()}, config_store_{config_store} {
-  if (config_store_) {
-    AddOnChangedCallback(base::Bind(&provider::ConfigStore::OnSettingsChanged,
-                                    base::Unretained(config_store_)));
-  }
 }
 
 void Config::AddOnChangedCallback(const OnChangedCallback& callback) {
diff --git a/libweave/src/config_unittest.cc b/libweave/src/config_unittest.cc
index f6f4fa0..613a1ca 100644
--- a/libweave/src/config_unittest.cc
+++ b/libweave/src/config_unittest.cc
@@ -218,7 +218,6 @@
         })";
         EXPECT_JSON_EQ(expected, *test::CreateValue(json));
       }));
-  EXPECT_CALL(config_store_, OnSettingsChanged(_)).Times(1);
 
   change.Commit();
 }
diff --git a/libweave/src/device_manager.cc b/libweave/src/device_manager.cc
index e7ddb23..7feadc0 100644
--- a/libweave/src/device_manager.cc
+++ b/libweave/src/device_manager.cc
@@ -64,6 +64,15 @@
   }
 }
 
+const Settings& DeviceManager::GetSettings() {
+  return device_info_->GetSettings();
+}
+
+void DeviceManager::AddSettingsChangedCallback(
+    const SettingsChangedCallback& callback) {
+  device_info_->GetMutableConfig()->AddOnChangedCallback(callback);
+}
+
 Commands* DeviceManager::GetCommands() {
   return command_manager_.get();
 }
diff --git a/libweave/src/device_manager.h b/libweave/src/device_manager.h
index 294a7cd..de294e7 100644
--- a/libweave/src/device_manager.h
+++ b/libweave/src/device_manager.h
@@ -26,6 +26,7 @@
   DeviceManager();
   ~DeviceManager() override;
 
+  // Device implementation.
   void Start(provider::ConfigStore* config_store,
              provider::TaskRunner* task_runner,
              provider::HttpClient* http_client,
@@ -34,7 +35,9 @@
              provider::HttpServer* http_server,
              provider::Wifi* wifi,
              provider::Bluetooth* bluetooth) override;
-
+  const Settings& GetSettings() override;
+  void AddSettingsChangedCallback(
+      const SettingsChangedCallback& callback) override;
   Commands* GetCommands() override;
   State* GetState() override;
   Cloud* GetCloud() override;
diff --git a/libweave/src/device_registration_info.cc b/libweave/src/device_registration_info.cc
index a5799d4..a011826 100644
--- a/libweave/src/device_registration_info.cc
+++ b/libweave/src/device_registration_info.cc
@@ -477,11 +477,6 @@
   callback.Run(registration_status_);
 }
 
-void DeviceRegistrationInfo::AddOnConfigChangedCallback(
-    const Config::OnChangedCallback& callback) {
-  config_->AddOnChangedCallback(callback);
-}
-
 std::unique_ptr<base::DictionaryValue>
 DeviceRegistrationInfo::BuildDeviceResource(ErrorPtr* error) {
   // Limit only to commands that are visible to the cloud.
diff --git a/libweave/src/device_registration_info.h b/libweave/src/device_registration_info.h
index 77820e0..752a4e2 100644
--- a/libweave/src/device_registration_info.h
+++ b/libweave/src/device_registration_info.h
@@ -85,9 +85,6 @@
                            const std::string& service_url,
                            ErrorPtr* error) override;
 
-  // Add callback to listen for changes in config.
-  void AddOnConfigChangedCallback(const Config::OnChangedCallback& callback);
-
   // Returns the GCD service request URL. If |subpath| is specified, it is
   // appended to the base URL which is normally
   //    https://www.googleapis.com/clouddevices/v1/".
diff --git a/libweave/src/device_registration_info_unittest.cc b/libweave/src/device_registration_info_unittest.cc
index 2a059fb..e17ecbe 100644
--- a/libweave/src/device_registration_info_unittest.cc
+++ b/libweave/src/device_registration_info_unittest.cc
@@ -482,11 +482,6 @@
         return ReplyWithJson(200, json);
       })));
 
-  Settings saved_settings;
-  EXPECT_CALL(config_store_, OnSettingsChanged(_))
-      .Times(AtLeast(1))
-      .WillRepeatedly(SaveArg<0>(&saved_settings));
-
   std::string cloud_id =
       dev_reg_->RegisterDevice(test_data::kClaimTicketId, nullptr);
 
diff --git a/libweave/src/privet/cloud_delegate.cc b/libweave/src/privet/cloud_delegate.cc
index cccb6ce..13f48a0 100644
--- a/libweave/src/privet/cloud_delegate.cc
+++ b/libweave/src/privet/cloud_delegate.cc
@@ -47,7 +47,7 @@
         device_{device},
         command_manager_{command_manager},
         state_manager_{state_manager} {
-    device_->AddOnConfigChangedCallback(base::Bind(
+    device_->GetMutableConfig()->AddOnChangedCallback(base::Bind(
         &CloudDelegateImpl::OnConfigChanged, weak_factory_.GetWeakPtr()));
     device_->AddOnRegistrationChangedCallback(base::Bind(
         &CloudDelegateImpl::OnRegistrationChanged, weak_factory_.GetWeakPtr()));