download_updates_command.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2006-2009 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/download_updates_command.h"
6
7#include <string>
8
9#include "chrome/browser/sync/engine/syncer.h"
10#include "chrome/browser/sync/engine/syncer_proto_util.h"
11#include "chrome/browser/sync/engine/syncproto.h"
12#include "chrome/browser/sync/sessions/sync_session.h"
13#include "chrome/browser/sync/syncable/directory_manager.h"
14
15using syncable::ScopedDirLookup;
16
17namespace browser_sync {
18using sessions::StatusController;
19using sessions::SyncSession;
20using std::string;
21using syncable::FIRST_REAL_MODEL_TYPE;
22using syncable::MODEL_TYPE_COUNT;
23
24DownloadUpdatesCommand::DownloadUpdatesCommand() {}
25DownloadUpdatesCommand::~DownloadUpdatesCommand() {}
26
27void DownloadUpdatesCommand::ExecuteImpl(SyncSession* session) {
28  ClientToServerMessage client_to_server_message;
29  ClientToServerResponse update_response;
30
31  client_to_server_message.set_share(session->context()->account_name());
32  client_to_server_message.set_message_contents(
33      ClientToServerMessage::GET_UPDATES);
34  GetUpdatesMessage* get_updates =
35      client_to_server_message.mutable_get_updates();
36
37  ScopedDirLookup dir(session->context()->directory_manager(),
38                      session->context()->account_name());
39  if (!dir.good()) {
40    LOG(ERROR) << "Scoped dir lookup failed!";
41    return;
42  }
43
44  // Pick some subset of the enabled types where all types in the set
45  // are at the same last_download_timestamp.  Do an update for those types.
46  syncable::ModelTypeBitSet enabled_types;
47  for (ModelSafeRoutingInfo::const_iterator i = session->routing_info().begin();
48       i != session->routing_info().end(); ++i) {
49    enabled_types[i->first] = true;
50  }
51  syncable::MultiTypeTimeStamp target =
52      dir->GetTypesWithOldestLastDownloadTimestamp(enabled_types);
53  VLOG(1) << "Getting updates from ts " << target.timestamp
54          << " for types " << target.data_types.to_string()
55          << " (of possible " << enabled_types.to_string() << ")";
56  DCHECK(target.data_types.any());
57  target.data_types.set(syncable::TOP_LEVEL_FOLDER);  // Always fetched.
58
59  get_updates->set_from_timestamp(target.timestamp);
60
61  // Set the requested_types protobuf field so that we fetch all enabled types.
62  SetRequestedTypes(target.data_types, get_updates->mutable_requested_types());
63
64  // We want folders for our associated types, always.  If we were to set
65  // this to false, the server would send just the non-container items
66  // (e.g. Bookmark URLs but not their containing folders).
67  get_updates->set_fetch_folders(true);
68
69  // Set GetUpdatesMessage.GetUpdatesCallerInfo information.
70  get_updates->mutable_caller_info()->set_source(session->TestAndSetSource());
71  get_updates->mutable_caller_info()->set_notifications_enabled(
72      session->context()->notifications_enabled());
73
74  SyncerProtoUtil::AddRequestBirthday(dir, &client_to_server_message);
75
76  bool ok = SyncerProtoUtil::PostClientToServerMessage(
77      client_to_server_message,
78      &update_response,
79      session);
80
81  VLOG(1) << SyncerProtoUtil::ClientToServerResponseDebugString(
82      update_response);
83
84  StatusController* status = session->status_controller();
85  status->set_updates_request_parameters(target);
86  if (!ok) {
87    status->increment_num_consecutive_errors();
88    status->mutable_updates_response()->Clear();
89    LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates";
90    return;
91  }
92
93  status->mutable_updates_response()->CopyFrom(update_response);
94
95  VLOG(1) << "GetUpdates from ts " << get_updates->from_timestamp()
96          << " returned " << update_response.get_updates().entries_size()
97          << " updates and indicated "
98          << update_response.get_updates().changes_remaining()
99          << " updates left on server.";
100}
101
102void DownloadUpdatesCommand::SetRequestedTypes(
103    const syncable::ModelTypeBitSet& target_datatypes,
104    sync_pb::EntitySpecifics* filter_protobuf) {
105  // The datatypes which should be synced are dictated by the value of the
106  // ModelSafeRoutingInfo.  If a datatype is in the routing info map, it
107  // should be synced (even if it's GROUP_PASSIVE).
108  int requested_type_count = 0;
109  for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
110    if (target_datatypes[i]) {
111      requested_type_count++;
112      syncable::AddDefaultExtensionValue(syncable::ModelTypeFromInt(i),
113                                         filter_protobuf);
114    }
115  }
116  DCHECK_LT(0, requested_type_count) << "Doing GetUpdates with empty filter.";
117}
118
119}  // namespace browser_sync
120