1 // Copyright (C) 2014 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef I18N_ADDRESSINPUT_PRELOAD_SUPPLIER_H_
16 #define I18N_ADDRESSINPUT_PRELOAD_SUPPLIER_H_
18 #include <libaddressinput/callback.h>
19 #include <libaddressinput/supplier.h>
20 #include <libaddressinput/util/basictypes.h>
21 #include <libaddressinput/util/scoped_ptr.h>
29 namespace addressinput {
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.
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.
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.)
51 class PreloadSupplier : public Supplier {
53 typedef i18n::addressinput::Callback<const std::string&, int> Callback;
55 // Takes ownership of |source| and |storage|.
56 PreloadSupplier(const Source* source, Storage* storage);
57 virtual ~PreloadSupplier();
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);
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;
70 // Loads all address metadata available for |region_code|. (A typical data
71 // size is 10 kB. The largest is 250 kB.)
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);
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;
82 bool IsLoaded(const std::string& region_code) const;
83 bool IsPending(const std::string& region_code) const;
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;
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_;
97 DISALLOW_COPY_AND_ASSIGN(PreloadSupplier);
100 } // namespace addressinput
103 #endif // I18N_ADDRESSINPUT_PRELOAD_SUPPLIER_H_