Merge: Add write callback into SaveSettings function

Saving critical settings needs confirmation.
When command alters device config, it should be set "Done" only after
settings are actually saved.

BUG:25776798
Reviewed-on: https://weave-review.googlesource.com/2199
Reviewed-by: Alex Vakulenko <avakulenko@google.com>
(cherry picked from commit 42e508f2559e019d2fcc8f88adfd184b7a6bc3a4)

Change-Id: I693e3c17b3f2f707c8df7af29eefd48362980bce
Reviewed-on: https://weave-review.googlesource.com/2421
Reviewed-by: Vitaly Buka <vitalybuka@google.com>
diff --git a/examples/provider/file_config_store.cc b/examples/provider/file_config_store.cc
index af887a7..31efaa7 100644
--- a/examples/provider/file_config_store.cc
+++ b/examples/provider/file_config_store.cc
@@ -12,14 +12,19 @@
 #include <string>
 #include <vector>
 
+#include <base/bind.h>
+
 namespace weave {
 namespace examples {
 
 const char kSettingsDir[] = "/var/lib/weave/";
 
 FileConfigStore::FileConfigStore(bool disable_security,
-                                 const std::string& model_id)
-    : disable_security_{disable_security}, model_id_{model_id} {}
+                                 const std::string& model_id,
+                                 provider::TaskRunner* task_runner)
+    : disable_security_{disable_security},
+      model_id_{model_id},
+      task_runner_{task_runner} {}
 
 std::string FileConfigStore::GetPath(const std::string& name) const {
   std::string path{kSettingsDir};
@@ -72,11 +77,14 @@
 }
 
 void FileConfigStore::SaveSettings(const std::string& name,
-                                   const std::string& settings) {
+                                   const std::string& settings,
+                                   const DoneCallback& callback) {
   CHECK(mkdir(kSettingsDir, S_IRWXU) == 0 || errno == EEXIST);
   LOG(INFO) << "Saving settings to " << GetPath(name);
   std::ofstream str(GetPath(name));
   str << settings;
+  if (!callback.is_null())
+    task_runner_->PostDelayedTask(FROM_HERE, base::Bind(callback, nullptr), {});
 }
 
 }  // namespace examples
diff --git a/examples/provider/file_config_store.h b/examples/provider/file_config_store.h
index 214194e..e7398d1 100644
--- a/examples/provider/file_config_store.h
+++ b/examples/provider/file_config_store.h
@@ -10,18 +10,22 @@
 #include <vector>
 
 #include <weave/provider/config_store.h>
+#include <weave/provider/task_runner.h>
 
 namespace weave {
 namespace examples {
 
 class FileConfigStore : public provider::ConfigStore {
  public:
-  FileConfigStore(bool disable_security, const std::string& model_id);
+  FileConfigStore(bool disable_security,
+                  const std::string& model_id,
+                  provider::TaskRunner* task_runner);
 
   bool LoadDefaults(Settings* settings) override;
   std::string LoadSettings(const std::string& name) override;
   void SaveSettings(const std::string& name,
-                    const std::string& settings) override;
+                    const std::string& settings,
+                    const DoneCallback& callback) override;
 
   std::string LoadSettings() override;
 
@@ -29,6 +33,7 @@
   std::string GetPath(const std::string& name) const;
   const bool disable_security_;
   const std::string model_id_;
+  provider::TaskRunner* task_runner_{nullptr};
 };
 
 }  // namespace examples