1// Copyright (C) 2013 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// A wrapper object for Storage that stores data with a checksum and a
16// timestamp. The existence of checksum and timestamp fields is transparent to
17// the user of the object.
18
19#ifndef I18N_ADDRESSINPUT_VALIDATING_STORAGE_H_
20#define I18N_ADDRESSINPUT_VALIDATING_STORAGE_H_
21
22#include <libaddressinput/storage.h>
23#include <libaddressinput/util/basictypes.h>
24#include <libaddressinput/util/scoped_ptr.h>
25
26#include <string>
27
28namespace i18n {
29namespace addressinput {
30
31// Wraps Storage to add checksum and timestamp to stored data. Sample usage:
32//    scoped_ptr<Storage> file_storage = ...;
33//    ValidatingStorage storage(file_storage));
34//    storage.Put("key", new std::string("data"));
35//    const scoped_ptr<const ValidatingStorage::Callback> data_ready(
36//        BuildCallback(this, &MyClass::OnDataReady));
37//    storage.Get("key", *data_ready);
38class ValidatingStorage : public Storage {
39 public:
40  // Takes ownership of |storage|.
41  explicit ValidatingStorage(Storage* storage);
42  virtual ~ValidatingStorage();
43
44  // Storage implementation.
45  virtual void Put(const std::string& key, std::string* data);
46
47  // Storage implementation.
48  // If the data is invalid, then |data_ready| will be called with (false, key,
49  // empty-string). If the data is valid, but stale, then |data_ready| will be
50  // called with (false, key, stale-data). If the data is valid and fresh, then
51  // |data_ready| will be called with (true, key, fresh-data).
52  virtual void Get(const std::string& key, const Callback& data_ready) const;
53
54 private:
55  // The storage being wrapped.
56  scoped_ptr<Storage> wrapped_storage_;
57
58  DISALLOW_COPY_AND_ASSIGN(ValidatingStorage);
59};
60
61}  // namespace addressinput
62}  // namespace i18n
63
64#endif  // I18N_ADDRESSINPUT_VALIDATING_STORAGE_H_
65