Home | History | Annotate | Download | only in src
      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_SUPPLY_TASK_H_
     16 #define I18N_ADDRESSINPUT_ONDEMAND_SUPPLY_TASK_H_
     17 
     18 #include <libaddressinput/supplier.h>
     19 #include <libaddressinput/util/basictypes.h>
     20 #include <libaddressinput/util/scoped_ptr.h>
     21 
     22 #include <map>
     23 #include <set>
     24 #include <string>
     25 
     26 #include "retriever.h"
     27 
     28 namespace i18n {
     29 namespace addressinput {
     30 
     31 class LookupKey;
     32 class Retriever;
     33 class Rule;
     34 
     35 // An OndemandSupplyTask object encapsulates the information necessary to
     36 // retrieve the set of Rule objects corresponding to a LookupKey and call a
     37 // callback when that has been done. Calling the Retrieve() method will load
     38 // required metadata, then call the callback and delete the OndemandSupplyTask
     39 // object itself.
     40 class OndemandSupplyTask {
     41  public:
     42   OndemandSupplyTask(const LookupKey& lookup_key,
     43                      std::map<std::string, const Rule*>* rules,
     44                      const Supplier::Callback& supplied);
     45   ~OndemandSupplyTask();
     46 
     47   // Adds lookup key string |key| to the queue of data to be retrieved.
     48   void Queue(const std::string& key);
     49 
     50   // Retrieves and parses data for all queued keys, then calls |supplied_|.
     51   void Retrieve(const Retriever& retriever);
     52 
     53   Supplier::RuleHierarchy hierarchy_;
     54 
     55  private:
     56   void Load(bool success, const std::string& key, const std::string& data);
     57   void Loaded();
     58 
     59   std::set<std::string> pending_;
     60   const LookupKey& lookup_key_;
     61   std::map<std::string, const Rule*>* const rule_cache_;
     62   const Supplier::Callback& supplied_;
     63   const scoped_ptr<const Retriever::Callback> retrieved_;
     64   bool success_;
     65 
     66   DISALLOW_COPY_AND_ASSIGN(OndemandSupplyTask);
     67 };
     68 
     69 }  // namespace addressinput
     70 }  // namespace i18n
     71 
     72 #endif  // I18N_ADDRESSINPUT_ONDEMAND_SUPPLY_TASK_H_
     73