Home | History | Annotate | Download | only in engine
      1 // Copyright 2012 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 // A class to schedule syncer tasks intelligently.
      6 #ifndef SYNC_ENGINE_SYNC_SCHEDULER_H_
      7 #define SYNC_ENGINE_SYNC_SCHEDULER_H_
      8 
      9 #include <string>
     10 
     11 #include "base/callback.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/time/time.h"
     14 #include "sync/base/sync_export.h"
     15 #include "sync/engine/nudge_source.h"
     16 #include "sync/internal_api/public/base/model_type_invalidation_map.h"
     17 #include "sync/sessions/sync_session.h"
     18 
     19 namespace tracked_objects {
     20 class Location;
     21 }  // namespace tracked_objects
     22 
     23 namespace syncer {
     24 
     25 struct ServerConnectionEvent;
     26 
     27 struct SYNC_EXPORT_PRIVATE ConfigurationParams {
     28   ConfigurationParams();
     29   ConfigurationParams(
     30       const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource& source,
     31       ModelTypeSet types_to_download,
     32       const ModelSafeRoutingInfo& routing_info,
     33       const base::Closure& ready_task);
     34   ~ConfigurationParams();
     35 
     36   // Source for the configuration.
     37   sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source;
     38   // The types that should be downloaded.
     39   ModelTypeSet types_to_download;
     40   // The new routing info (superset of types to be downloaded).
     41   ModelSafeRoutingInfo routing_info;
     42   // Callback to invoke on configuration completion.
     43   base::Closure ready_task;
     44 };
     45 
     46 class SYNC_EXPORT_PRIVATE SyncScheduler
     47     : public sessions::SyncSession::Delegate {
     48  public:
     49   enum Mode {
     50     // In this mode, the thread only performs configuration tasks.  This is
     51     // designed to make the case where we want to download updates for a
     52     // specific type only, and not continue syncing until we are moved into
     53     // normal mode.
     54     CONFIGURATION_MODE,
     55     // Resumes polling and allows nudges, drops configuration tasks.  Runs
     56     // through entire sync cycle.
     57     NORMAL_MODE,
     58   };
     59 
     60   // All methods of SyncScheduler must be called on the same thread
     61   // (except for RequestEarlyExit()).
     62 
     63   SyncScheduler();
     64   virtual ~SyncScheduler();
     65 
     66   // Start the scheduler with the given mode.  If the scheduler is
     67   // already started, switch to the given mode, although some
     68   // scheduled tasks from the old mode may still run.
     69   virtual void Start(Mode mode) = 0;
     70 
     71   // Schedules the configuration task specified by |params|. Returns true if
     72   // the configuration task executed immediately, false if it had to be
     73   // scheduled for a later attempt. |params.ready_task| is invoked whenever the
     74   // configuration task executes.
     75   // Note: must already be in CONFIGURATION mode.
     76   virtual bool ScheduleConfiguration(const ConfigurationParams& params) = 0;
     77 
     78   // Request that any running syncer task stop as soon as possible and
     79   // cancel all scheduled tasks. This function can be called from any thread,
     80   // and should in fact be called from a thread that isn't the sync loop to
     81   // allow preempting ongoing sync cycles.
     82   virtual void RequestStop() = 0;
     83 
     84   // The meat and potatoes. All three of the following methods will post a
     85   // delayed task to attempt the actual nudge (see ScheduleNudgeImpl).
     86   //
     87   // NOTE: |desired_delay| is best-effort. If a nudge is already scheduled to
     88   // depart earlier than Now() + delay, the scheduler can and will prefer to
     89   // batch the two so that only one nudge is sent (at the earlier time). Also,
     90   // as always with delayed tasks and timers, it's possible the task gets run
     91   // any time after |desired_delay|.
     92 
     93   // The LocalNudge indicates that we've made a local change, and that the
     94   // syncer should plan to commit this to the server some time soon.
     95   virtual void ScheduleLocalNudge(
     96       const base::TimeDelta& desired_delay,
     97       ModelTypeSet types,
     98       const tracked_objects::Location& nudge_location) = 0;
     99 
    100   // The LocalRefreshRequest occurs when we decide for some reason to manually
    101   // request updates.  This should be used sparingly.  For example, one of its
    102   // uses is to fetch the latest tab sync data when it's relevant to the UI on
    103   // platforms where tab sync is not registered for invalidations.
    104   virtual void ScheduleLocalRefreshRequest(
    105       const base::TimeDelta& desired_delay,
    106       ModelTypeSet types,
    107       const tracked_objects::Location& nudge_location) = 0;
    108 
    109   // Invalidations are notifications the server sends to let us know when other
    110   // clients have committed data.  We need to contact the sync server (being
    111   // careful to pass along the "hints" delivered with those invalidations) in
    112   // order to fetch the update.
    113   virtual void ScheduleInvalidationNudge(
    114       const base::TimeDelta& desired_delay,
    115       const ModelTypeInvalidationMap& invalidation_map,
    116       const tracked_objects::Location& nudge_location) = 0;
    117 
    118   // Change status of notifications in the SyncSessionContext.
    119   virtual void SetNotificationsEnabled(bool notifications_enabled) = 0;
    120 
    121   virtual base::TimeDelta GetSessionsCommitDelay() const = 0;
    122 
    123   // Called when credentials are updated by the user.
    124   virtual void OnCredentialsUpdated() = 0;
    125 
    126   // Called when the network layer detects a connection status change.
    127   virtual void OnConnectionStatusChange() = 0;
    128 };
    129 
    130 }  // namespace syncer
    131 
    132 #endif  // SYNC_ENGINE_SYNC_SCHEDULER_H_
    133