1e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// Copyright (C) 2013 Google Inc.
2e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org//
3e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// Licensed under the Apache License, Version 2.0 (the "License");
4e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// you may not use this file except in compliance with the License.
5e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// You may obtain a copy of the License at
6e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org//
7e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// http://www.apache.org/licenses/LICENSE-2.0
8e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org//
9e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// Unless required by applicable law or agreed to in writing, software
10e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// distributed under the License is distributed on an "AS IS" BASIS,
11e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// See the License for the specific language governing permissions and
13e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// limitations under the License.
14e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org//
15e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// An object to retrieve data.
16e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
17e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org#ifndef I18N_ADDRESSINPUT_RETRIEVER_H_
18e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org#define I18N_ADDRESSINPUT_RETRIEVER_H_
19e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
20e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org#include <libaddressinput/callback.h>
21e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org#include <libaddressinput/util/basictypes.h>
22e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org#include <libaddressinput/util/scoped_ptr.h>
23e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
24e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org#include <string>
25e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
26e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.orgnamespace i18n {
27e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.orgnamespace addressinput {
28e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
298466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.comclass Source;
30e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.orgclass Storage;
31bf030e4c78d86bd72699e6d06d645ebb130bf8e4rouslan@chromium.orgclass ValidatingStorage;
32e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
33e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org// Retrieves data. Sample usage:
348466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com//    Source* source = ...;
35e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org//    Storage* storage = ...;
368466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com//    Retriever retriever(source, storage);
37e543c57d2256e9f144bbfc3783cc30a5d2129eaeroubert@google.com//    const scoped_ptr<const Retriever::Callback> retrieved(
38e543c57d2256e9f144bbfc3783cc30a5d2129eaeroubert@google.com//        BuildCallback(this, &MyClass::OnDataRetrieved));
39e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org//    retriever.Retrieve("data/CA/AB--fr", *retrieved);
40e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.orgclass Retriever {
41e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org public:
428c9b0e35847b85f4cc7434058e4e4ec5d5560c40roubert@google.com  typedef i18n::addressinput::Callback<const std::string&,
438c9b0e35847b85f4cc7434058e4e4ec5d5560c40roubert@google.com                                       const std::string&> Callback;
44e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
458466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  // Takes ownership of |source| and |storage|.
468466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  Retriever(const Source* source, Storage* storage);
47e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org  ~Retriever();
48e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
49e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org  // Retrieves the data for |key| and invokes the |retrieved| callback. Checks
508466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  // for the data in |storage_| first. If storage does not have the data for
518466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  // |key|, then gets the data from |source_| and places it in storage. If the
528466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  // data in storage is corrupted, then it's discarded and requested anew. If
538466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  // the data is stale, then it's requested anew. If the request fails, then
548466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  // stale data will be returned this one time. Any subsequent call to
558466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  // Retrieve() will attempt to get fresh data again.
56e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org  void Retrieve(const std::string& key, const Callback& retrieved) const;
57e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
58e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org private:
598466869ca5e66ec41d133c8ca2f25789028ef4dfroubert@google.com  scoped_ptr<const Source> source_;
60bf030e4c78d86bd72699e6d06d645ebb130bf8e4rouslan@chromium.org  scoped_ptr<ValidatingStorage> storage_;
61e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
62e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org  DISALLOW_COPY_AND_ASSIGN(Retriever);
63e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org};
64e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
65e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org}  // namespace addressinput
66e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org}  // namespace i18n
67e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org
68e281bd3ef5806b0cfd58079a7215a6ae94ecef96rouslan@chromium.org#endif  // I18N_ADDRESSINPUT_RETRIEVER_H_
69