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
13namespace syncer {
14
15namespace {
16
17void 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
28void 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
41GetUpdatesDelegate::GetUpdatesDelegate() {}
42
43GetUpdatesDelegate::~GetUpdatesDelegate() {}
44
45NormalGetUpdatesDelegate::NormalGetUpdatesDelegate(
46    const sessions::NudgeTracker& nudge_tracker)
47  : nudge_tracker_(nudge_tracker) {}
48
49NormalGetUpdatesDelegate::~NormalGetUpdatesDelegate() {}
50
51// This function assumes the progress markers have already been populated.
52void 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
84void 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
91scoped_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
98ConfigureGetUpdatesDelegate::ConfigureGetUpdatesDelegate(
99    sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) : source_(source) {}
100
101ConfigureGetUpdatesDelegate::~ConfigureGetUpdatesDelegate() {}
102
103void 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
109void 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
116scoped_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
126sync_pb::SyncEnums::GetUpdatesOrigin
127ConfigureGetUpdatesDelegate::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    default:
140      NOTREACHED();
141      return sync_pb::SyncEnums::UNKNOWN_ORIGIN;
142  }
143}
144
145PollGetUpdatesDelegate::PollGetUpdatesDelegate() {}
146
147PollGetUpdatesDelegate::~PollGetUpdatesDelegate() {}
148
149void PollGetUpdatesDelegate::HelpPopulateGuMessage(
150    sync_pb::GetUpdatesMessage* get_updates) const {
151  // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information.
152  get_updates->mutable_caller_info()->set_source(
153      sync_pb::GetUpdatesCallerInfo::PERIODIC);
154
155  // Set the new and improved version of source, too.
156  get_updates->set_get_updates_origin(sync_pb::SyncEnums::PERIODIC);
157}
158
159void PollGetUpdatesDelegate::ApplyUpdates(
160    ModelTypeSet gu_types,
161    sessions::StatusController* status_controller,
162    UpdateHandlerMap* update_handler_map) const {
163  NonPassiveApplyUpdates(gu_types, status_controller, update_handler_map);
164}
165
166scoped_ptr<ProtocolEvent> PollGetUpdatesDelegate::GetNetworkRequestEvent(
167    base::Time timestamp,
168    const sync_pb::ClientToServerMessage& request) const {
169  return scoped_ptr<ProtocolEvent>(
170      new PollGetUpdatesRequestEvent(timestamp, request));
171}
172
173}  // namespace syncer
174