1 // Copyright (c) 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 #include "sync/sessions/sync_session.h"
6
7 #include <algorithm>
8 #include <iterator>
9
10 #include "base/logging.h"
11 #include "sync/internal_api/public/base/model_type.h"
12 #include "sync/internal_api/public/engine/model_safe_worker.h"
13 #include "sync/syncable/directory.h"
14
15 namespace syncer {
16 namespace sessions {
17
18 // static
Build(SyncSessionContext * context,Delegate * delegate)19 SyncSession* SyncSession::Build(SyncSessionContext* context,
20 Delegate* delegate) {
21 return new SyncSession(context, delegate);
22 }
23
SyncSession(SyncSessionContext * context,Delegate * delegate)24 SyncSession::SyncSession(
25 SyncSessionContext* context,
26 Delegate* delegate)
27 : context_(context),
28 delegate_(delegate) {
29 status_controller_.reset(new StatusController());
30 }
31
~SyncSession()32 SyncSession::~SyncSession() {}
33
TakeSnapshot() const34 SyncSessionSnapshot SyncSession::TakeSnapshot() const {
35 return TakeSnapshotWithSource(sync_pb::GetUpdatesCallerInfo::UNKNOWN);
36 }
37
TakeSnapshotWithSource(sync_pb::GetUpdatesCallerInfo::GetUpdatesSource legacy_updates_source) const38 SyncSessionSnapshot SyncSession::TakeSnapshotWithSource(
39 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource legacy_updates_source) const {
40 syncable::Directory* dir = context_->directory();
41
42 ProgressMarkerMap download_progress_markers;
43 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
44 ModelType type(ModelTypeFromInt(i));
45 dir->GetDownloadProgressAsString(type, &download_progress_markers[type]);
46 }
47
48 std::vector<int> num_entries_by_type(MODEL_TYPE_COUNT, 0);
49 std::vector<int> num_to_delete_entries_by_type(MODEL_TYPE_COUNT, 0);
50 dir->CollectMetaHandleCounts(&num_entries_by_type,
51 &num_to_delete_entries_by_type);
52
53 SyncSessionSnapshot snapshot(
54 status_controller_->model_neutral_state(),
55 download_progress_markers,
56 delegate_->IsCurrentlyThrottled(),
57 status_controller_->num_encryption_conflicts(),
58 status_controller_->num_hierarchy_conflicts(),
59 status_controller_->num_server_conflicts(),
60 context_->notifications_enabled(),
61 dir->GetEntriesCount(),
62 status_controller_->sync_start_time(),
63 num_entries_by_type,
64 num_to_delete_entries_by_type,
65 legacy_updates_source);
66
67 return snapshot;
68 }
69
SendSyncCycleEndEventNotification(sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source)70 void SyncSession::SendSyncCycleEndEventNotification(
71 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
72 SyncCycleEvent event(SyncCycleEvent::SYNC_CYCLE_ENDED);
73 event.snapshot = TakeSnapshotWithSource(source);
74
75 DVLOG(1) << "Sending cycle end event with snapshot: "
76 << event.snapshot.ToString();
77 FOR_EACH_OBSERVER(SyncEngineEventListener,
78 *(context_->listeners()),
79 OnSyncCycleEvent(event));
80 }
81
SendEventNotification(SyncCycleEvent::EventCause cause)82 void SyncSession::SendEventNotification(SyncCycleEvent::EventCause cause) {
83 SyncCycleEvent event(cause);
84 event.snapshot = TakeSnapshot();
85
86 DVLOG(1) << "Sending event with snapshot: " << event.snapshot.ToString();
87 FOR_EACH_OBSERVER(SyncEngineEventListener,
88 *(context_->listeners()),
89 OnSyncCycleEvent(event));
90 }
91
SendProtocolEvent(const ProtocolEvent & event)92 void SyncSession::SendProtocolEvent(const ProtocolEvent& event) {
93 FOR_EACH_OBSERVER(SyncEngineEventListener,
94 *(context_->listeners()),
95 OnProtocolEvent(event));
96 }
97
98 } // namespace sessions
99 } // namespace syncer
100