1// Copyright (C) 2014 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#ifndef I18N_ADDRESSINPUT_PRELOAD_SUPPLIER_H_
16#define I18N_ADDRESSINPUT_PRELOAD_SUPPLIER_H_
17
18#include <libaddressinput/callback.h>
19#include <libaddressinput/supplier.h>
20#include <libaddressinput/util/basictypes.h>
21#include <libaddressinput/util/scoped_ptr.h>
22
23#include <map>
24#include <set>
25#include <string>
26#include <vector>
27
28namespace i18n {
29namespace addressinput {
30
31class IndexMap;
32class LookupKey;
33class Retriever;
34class Rule;
35class Source;
36class Storage;
37
38// An implementation of the Supplier interface that owns a Retriever object,
39// through which it can load aggregated address metadata for a region when
40// instructed to, creating Rule objects and caching these. It also provides
41// methods to check whether metadata for a particular region is already loaded
42// or in progress of being loaded.
43//
44// When using a PreloadSupplier, it becomes possible to do synchronous address
45// validation using an asynchronous Source, and to have full control over when
46// network access is being done.
47//
48// The maximum size of this cache is naturally limited to the amount of data
49// available from the data server. (Currently this is less than 12,000 items of
50// in total less than 2 MB of JSON data.)
51class PreloadSupplier : public Supplier {
52 public:
53  typedef i18n::addressinput::Callback<const std::string&, int> Callback;
54
55  // Takes ownership of |source| and |storage|.
56  PreloadSupplier(const Source* source, Storage* storage);
57  virtual ~PreloadSupplier();
58
59  // Collects the metadata needed for |lookup_key| from the cache, then calls
60  // |supplied|. If the metadata needed isn't found in the cache, it will call
61  // the callback with status false.
62  virtual void Supply(const LookupKey& lookup_key,
63                      const Supplier::Callback& supplied);
64
65  // Should be called only when IsLoaded() returns true for the region code of
66  // the |lookup_key|. Can return NULL if the |lookup_key| does not correspond
67  // to any rule data. The caller does not own the result.
68  const Rule* GetRule(const LookupKey& lookup_key) const;
69
70  // Loads all address metadata available for |region_code|. (A typical data
71  // size is 10 kB. The largest is 250 kB.)
72  //
73  // If the rules are already in progress of being loaded, it does nothing.
74  // Calls |loaded| when the loading has finished.
75  void LoadRules(const std::string& region_code, const Callback& loaded);
76
77  // Returns a mapping of lookup keys to rules. Should be called only when
78  // IsLoaded() returns true for the |region_code|.
79  const std::map<std::string, const Rule*>& GetRulesForRegion(
80      const std::string& region_code) const;
81
82  bool IsLoaded(const std::string& region_code) const;
83  bool IsPending(const std::string& region_code) const;
84
85 private:
86  bool GetRuleHierarchy(const LookupKey& lookup_key,
87                        RuleHierarchy* hierarchy) const;
88  bool IsLoadedKey(const std::string& key) const;
89  bool IsPendingKey(const std::string& key) const;
90
91  const scoped_ptr<const Retriever> retriever_;
92  std::set<std::string> pending_;
93  const scoped_ptr<IndexMap> rule_index_;
94  std::vector<const Rule*> rule_storage_;
95  std::map<std::string, std::map<std::string, const Rule*> > region_rules_;
96
97  DISALLOW_COPY_AND_ASSIGN(PreloadSupplier);
98};
99
100}  // namespace addressinput
101}  // namespace i18n
102
103#endif  // I18N_ADDRESSINPUT_PRELOAD_SUPPLIER_H_
104