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// An object to retrieve data.
16
17#ifndef I18N_ADDRESSINPUT_RETRIEVER_H_
18#define I18N_ADDRESSINPUT_RETRIEVER_H_
19
20#include <libaddressinput/callback.h>
21#include <libaddressinput/util/basictypes.h>
22#include <libaddressinput/util/scoped_ptr.h>
23
24#include <string>
25
26namespace i18n {
27namespace addressinput {
28
29class Source;
30class Storage;
31class ValidatingStorage;
32
33// Retrieves data. Sample usage:
34//    Source* source = ...;
35//    Storage* storage = ...;
36//    Retriever retriever(source, storage);
37//    const scoped_ptr<const Retriever::Callback> retrieved(
38//        BuildCallback(this, &MyClass::OnDataRetrieved));
39//    retriever.Retrieve("data/CA/AB--fr", *retrieved);
40class Retriever {
41 public:
42  typedef i18n::addressinput::Callback<const std::string&,
43                                       const std::string&> Callback;
44
45  // Takes ownership of |source| and |storage|.
46  Retriever(const Source* source, Storage* storage);
47  ~Retriever();
48
49  // Retrieves the data for |key| and invokes the |retrieved| callback. Checks
50  // for the data in |storage_| first. If storage does not have the data for
51  // |key|, then gets the data from |source_| and places it in storage. If the
52  // data in storage is corrupted, then it's discarded and requested anew. If
53  // the data is stale, then it's requested anew. If the request fails, then
54  // stale data will be returned this one time. Any subsequent call to
55  // Retrieve() will attempt to get fresh data again.
56  void Retrieve(const std::string& key, const Callback& retrieved) const;
57
58 private:
59  scoped_ptr<const Source> source_;
60  scoped_ptr<ValidatingStorage> storage_;
61
62  DISALLOW_COPY_AND_ASSIGN(Retriever);
63};
64
65}  // namespace addressinput
66}  // namespace i18n
67
68#endif  // I18N_ADDRESSINPUT_RETRIEVER_H_
69