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.
5 #include "sync/engine/get_updates_delegate.h"
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"
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
);
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
);
41 GetUpdatesDelegate::GetUpdatesDelegate() {}
43 GetUpdatesDelegate::~GetUpdatesDelegate() {}
45 NormalGetUpdatesDelegate::NormalGetUpdatesDelegate(
46 const sessions::NudgeTracker
& nudge_tracker
)
47 : nudge_tracker_(nudge_tracker
) {}
49 NormalGetUpdatesDelegate::~NormalGetUpdatesDelegate() {}
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());
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());
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
);
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());
74 DCHECK(!nudge_tracker_
.IsTypeThrottled(type
))
75 << "Throttled types should have been removed from the request_types.";
77 nudge_tracker_
.SetLegacyNotificationHint(type
, progress_marker
);
78 nudge_tracker_
.FillProtoMessage(
80 progress_marker
->mutable_get_update_triggers());
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
);
91 scoped_ptr
<ProtocolEvent
> NormalGetUpdatesDelegate::GetNetworkRequestEvent(
93 const sync_pb::ClientToServerMessage
& request
) const {
94 return scoped_ptr
<ProtocolEvent
>(
95 new NormalGetUpdatesRequestEvent(timestamp
, nudge_tracker_
, request
));
98 ConfigureGetUpdatesDelegate::ConfigureGetUpdatesDelegate(
99 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source
) : source_(source
) {}
101 ConfigureGetUpdatesDelegate::~ConfigureGetUpdatesDelegate() {}
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_
));
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
);
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(
122 ConvertConfigureSourceToOrigin(source_
),
126 sync_pb::SyncEnums::GetUpdatesOrigin
127 ConfigureGetUpdatesDelegate::ConvertConfigureSourceToOrigin(
128 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source
) {
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
;
141 return sync_pb::SyncEnums::UNKNOWN_ORIGIN
;
145 PollGetUpdatesDelegate::PollGetUpdatesDelegate() {}
147 PollGetUpdatesDelegate::~PollGetUpdatesDelegate() {}
149 void 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
);
155 // Set the new and improved version of source, too.
156 get_updates
->set_get_updates_origin(sync_pb::SyncEnums::PERIODIC
);
159 void 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
);
166 scoped_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
));
173 } // namespace syncer