Home | History | Annotate | Download | only in engine
      1 // Copyright (c) 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 // Wrappers to help us work with ids and protobuffers.
      6 
      7 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCPROTO_H_
      8 #define CHROME_BROWSER_SYNC_ENGINE_SYNCPROTO_H_
      9 #pragma once
     10 
     11 #include "chrome/browser/sync/protocol/bookmark_specifics.pb.h"
     12 #include "chrome/browser/sync/protocol/password_specifics.pb.h"
     13 #include "chrome/browser/sync/protocol/preference_specifics.pb.h"
     14 #include "chrome/browser/sync/protocol/sync.pb.h"
     15 #include "chrome/browser/sync/syncable/model_type.h"
     16 #include "chrome/browser/sync/syncable/syncable_id.h"
     17 
     18 namespace browser_sync {
     19 
     20 template<class Base>
     21 class IdWrapper : public Base {
     22  public:
     23   IdWrapper() {}
     24   explicit IdWrapper(const Base& other) : Base(other) {
     25   }
     26   syncable::Id id() const {
     27     return syncable::Id::CreateFromServerId(Base::id_string());
     28   }
     29   void set_id(const syncable::Id& id) {
     30     Base::set_id_string(id.GetServerId());
     31   }
     32 };
     33 
     34 // These wrapper classes contain no data, so their super classes can be cast to
     35 // them directly.
     36 class SyncEntity : public IdWrapper<sync_pb::SyncEntity> {
     37  public:
     38   SyncEntity() {}
     39   explicit SyncEntity(const sync_pb::SyncEntity& other)
     40       : IdWrapper<sync_pb::SyncEntity>(other) {
     41   }
     42 
     43   void set_parent_id(const syncable::Id& id) {
     44     set_parent_id_string(id.GetServerId());
     45   }
     46   syncable::Id parent_id() const {
     47     return syncable::Id::CreateFromServerId(parent_id_string());
     48   }
     49   void set_old_parent_id(const syncable::Id& id) {
     50     IdWrapper<sync_pb::SyncEntity>::set_old_parent_id(
     51       id.GetServerId());
     52   }
     53   syncable::Id old_parent_id() const {
     54     return syncable::Id::CreateFromServerId(
     55       sync_pb::SyncEntity::old_parent_id());
     56   }
     57   // Binary predicate helper to determine whether an Entity represents a folder
     58   // or non-folder object. Use this instead of checking these properties
     59   // directly, because the addition of bookmarks to the protobuf schema
     60   // makes the check slightly more tricky.
     61   bool IsFolder() const {
     62     return ((has_folder() && folder()) ||
     63             (has_bookmarkdata() && bookmarkdata().bookmark_folder()));
     64   }
     65 
     66   syncable::ModelType GetModelType() const {
     67     return syncable::GetModelType(*this);
     68   }
     69 };
     70 
     71 class CommitResponse_EntryResponse
     72     : public IdWrapper<sync_pb::CommitResponse_EntryResponse> {
     73 };
     74 
     75 class ClientToServerMessage : public sync_pb::ClientToServerMessage {
     76  public:
     77   ClientToServerMessage() {
     78     set_protocol_version(protocol_version());
     79   }
     80 };
     81 
     82 typedef sync_pb::CommitMessage CommitMessage;
     83 typedef sync_pb::ClientToServerResponse ClientToServerResponse;
     84 typedef sync_pb::CommitResponse CommitResponse;
     85 typedef sync_pb::GetUpdatesResponse GetUpdatesResponse;
     86 typedef sync_pb::GetUpdatesMessage GetUpdatesMessage;
     87 
     88 }  // namespace browser_sync
     89 
     90 #endif  // CHROME_BROWSER_SYNC_ENGINE_SYNCPROTO_H_
     91