Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: sync/engine/sync_session_job.cc

Issue 10917234: sync: make scheduling logic and job ownership more obvious. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test + comment + rebase Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/engine/sync_session_job.h"
6 #include "sync/internal_api/public/sessions/model_neutral_state.h"
7
8 namespace syncer {
9
10 SyncSessionJob::~SyncSessionJob() {}
11
12 SyncSessionJob::SyncSessionJob(
13 Purpose purpose,
14 const base::TimeTicks& start,
15 scoped_ptr<sessions::SyncSession> session,
16 const ConfigurationParams& config_params,
17 const tracked_objects::Location& from_location)
18 : purpose_(purpose),
19 scheduled_start_(start),
20 session_(session.Pass()),
21 is_canary_(false),
22 config_params_(config_params),
23 finished_(NOT_FINISHED),
24 from_location_(from_location) {
25 }
26
27 #define ENUM_CASE(x) case x: return #x; break;
28 const char* SyncSessionJob::GetPurposeString(SyncSessionJob::Purpose purpose) {
29 switch (purpose) {
30 ENUM_CASE(UNKNOWN);
31 ENUM_CASE(POLL);
32 ENUM_CASE(NUDGE);
33 ENUM_CASE(CONFIGURATION);
34 }
35 NOTREACHED();
36 return "";
37 }
38 #undef ENUM_CASE
39
40 void SyncSessionJob::Finish(bool early_exit) {
41 finished_ = early_exit ? EARLY_EXIT : FINISHED;
42 if (!Succeeded())
43 return;
44
45 if (!config_params_.ready_task.is_null())
46 config_params_.ready_task.Run();
47 }
48
49 bool SyncSessionJob::Succeeded() const {
50 // Did we run through all SyncerSteps from start_step() to end_step()
51 // until the SyncSession returned !HasMoreToSync()?
52 // Note: if not, it's possible the scheduler hasn't started with
53 // SyncShare yet, it's possible there is still more to sync in the session,
54 // and it's also possible the job quit part way through due to a premature
55 // exit condition (such as shutdown).
56 if (finished_ != FINISHED)
57 return false;
58
59 DCHECK(!session_->HasMoreToSync());
60
61 // Did we hit any errors along the way?
62 if (sessions::HasSyncerError(
63 session_->status_controller().model_neutral_state())) {
64 return false;
65 }
66
67 #ifndef NDEBUG
rlarocque 2012/09/24 21:56:47 I wouldn't bother with the #ifndef. The compiler
akalin 2012/09/25 22:37:24 better to use DCHECK_IS_ON() anyway
tim (not reviewing) 2012/10/08 00:20:03 Agree w/ both points. Removed.
68 const sessions::ModelNeutralState& state(
69 session_->status_controller().model_neutral_state());
70 switch (purpose_) {
71 case POLL:
72 case NUDGE:
73 DCHECK(state.last_download_updates_result != UNSET &&
74 state.commit_result != UNSET);
75 break;
76 case CONFIGURATION:
77 DCHECK_NE(state.last_download_updates_result, UNSET);
78 break;
79 case UNKNOWN:
80 default:
81 NOTREACHED();
82 }
83 #endif // NDEBUG
84
85 return true;
86 }
87
88 scoped_ptr<SyncSessionJob> SyncSessionJob::CloneAndAbandon() {
89 DCHECK_EQ(finished_, NOT_FINISHED);
90 // Clone |this|, and abandon it by NULL-ing session_.
91 return scoped_ptr<SyncSessionJob> (new SyncSessionJob(
92 purpose_, scheduled_start_, session_.Pass(),
93 config_params_, from_location_));
94 }
95
96 scoped_ptr<SyncSessionJob> SyncSessionJob::Clone() const {
97 DCHECK_GT(finished_, NOT_FINISHED);
98 return scoped_ptr<SyncSessionJob>(new SyncSessionJob(
99 purpose_, scheduled_start_, CloneSession().Pass(),
100 config_params_, from_location_));
101 }
102
103 scoped_ptr<SyncSessionJob> SyncSessionJob::CloneFromLocation(
104 const tracked_objects::Location& from_here) const {
105 DCHECK_GT(finished_, NOT_FINISHED);
106 return scoped_ptr<SyncSessionJob>(new SyncSessionJob(
107 purpose_, scheduled_start_, CloneSession().Pass(),
108 config_params_, from_here));
109 }
110
111 scoped_ptr<sessions::SyncSession> SyncSessionJob::CloneSession() const {
112 return scoped_ptr<sessions::SyncSession>(
113 new sessions::SyncSession(session_->context(),
114 session_->delegate(), session_->source(), session_->routing_info(),
115 session_->workers()));
116 }
117
118 bool SyncSessionJob::is_canary() const {
119 return is_canary_;
120 }
121
122 SyncSessionJob::Purpose SyncSessionJob::purpose() const {
123 return purpose_;
124 }
125
126 const base::TimeTicks& SyncSessionJob::scheduled_start() const {
127 return scheduled_start_;
128 }
129
130 void SyncSessionJob::set_scheduled_start(const base::TimeTicks& start) {
131 scheduled_start_ = start;
132 };
133
134 const sessions::SyncSession* SyncSessionJob::session() const {
135 return session_.get();
136 }
137
138 sessions::SyncSession* SyncSessionJob::mutable_session() {
139 return session_.get();
140 }
141
142 const tracked_objects::Location& SyncSessionJob::from_location() const {
143 return from_location_;
144 }
145
146 ConfigurationParams SyncSessionJob::config_params() const {
147 return config_params_;
148 }
149
150 void SyncSessionJob::GrantCanaryPrivilege() {
151 DCHECK_EQ(finished_, NOT_FINISHED);
152 DVLOG(2) << "Granting canary priviliege to " << session_.get();
153 is_canary_ = true;
154 }
155
156 SyncerStep SyncSessionJob::start_step() const {
157 SyncerStep start, end;
158 GetSyncerStepsForPurpose(purpose_, &start, &end);
159 return start;
160 }
161
162 SyncerStep SyncSessionJob::end_step() const {
163 SyncerStep start, end;
164 GetSyncerStepsForPurpose(purpose_, &start, &end);
165 return end;
166 }
167
168 // static
169 void SyncSessionJob::GetSyncerStepsForPurpose(Purpose purpose,
170 SyncerStep* start,
171 SyncerStep* end) {
172 switch (purpose) {
173 case SyncSessionJob::CONFIGURATION:
174 *start = DOWNLOAD_UPDATES;
175 *end = APPLY_UPDATES;
176 return;
177 case SyncSessionJob::NUDGE:
178 case SyncSessionJob::POLL:
179 *start = SYNCER_BEGIN;
180 *end = SYNCER_END;
181 return;
182 default:
183 NOTREACHED();
184 *start = SYNCER_END;
185 *end = SYNCER_END;
186 return;
187 }
188 }
189
190 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698