summaryrefslogtreecommitdiffstats
path: root/third_party/libaddressinput/chromium/chrome_storage_impl.h
blob: 6a08adc59e2ac7ed27e832d9715cfc691a69238b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_STORAGE_IMPL_H_
#define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_STORAGE_IMPL_H_

#include <list>
#include <string>

#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/scoped_observer.h"
#include "components/prefs/pref_store.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"

class WriteablePrefStore;

namespace autofill {

// An implementation of the Storage interface which passes through to an
// underlying WriteablePrefStore.
class ChromeStorageImpl : public ::i18n::addressinput::Storage,
                          public PrefStore::Observer {
 public:
  // |store| must outlive |this|.
  explicit ChromeStorageImpl(WriteablePrefStore* store);
  virtual ~ChromeStorageImpl();

  // ::i18n::addressinput::Storage implementation.
  virtual void Put(const std::string& key, std::string* data) override;
  virtual void Get(const std::string& key, const Callback& data_ready)
      const override;

  // PrefStore::Observer implementation.
  virtual void OnPrefValueChanged(const std::string& key) override;
  virtual void OnInitializationCompleted(bool succeeded) override;

 private:
  struct Request {
    Request(const std::string& key, const Callback& callback);

    std::string key;
    const Callback& callback;
  };

  // Non-const version of Get().
  void DoGet(const std::string& key, const Callback& data_ready);

  WriteablePrefStore* backing_store_;  // weak

  // Get requests that haven't yet been serviced.
  ScopedVector<Request> outstanding_requests_;

  ScopedObserver<PrefStore, ChromeStorageImpl> scoped_observer_;

  DISALLOW_COPY_AND_ASSIGN(ChromeStorageImpl);
};

}  // namespace autofill

#endif  // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_STORAGE_IMPL_H_