Home | History | Annotate | Download | only in engine
      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 #include "sync/engine/get_updates_delegate.h"
      6 
      7 #include "sync/engine/directory_update_handler.h"
      8 #include "sync/engine/get_updates_processor.h"
      9 #include "sync/internal_api/public/events/configure_get_updates_request_event.h"
     10 #include "sync/internal_api/public/events/normal_get_updates_request_event.h"
     11 #include "sync/internal_api/public/events/poll_get_updates_request_event.h"
     12 
     13 namespace syncer {
     14 
     15 namespace {
     16 
     17 void NonPassiveApplyUpdates(
     18     ModelTypeSet gu_types,
     19     sessions::StatusController* status_controller,
     20     UpdateHandlerMap* update_handler_map) {
     21   for (UpdateHandlerMap::iterator it = update_handler_map->begin();
     22        it != update_handler_map->end(); ++it) {
     23     if (gu_types.Has(it->first))
     24       it->second->ApplyUpdates(status_controller);
     25   }
     26 }
     27 
     28 void PassiveApplyUpdates(
     29     ModelTypeSet gu_types,
     30     sessions::StatusController* status_controller,
     31     UpdateHandlerMap* update_handler_map) {
     32   for (UpdateHandlerMap::iterator it = update_handler_map->begin();
     33        it != update_handler_map->end(); ++it) {
     34     if (gu_types.Has(it->first))
     35       it->second->PassiveApplyUpdates(status_controller);
     36   }
     37 }
     38 
     39 }  // namespace
     40 
     41 GetUpdatesDelegate::GetUpdatesDelegate() {}
     42 
     43 GetUpdatesDelegate::~GetUpdatesDelegate() {}
     44 
     45 NormalGetUpdatesDelegate::NormalGetUpdatesDelegate(
     46     const sessions::NudgeTracker& nudge_tracker)
     47   : nudge_tracker_(nudge_tracker) {}
     48 
     49 NormalGetUpdatesDelegate::~NormalGetUpdatesDelegate() {}
     50 
     51 // This function assumes the progress markers have already been populated.
     52 void NormalGetUpdatesDelegate::HelpPopulateGuMessage(
     53     sync_pb::GetUpdatesMessage* get_updates) const {
     54   // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
     55   get_updates->mutable_caller_info()->set_source(
     56       nudge_tracker_.GetLegacySource());
     57 
     58   // Set the new and improved version of source, too.
     59   get_updates->set_get_updates_origin(sync_pb::SyncEnums::GU_TRIGGER);
     60   get_updates->set_is_retry(nudge_tracker_.IsRetryRequired());
     61 
     62   // Special case: A GU performed for no other reason than retry will have its
     63   // origin set to RETRY.
     64   if (nudge_tracker_.GetLegacySource() == sync_pb::GetUpdatesCallerInfo::RETRY)
     65     get_updates->set_get_updates_origin(sync_pb::SyncEnums::RETRY);
     66 
     67   // Fill in the notification hints.
     68   for (int i = 0; i < get_updates->from_progress_marker_size(); ++i) {
     69     sync_pb::DataTypeProgressMarker* progress_marker =
     70         get_updates->mutable_from_progress_marker(i);
     71     ModelType type = GetModelTypeFromSpecificsFieldNumber(
     72         progress_marker->data_type_id());
     73 
     74     DCHECK(!nudge_tracker_.IsTypeThrottled(type))
     75         << "Throttled types should have been removed from the request_types.";
     76 
     77     nudge_tracker_.SetLegacyNotificationHint(type, progress_marker);
     78     nudge_tracker_.FillProtoMessage(
     79         type,
     80         progress_marker->mutable_get_update_triggers());
     81   }
     82 }
     83 
     84 void NormalGetUpdatesDelegate::ApplyUpdates(
     85     ModelTypeSet gu_types,
     86     sessions::StatusController* status_controller,
     87     UpdateHandlerMap* update_handler_map) const {
     88   NonPassiveApplyUpdates(gu_types, status_controller, update_handler_map);
     89 }
     90 
     91 scoped_ptr<ProtocolEvent> NormalGetUpdatesDelegate::GetNetworkRequestEvent(
     92     base::Time timestamp,
     93     const sync_pb::ClientToServerMessage& request) const {
     94   return scoped_ptr<ProtocolEvent>(
     95       new NormalGetUpdatesRequestEvent(timestamp, nudge_tracker_, request));
     96 }
     97 
     98 ConfigureGetUpdatesDelegate::ConfigureGetUpdatesDelegate(
     99     sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) : source_(source) {}
    100 
    101 ConfigureGetUpdatesDelegate::~ConfigureGetUpdatesDelegate() {}
    102 
    103 void ConfigureGetUpdatesDelegate::HelpPopulateGuMessage(
    104     sync_pb::GetUpdatesMessage* get_updates) const {
    105   get_updates->mutable_caller_info()->set_source(source_);
    106   get_updates->set_get_updates_origin(ConvertConfigureSourceToOrigin(source_));
    107 }
    108 
    109 void ConfigureGetUpdatesDelegate::ApplyUpdates(
    110     ModelTypeSet gu_types,
    111     sessions::StatusController* status_controller,
    112     UpdateHandlerMap* update_handler_map) const {
    113   PassiveApplyUpdates(gu_types, status_controller, update_handler_map);
    114 }
    115 
    116 scoped_ptr<ProtocolEvent> ConfigureGetUpdatesDelegate::GetNetworkRequestEvent(
    117     base::Time timestamp,
    118     const sync_pb::ClientToServerMessage& request) const {
    119   return scoped_ptr<ProtocolEvent>(
    120       new ConfigureGetUpdatesRequestEvent(
    121           timestamp,
    122           ConvertConfigureSourceToOrigin(source_),
    123           request));
    124 }
    125 
    126 sync_pb::SyncEnums::GetUpdatesOrigin
    127 ConfigureGetUpdatesDelegate::ConvertConfigureSourceToOrigin(
    128     sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
    129   switch (source) {
    130     // Configurations:
    131     case sync_pb::GetUpdatesCallerInfo::NEWLY_SUPPORTED_DATATYPE:
    132       return sync_pb::SyncEnums::NEWLY_SUPPORTED_DATATYPE;
    133     case sync_pb::GetUpdatesCallerInfo::MIGRATION:
    134       return sync_pb::SyncEnums::MIGRATION;
    135     case sync_pb::GetUpdatesCallerInfo::RECONFIGURATION:
    136       return sync_pb::SyncEnums::RECONFIGURATION;
    137     case sync_pb::GetUpdatesCallerInfo::NEW_CLIENT:
    138       return sync_pb::SyncEnums::NEW_CLIENT;
    139     case sync_pb::GetUpdatesCallerInfo::PROGRAMMATIC:
    140       return sync_pb::SyncEnums::PROGRAMMATIC;
    141     default:
    142       NOTREACHED();
    143       return sync_pb::SyncEnums::UNKNOWN_ORIGIN;
    144   }
    145 }
    146 
    147 PollGetUpdatesDelegate::PollGetUpdatesDelegate() {}
    148 
    149 PollGetUpdatesDelegate::~PollGetUpdatesDelegate() {}
    150 
    151 void PollGetUpdatesDelegate::HelpPopulateGuMessage(
    152     sync_pb::GetUpdatesMessage* get_updates) const {
    153   // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
    154   get_updates->mutable_caller_info()->set_source(
    155       sync_pb::GetUpdatesCallerInfo::PERIODIC);
    156 
    157   // Set the new and improved version of source, too.
    158   get_updates->set_get_updates_origin(sync_pb::SyncEnums::PERIODIC);
    159 }
    160 
    161 void PollGetUpdatesDelegate::ApplyUpdates(
    162     ModelTypeSet gu_types,
    163     sessions::StatusController* status_controller,
    164     UpdateHandlerMap* update_handler_map) const {
    165   NonPassiveApplyUpdates(gu_types, status_controller, update_handler_map);
    166 }
    167 
    168 scoped_ptr<ProtocolEvent> PollGetUpdatesDelegate::GetNetworkRequestEvent(
    169     base::Time timestamp,
    170     const sync_pb::ClientToServerMessage& request) const {
    171   return scoped_ptr<ProtocolEvent>(
    172       new PollGetUpdatesRequestEvent(timestamp, request));
    173 }
    174 
    175 }  // namespace syncer
    176