Home | History | Annotate | Download | only in domain_reliability
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_DISPATCHER_H_
      6 #define COMPONENTS_DOMAIN_RELIABILITY_DISPATCHER_H_
      7 
      8 #include <set>
      9 
     10 #include "base/callback_forward.h"
     11 #include "base/time/time.h"
     12 #include "components/domain_reliability/domain_reliability_export.h"
     13 
     14 namespace tracked_objects {
     15 class Location;
     16 }  // namespace tracked_objects
     17 
     18 namespace domain_reliability {
     19 
     20 class MockableTime;
     21 
     22 // Runs tasks during a specified interval. Calling |RunEligibleTasks| gives any
     23 // task a chance to run early (if the minimum delay has already passed); tasks
     24 // that aren't run early will be run once their maximum delay has passed.
     25 //
     26 // (See scheduler.h for an explanation of how the intervals are chosen.)
     27 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityDispatcher {
     28  public:
     29   explicit DomainReliabilityDispatcher(MockableTime* time);
     30   ~DomainReliabilityDispatcher();
     31 
     32   // Schedules |task| to be executed between |min_delay| and |max_delay| from
     33   // now. The task will be run at most |max_delay| from now; once |min_delay|
     34   // has passed, any call to |RunEligibleTasks| will run the task earlier than
     35   // that.
     36   void ScheduleTask(const base::Closure& task,
     37                     base::TimeDelta min_delay,
     38                     base::TimeDelta max_delay);
     39 
     40   // Runs all tasks whose minimum delay has already passed.
     41   void RunEligibleTasks();
     42 
     43  private:
     44   struct Task;
     45 
     46   // Adds |task| to the set of all tasks, but not the set of eligible tasks.
     47   void MakeTaskWaiting(Task* task);
     48 
     49   // Adds |task| to the set of eligible tasks, and also the set of all tasks
     50   // if not already there.
     51   void MakeTaskEligible(Task* task);
     52 
     53   // Runs |task|'s callback, removes it from both sets, and deletes it.
     54   void RunAndDeleteTask(Task* task);
     55 
     56   MockableTime* time_;
     57   std::set<Task*> tasks_;
     58   std::set<Task*> eligible_tasks_;
     59 };
     60 
     61 }  // namespace domain_reliability
     62 
     63 #endif
     64