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
26#include "lookup_key_util.h"
27
28namespace i18n {
29namespace addressinput {
30
31class Downloader;
32class Storage;
33
34// Retrieves data. Sample usage:
35//    Storage* storage = ...;
36//    Downloader* downloader = ...;
37//    Retriever retriever("https://i18napis.appspot.com/ssl-address/",
38//                        downloader, storage);
39//    scoped_ptr<Retriever::Callback> retrieved(BuildCallback(
40//        this, &MyClass::OnDataRetrieved));
41//    retriever.Retrieve("data/CA/AB--fr", *retrieved);
42class Retriever {
43 public:
44  typedef i18n::addressinput::Callback<std::string, std::string> Callback;
45
46  // Takes ownership of |downloader| and |storage|.
47  Retriever(const std::string& validation_data_url,
48            const Downloader* downloader,
49            Storage* storage);
50  ~Retriever();
51
52  // Retrieves the data for |key| and invokes the |retrieved| callback. Checks
53  // for the data in storage first. If storage does not have the data for |key|,
54  // then downloads the data and places it in storage.
55  void Retrieve(const std::string& key, const Callback& retrieved) const;
56
57 private:
58  const LookupKeyUtil lookup_key_util_;
59  scoped_ptr<const Downloader> downloader_;
60  scoped_ptr<Storage> storage_;
61
62  DISALLOW_COPY_AND_ASSIGN(Retriever);
63};
64
65}  // namespace addressinput
66}  // namespace i18n
67
68#endif  // I18N_ADDRESSINPUT_RETRIEVER_H_
69