Home | History | Annotate | Download | only in notifier
      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/notifier/invalidation_util.h"
      6 
      7 #include <ostream>
      8 #include <sstream>
      9 
     10 #include "base/json/json_writer.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/values.h"
     13 #include "google/cacheinvalidation/include/types.h"
     14 #include "google/cacheinvalidation/types.pb.h"
     15 
     16 namespace invalidation {
     17 void PrintTo(const invalidation::ObjectId& id, std::ostream* os) {
     18   *os << syncer::ObjectIdToString(id);
     19 }
     20 }  // namespace invalidation
     21 
     22 namespace syncer {
     23 
     24 bool ObjectIdLessThan::operator()(const invalidation::ObjectId& lhs,
     25                                   const invalidation::ObjectId& rhs) const {
     26   return (lhs.source() < rhs.source()) ||
     27          (lhs.source() == rhs.source() && lhs.name() < rhs.name());
     28 }
     29 
     30 bool RealModelTypeToObjectId(ModelType model_type,
     31                              invalidation::ObjectId* object_id) {
     32   std::string notification_type;
     33   if (!RealModelTypeToNotificationType(model_type, &notification_type)) {
     34     return false;
     35   }
     36   object_id->Init(ipc::invalidation::ObjectSource::CHROME_SYNC,
     37                   notification_type);
     38   return true;
     39 }
     40 
     41 bool ObjectIdToRealModelType(const invalidation::ObjectId& object_id,
     42                              ModelType* model_type) {
     43   return NotificationTypeToRealModelType(object_id.name(), model_type);
     44 }
     45 
     46 scoped_ptr<base::DictionaryValue> ObjectIdToValue(
     47     const invalidation::ObjectId& object_id) {
     48   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
     49   value->SetInteger("source", object_id.source());
     50   value->SetString("name", object_id.name());
     51   return value.Pass();
     52 }
     53 
     54 bool ObjectIdFromValue(const base::DictionaryValue& value,
     55                        invalidation::ObjectId* out) {
     56   *out = invalidation::ObjectId();
     57   std::string name;
     58   int source = 0;
     59   if (!value.GetInteger("source", &source) ||
     60       !value.GetString("name", &name)) {
     61     return false;
     62   }
     63   *out = invalidation::ObjectId(source, name);
     64   return true;
     65 }
     66 
     67 std::string ObjectIdToString(
     68     const invalidation::ObjectId& object_id) {
     69   scoped_ptr<base::DictionaryValue> value(ObjectIdToValue(object_id));
     70   std::string str;
     71   base::JSONWriter::Write(value.get(), &str);
     72   return str;
     73 }
     74 
     75 ObjectIdSet ModelTypeSetToObjectIdSet(ModelTypeSet model_types) {
     76   ObjectIdSet ids;
     77   for (ModelTypeSet::Iterator it = model_types.First(); it.Good(); it.Inc()) {
     78     invalidation::ObjectId model_type_as_id;
     79     if (!RealModelTypeToObjectId(it.Get(), &model_type_as_id)) {
     80       DLOG(WARNING) << "Invalid model type " << it.Get();
     81       continue;
     82     }
     83     ids.insert(model_type_as_id);
     84   }
     85   return ids;
     86 }
     87 
     88 ModelTypeSet ObjectIdSetToModelTypeSet(const ObjectIdSet& ids) {
     89   ModelTypeSet model_types;
     90   for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
     91     ModelType model_type;
     92     if (!ObjectIdToRealModelType(*it, &model_type)) {
     93       DLOG(WARNING) << "Invalid object ID " << ObjectIdToString(*it);
     94       continue;
     95     }
     96     model_types.Put(model_type);
     97   }
     98   return model_types;
     99 }
    100 
    101 std::string InvalidationToString(
    102     const invalidation::Invalidation& invalidation) {
    103   std::stringstream ss;
    104   ss << "{ ";
    105   ss << "object_id: " << ObjectIdToString(invalidation.object_id()) << ", ";
    106   ss << "version: " << invalidation.version();
    107   ss << " }";
    108   return ss.str();
    109 }
    110 
    111 }  // namespace syncer
    112