15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 50529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/cloud_devices/common/cloud_device_description.h" 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/json/json_reader.h" 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/json/json_writer.h" 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h" 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/values.h" 110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/cloud_devices/common/cloud_device_description_consts.h" 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace cloud_devices { 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)CloudDeviceDescription::CloudDeviceDescription() { 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Reset(); 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)CloudDeviceDescription::~CloudDeviceDescription() { 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void CloudDeviceDescription::Reset() { 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) root_.reset(new base::DictionaryValue); 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) root_->SetString(json::kVersion, json::kVersion10); 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool CloudDeviceDescription::InitFromDictionary( 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_ptr<base::DictionaryValue> root) { 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (!root) 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return false; 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Reset(); 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) root_ = root.Pass(); 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string version; 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) root_->GetString(json::kVersion, &version); 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return version == json::kVersion10; 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool CloudDeviceDescription::InitFromString(const std::string& json) { 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_ptr<base::Value> parsed(base::JSONReader::Read(json)); 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::DictionaryValue* description = NULL; 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (!parsed || !parsed->GetAsDictionary(&description)) 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return false; 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ignore_result(parsed.release()); 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return InitFromDictionary(make_scoped_ptr(description)); 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string CloudDeviceDescription::ToString() const { 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string json; 490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch base::JSONWriter::WriteWithOptions( 500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch root_.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return json; 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const base::DictionaryValue* CloudDeviceDescription::GetItem( 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& path) const { 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::DictionaryValue* value = NULL; 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) root_->GetDictionary(path, &value); 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return value; 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::DictionaryValue* CloudDeviceDescription::CreateItem( 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& path) { 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::DictionaryValue* value = new base::DictionaryValue; 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) root_->Set(path, value); 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return value; 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const base::ListValue* CloudDeviceDescription::GetListItem( 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& path) const { 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::ListValue* value = NULL; 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) root_->GetList(path, &value); 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return value; 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::ListValue* CloudDeviceDescription::CreateListItem( 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& path) { 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::ListValue* value = new base::ListValue; 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) root_->Set(path, value); 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return value; 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace cloud_devices 83