summaryrefslogtreecommitdiffstats
path: root/sync/engine/sync_session_job.cc
blob: 7dc5661be5c36c09d4cd22592982cf738d3a88e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/engine/sync_session_job.h"
#include "sync/internal_api/public/sessions/model_neutral_state.h"

namespace syncer {

SyncSessionJob::~SyncSessionJob() {}

SyncSessionJob::SyncSessionJob(
    Purpose purpose,
    base::TimeTicks start,
    scoped_ptr<sessions::SyncSession> session,
    const ConfigurationParams& config_params,
    const tracked_objects::Location& from_location)
    : purpose_(purpose),
      scheduled_start_(start),
      session_(session.Pass()),
      is_canary_(false),
      config_params_(config_params),
      finished_(NOT_FINISHED),
      from_location_(from_location) {
}

#define ENUM_CASE(x) case x: return #x; break;
const char* SyncSessionJob::GetPurposeString(SyncSessionJob::Purpose purpose) {
  switch (purpose) {
    ENUM_CASE(UNKNOWN);
    ENUM_CASE(POLL);
    ENUM_CASE(NUDGE);
    ENUM_CASE(CONFIGURATION);
  }
  NOTREACHED();
  return "";
}
#undef ENUM_CASE

bool SyncSessionJob::Finish(bool early_exit) {
  DCHECK_EQ(finished_, NOT_FINISHED);
  // Did we run through all SyncerSteps from start_step() to end_step()
  // until the SyncSession returned !HasMoreToSync()?
  // Note: if not, it's possible the scheduler hasn't started with
  // SyncShare yet, it's possible there is still more to sync in the session,
  // and it's also possible the job quit part way through due to a premature
  // exit condition (such as shutdown).
  finished_ = early_exit ? EARLY_EXIT : FINISHED;

  if (early_exit)
    return false;

  DCHECK(!session_->HasMoreToSync());

  // Did we hit any errors along the way?
  if (sessions::HasSyncerError(
      session_->status_controller().model_neutral_state())) {
    return false;
  }

  const sessions::ModelNeutralState& state(
      session_->status_controller().model_neutral_state());
  switch (purpose_) {
    case POLL:
    case NUDGE:
      DCHECK_NE(state.last_download_updates_result, UNSET);
      DCHECK_NE(state.commit_result, UNSET);
      break;
    case CONFIGURATION:
      DCHECK_NE(state.last_download_updates_result, UNSET);
      break;
    case UNKNOWN:
    default:
      NOTREACHED();
  }

  if (!config_params_.ready_task.is_null())
    config_params_.ready_task.Run();
  return true;
}

scoped_ptr<SyncSessionJob> SyncSessionJob::CloneAndAbandon() {
  DCHECK_EQ(finished_, NOT_FINISHED);
  // Clone |this|, and abandon it by NULL-ing session_.
  return scoped_ptr<SyncSessionJob> (new SyncSessionJob(
      purpose_, scheduled_start_, session_.Pass(),
      config_params_, from_location_));
}

scoped_ptr<SyncSessionJob> SyncSessionJob::Clone() const {
  DCHECK_GT(finished_, NOT_FINISHED);
  return scoped_ptr<SyncSessionJob>(new SyncSessionJob(
      purpose_, scheduled_start_, CloneSession().Pass(),
      config_params_, from_location_));
}

scoped_ptr<SyncSessionJob> SyncSessionJob::CloneFromLocation(
    const tracked_objects::Location& from_here) const {
  DCHECK_GT(finished_, NOT_FINISHED);
  return scoped_ptr<SyncSessionJob>(new SyncSessionJob(
      purpose_, scheduled_start_, CloneSession().Pass(),
      config_params_, from_here));
}

scoped_ptr<sessions::SyncSession> SyncSessionJob::CloneSession() const {
  return scoped_ptr<sessions::SyncSession>(
      new sessions::SyncSession(session_->context(),
          session_->delegate(), session_->source(), session_->routing_info(),
          session_->workers()));
}

bool SyncSessionJob::is_canary() const {
  return is_canary_;
}

SyncSessionJob::Purpose SyncSessionJob::purpose() const {
  return purpose_;
}

base::TimeTicks SyncSessionJob::scheduled_start() const {
  return scheduled_start_;
}

void SyncSessionJob::set_scheduled_start(base::TimeTicks start) {
  scheduled_start_ = start;
};

const sessions::SyncSession* SyncSessionJob::session() const {
  return session_.get();
}

sessions::SyncSession* SyncSessionJob::mutable_session() {
  return session_.get();
}

const tracked_objects::Location& SyncSessionJob::from_location() const {
  return from_location_;
}

ConfigurationParams SyncSessionJob::config_params() const {
  return config_params_;
}

void SyncSessionJob::GrantCanaryPrivilege() {
  DCHECK_EQ(finished_, NOT_FINISHED);
  DVLOG(2) << "Granting canary priviliege to " << session_.get();
  is_canary_ = true;
}

SyncerStep SyncSessionJob::start_step() const {
  SyncerStep start, end;
  GetSyncerStepsForPurpose(purpose_, &start, &end);
  return start;
}

SyncerStep SyncSessionJob::end_step() const {
  SyncerStep start, end;
  GetSyncerStepsForPurpose(purpose_, &start, &end);
  return end;
}

// static
void SyncSessionJob::GetSyncerStepsForPurpose(Purpose purpose,
                                              SyncerStep* start,
                                              SyncerStep* end) {
  switch (purpose) {
    case SyncSessionJob::CONFIGURATION:
      *start = DOWNLOAD_UPDATES;
      *end = APPLY_UPDATES;
      return;
    case SyncSessionJob::NUDGE:
    case SyncSessionJob::POLL:
      *start = SYNCER_BEGIN;
      *end = SYNCER_END;
      return;
    default:
      NOTREACHED();
      *start = SYNCER_END;
      *end = SYNCER_END;
      return;
  }
}

}  // namespace syncer