1// Copyright (c) 2011 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 "chrome/browser/sync/engine/apply_updates_command.h"
6
7#include "chrome/browser/sync/engine/update_applicator.h"
8#include "chrome/browser/sync/sessions/sync_session.h"
9#include "chrome/browser/sync/syncable/directory_manager.h"
10#include "chrome/browser/sync/syncable/syncable.h"
11
12namespace browser_sync {
13
14using sessions::SyncSession;
15
16ApplyUpdatesCommand::ApplyUpdatesCommand() {}
17ApplyUpdatesCommand::~ApplyUpdatesCommand() {}
18
19void ApplyUpdatesCommand::ModelChangingExecuteImpl(SyncSession* session) {
20  syncable::ScopedDirLookup dir(session->context()->directory_manager(),
21                                session->context()->account_name());
22  if (!dir.good()) {
23    LOG(ERROR) << "Scoped dir lookup failed!";
24    return;
25  }
26  syncable::WriteTransaction trans(dir, syncable::SYNCER, __FILE__, __LINE__);
27  syncable::Directory::UnappliedUpdateMetaHandles handles;
28  dir->GetUnappliedUpdateMetaHandles(&trans, &handles);
29
30  UpdateApplicator applicator(
31      session->context()->resolver(),
32      session->context()->directory_manager()->GetCryptographer(&trans),
33      handles.begin(), handles.end(), session->routing_info(),
34      session->status_controller()->group_restriction());
35  while (applicator.AttemptOneApplication(&trans)) {}
36  applicator.SaveProgressIntoSessionState(
37      session->status_controller()->mutable_conflict_progress(),
38      session->status_controller()->mutable_update_progress());
39
40  // This might be the first time we've fully completed a sync cycle, for
41  // some subset of the currently synced datatypes.
42  sessions::StatusController* status(session->status_controller());
43  if (status->ServerSaysNothingMoreToDownload()) {
44    syncable::ScopedDirLookup dir(session->context()->directory_manager(),
45                                  session->context()->account_name());
46    if (!dir.good()) {
47      LOG(ERROR) << "Scoped dir lookup failed!";
48      return;
49    }
50
51    for (int i = syncable::FIRST_REAL_MODEL_TYPE;
52         i < syncable::MODEL_TYPE_COUNT; ++i) {
53      syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
54      if (status->updates_request_types()[i]) {
55        // This gets persisted to the directory's backing store.
56        dir->set_initial_sync_ended_for_type(model_type, true);
57      }
58    }
59  }
60}
61
62}  // namespace browser_sync
63