Home | History | Annotate | Download | only in libaddressinput
      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_ONDEMAND_SUPPLIER_H_
     16 #define I18N_ADDRESSINPUT_ONDEMAND_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 <string>
     25 
     26 namespace i18n {
     27 namespace addressinput {
     28 
     29 class Downloader;
     30 class LookupKey;
     31 class Retriever;
     32 class Rule;
     33 class Storage;
     34 
     35 // An implementation of the Supplier interface that owns a Retriever object,
     36 // through which it loads address metadata as needed, creating Rule objects and
     37 // caching these.
     38 //
     39 // When using an OndemandSupplier, address validation will benefit from address
     40 // metadata server synonym resolution, because the server will be contacted for
     41 // every new LookupKey (ie. every LookupKey that isn't on canonical form and
     42 // isn't already cached).
     43 //
     44 // The maximum size of this cache is naturally limited to the amount of data
     45 // available from the data server. (Currently this is less than 12,000 items of
     46 // in total less than 2 MB of JSON data.)
     47 class OndemandSupplier : public Supplier {
     48  public:
     49   // Takes ownership of |downloader| and |storage|. The |validation_data_url|
     50   // should be a URL to an address data server that |downloader| can access.
     51   //
     52   // (See the documentation for the Downloader implementation used for
     53   // information about what URLs are useable with that Downloader.)
     54   OndemandSupplier(const std::string& validation_data_url,
     55                    const Downloader* downloader,
     56                    Storage* storage);
     57   virtual ~OndemandSupplier();
     58 
     59   // Loads the metadata needed for |lookup_key|, then calls |supplied|.
     60   virtual void Supply(const LookupKey& lookup_key, const Callback& supplied);
     61 
     62  private:
     63   const scoped_ptr<const Retriever> retriever_;
     64   std::map<std::string, const Rule*> rule_cache_;
     65 
     66   DISALLOW_COPY_AND_ASSIGN(OndemandSupplier);
     67 };
     68 
     69 }  // namespace addressinput
     70 }  // namespace i18n
     71 
     72 #endif  // I18N_ADDRESSINPUT_ONDEMAND_SUPPLIER_H_
     73