14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifndef CHROMEOS_SETTINGS_CROS_SETTINGS_PROVIDER_H_
64e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#define CHROMEOS_SETTINGS_CROS_SETTINGS_PROVIDER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chromeos/chromeos_export.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Value;
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chromeos {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class CHROMEOS_EXPORT CrosSettingsProvider {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The callback type that is called to notify the CrosSettings observers
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // about a setting change.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef base::Callback<void(const std::string&)> NotifyObserversCallback;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Possible results of a trusted check.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum TrustedStatus {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The trusted values were populated in the cache and can be accessed
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // until the next iteration of the message loop.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TRUSTED,
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Either a store or a load operation is in progress. The provided
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // callback will be invoked once the verification has finished.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEMPORARILY_UNTRUSTED,
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The verification of the trusted store has failed permanently. The
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // client should assume this state final and further checks for
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // trustedness will fail at least until the browser restarts.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PERMANENTLY_UNTRUSTED,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a new provider instance. |notify_cb| will be used to notify
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // about setting changes.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit CrosSettingsProvider(const NotifyObserversCallback& notify_cb);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~CrosSettingsProvider();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets |in_value| to given |path| in cros settings.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Set(const std::string& path, const base::Value& in_value);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets settings value of given |path| to |out_value|.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const base::Value* Get(const std::string& path) const = 0;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Requests the provider to fetch its values from a trusted store, if it
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // hasn't done so yet. Returns TRUSTED if the values returned by this provider
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are trusted during the current loop cycle. Otherwise returns
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TEMPORARILY_UNTRUSTED, and |callback| will be invoked later when trusted
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // values become available, PrepareTrustedValues() should be tried again in
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that case. Returns PERMANENTLY_UNTRUSTED if a permanent error has occurred.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual TrustedStatus PrepareTrustedValues(
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const base::Closure& callback) = 0;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the namespace prefix provided by this provider.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool HandlesSetting(const std::string& path) const = 0;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetNotifyObserversCallback(const NotifyObserversCallback& notify_cb);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notifies the observers about a setting change.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NotifyObservers(const std::string& path);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Does the real job for Set().
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void DoSet(const std::string& path,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     const base::Value& in_value) = 0;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Callback used to notify about setting changes.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotifyObserversCallback notify_cb_;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace chromeos
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif  // CHROMEOS_SETTINGS_CROS_SETTINGS_PROVIDER_H_
80