Home | History | Annotate | Download | only in engine
      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/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/syncable/directory_manager.h"
     13 #include "chrome/browser/sync/syncable/model_type_payload_map.h"
     14 
     15 using syncable::ScopedDirLookup;
     16 
     17 namespace browser_sync {
     18 using sessions::StatusController;
     19 using sessions::SyncSession;
     20 using std::string;
     21 using syncable::FIRST_REAL_MODEL_TYPE;
     22 using syncable::MODEL_TYPE_COUNT;
     23 
     24 DownloadUpdatesCommand::DownloadUpdatesCommand() {}
     25 DownloadUpdatesCommand::~DownloadUpdatesCommand() {}
     26 
     27 void 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   // Request updates for all enabled types.
     45   syncable::ModelTypeBitSet enabled_types;
     46   const syncable::ModelTypePayloadMap& type_payload_map =
     47       session->source().types;
     48   for (ModelSafeRoutingInfo::const_iterator i = session->routing_info().begin();
     49        i != session->routing_info().end(); ++i) {
     50     syncable::ModelType model_type = syncable::ModelTypeFromInt(i->first);
     51     enabled_types[i->first] = true;
     52     sync_pb::DataTypeProgressMarker* progress_marker =
     53         get_updates->add_from_progress_marker();
     54     dir->GetDownloadProgress(model_type, progress_marker);
     55 
     56     // Set notification hint if present.
     57     syncable::ModelTypePayloadMap::const_iterator type_payload =
     58         type_payload_map.find(i->first);
     59     if (type_payload != type_payload_map.end()) {
     60       progress_marker->set_notification_hint(type_payload->second);
     61     }
     62   }
     63 
     64   VLOG(1) << "Getting updates for types " << enabled_types.to_string();
     65   DCHECK(enabled_types.any());
     66 
     67   // We want folders for our associated types, always.  If we were to set
     68   // this to false, the server would send just the non-container items
     69   // (e.g. Bookmark URLs but not their containing folders).
     70   get_updates->set_fetch_folders(true);
     71 
     72   // Set GetUpdatesMessage.GetUpdatesCallerInfo information.
     73   get_updates->mutable_caller_info()->set_source(
     74       session->TestAndSetSource().updates_source);
     75   get_updates->mutable_caller_info()->set_notifications_enabled(
     76       session->context()->notifications_enabled());
     77 
     78   SyncerProtoUtil::AddRequestBirthday(dir, &client_to_server_message);
     79 
     80   bool ok = SyncerProtoUtil::PostClientToServerMessage(
     81       client_to_server_message,
     82       &update_response,
     83       session);
     84 
     85   VLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString(
     86       update_response);
     87 
     88   StatusController* status = session->status_controller();
     89   status->set_updates_request_types(enabled_types);
     90   if (!ok) {
     91     status->increment_num_consecutive_errors();
     92     status->mutable_updates_response()->Clear();
     93     LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates";
     94     return;
     95   }
     96 
     97   status->mutable_updates_response()->CopyFrom(update_response);
     98 
     99   VLOG(1) << "GetUpdates "
    100           << " returned " << update_response.get_updates().entries_size()
    101           << " updates and indicated "
    102           << update_response.get_updates().changes_remaining()
    103           << " updates left on server.";
    104 }
    105 
    106 void DownloadUpdatesCommand::SetRequestedTypes(
    107     const syncable::ModelTypeBitSet& target_datatypes,
    108     sync_pb::EntitySpecifics* filter_protobuf) {
    109   // The datatypes which should be synced are dictated by the value of the
    110   // ModelSafeRoutingInfo.  If a datatype is in the routing info map, it
    111   // should be synced (even if it's GROUP_PASSIVE).
    112   int requested_type_count = 0;
    113   for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
    114     if (target_datatypes[i]) {
    115       requested_type_count++;
    116       syncable::AddDefaultExtensionValue(syncable::ModelTypeFromInt(i),
    117                                          filter_protobuf);
    118     }
    119   }
    120   DCHECK_LT(0, requested_type_count) << "Doing GetUpdates with empty filter.";
    121 }
    122 
    123 }  // namespace browser_sync
    124