1 // Copyright 2013 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 "cloud_print/gcp20/prototype/printer_state.h" 6 7 #include "base/file_util.h" 8 #include "base/json/json_reader.h" 9 #include "base/json/json_writer.h" 10 #include "base/logging.h" 11 #include "base/numerics/safe_conversions.h" 12 #include "base/values.h" 13 14 namespace { 15 16 const char kRegistered[] = "registered"; 17 const char kUser[] = "user"; 18 const char kDeviceId[] = "device_id"; 19 const char kRefreshToken[] = "refresh_token"; 20 const char kXmppJid[] = "xmpp_jid"; 21 const char kAccessToken[] = "access_token"; 22 const char kAccessTokenUpdate[] = "access_token_update"; 23 24 const char kLocalSettings[] = "local_settings"; 25 const char kCdd[] = "cdd"; 26 const char kLocalSettingsLocalDiscovery[] = "local_discovery"; 27 const char kLocalSettingsAccessTokenEnabled[] = "access_token_enabled"; 28 const char kLocalSettingsLocalPrintingEnabled[] = 29 "printer/local_printing_enabled"; 30 const char kLocalSettingsXmppTimeoutValue[] = "xmpp_timeout_value"; 31 32 } // namespace 33 34 PrinterState::PrinterState() 35 : registration_state(UNREGISTERED), 36 confirmation_state(CONFIRMATION_PENDING) { 37 } 38 39 PrinterState::~PrinterState() { 40 } 41 42 namespace printer_state { 43 44 bool SaveToFile(const base::FilePath& path, const PrinterState& state) { 45 base::DictionaryValue json; 46 if (state.registration_state == PrinterState::REGISTERED) { 47 json.SetBoolean(kRegistered, true); 48 json.SetString(kUser, state.user); 49 json.SetString(kDeviceId, state.device_id); 50 json.SetString(kRefreshToken, state.refresh_token); 51 json.SetString(kXmppJid, state.xmpp_jid); 52 json.SetString(kAccessToken, state.access_token); 53 json.SetInteger(kAccessTokenUpdate, 54 static_cast<int>(state.access_token_update.ToTimeT())); 55 56 scoped_ptr<base::DictionaryValue> local_settings(new base::DictionaryValue); 57 local_settings->SetBoolean(kLocalSettingsLocalDiscovery, 58 state.local_settings.local_discovery); 59 local_settings->SetBoolean(kLocalSettingsAccessTokenEnabled, 60 state.local_settings.access_token_enabled); 61 local_settings->SetBoolean(kLocalSettingsLocalPrintingEnabled, 62 state.local_settings.local_printing_enabled); 63 local_settings->SetInteger(kLocalSettingsXmppTimeoutValue, 64 state.local_settings.xmpp_timeout_value); 65 json.Set(kLocalSettings, local_settings.release()); 66 } else { 67 json.SetBoolean(kRegistered, false); 68 } 69 70 if (state.cdd.get()) 71 json.Set(kCdd, state.cdd->DeepCopy()); 72 73 std::string json_str; 74 base::JSONWriter::WriteWithOptions(&json, 75 base::JSONWriter::OPTIONS_PRETTY_PRINT, 76 &json_str); 77 int size = base::checked_cast<int>(json_str.size()); 78 return (base::WriteFile(path, json_str.data(), size) == size); 79 } 80 81 bool LoadFromFile(const base::FilePath& path, PrinterState* state) { 82 std::string json_str; 83 if (!base::ReadFileToString(path, &json_str)) { 84 LOG(ERROR) << "Cannot open file."; 85 return false; 86 } 87 88 scoped_ptr<base::Value> json_val(base::JSONReader::Read(json_str)); 89 base::DictionaryValue* json = NULL; 90 if (!json_val || !json_val->GetAsDictionary(&json)) { 91 LOG(ERROR) << "Cannot read JSON dictionary from file."; 92 return false; 93 } 94 95 bool registered = false; 96 if (!json->GetBoolean(kRegistered, ®istered)) { 97 LOG(ERROR) << "Cannot parse |registered| state."; 98 return false; 99 } 100 101 if (!registered) 102 return true; 103 104 std::string user; 105 if (!json->GetString(kUser, &user)) { 106 LOG(ERROR) << "Cannot parse |user|."; 107 return false; 108 } 109 110 std::string device_id; 111 if (!json->GetString(kDeviceId, &device_id)) { 112 LOG(ERROR) << "Cannot parse |device_id|."; 113 return false; 114 } 115 116 std::string refresh_token; 117 if (!json->GetString(kRefreshToken, &refresh_token)) { 118 LOG(ERROR) << "Cannot parse |refresh_token|."; 119 return false; 120 } 121 122 std::string xmpp_jid; 123 if (!json->GetString(kXmppJid, &xmpp_jid)) { 124 LOG(ERROR) << "Cannot parse |xmpp_jid|."; 125 return false; 126 } 127 128 std::string access_token; 129 if (!json->GetString(kAccessToken, &access_token)) { 130 LOG(ERROR) << "Cannot parse |access_token|."; 131 return false; 132 } 133 134 int access_token_update; 135 if (!json->GetInteger(kAccessTokenUpdate, &access_token_update)) { 136 LOG(ERROR) << "Cannot parse |access_token_update|."; 137 return false; 138 } 139 140 LocalSettings local_settings; 141 base::DictionaryValue* settings_dict; 142 if (!json->GetDictionary(kLocalSettings, &settings_dict)) { 143 LOG(WARNING) << "Cannot read |local_settings|. Reset to default."; 144 } else { 145 if (!settings_dict->GetBoolean(kLocalSettingsLocalDiscovery, 146 &local_settings.local_discovery) || 147 !settings_dict->GetBoolean(kLocalSettingsAccessTokenEnabled, 148 &local_settings.access_token_enabled) || 149 !settings_dict->GetBoolean(kLocalSettingsLocalPrintingEnabled, 150 &local_settings.local_printing_enabled) || 151 !settings_dict->GetInteger(kLocalSettingsXmppTimeoutValue, 152 &local_settings.xmpp_timeout_value)) { 153 LOG(WARNING) << "Cannot parse |local_settings|. Reset to default."; 154 local_settings = LocalSettings(); 155 } 156 } 157 158 base::DictionaryValue* cdd_dict = NULL; 159 if (!json->GetDictionary(kCdd, &cdd_dict)) 160 LOG(WARNING) << "Cannot read |cdd|. Reset to default."; 161 162 *state = PrinterState(); 163 state->registration_state = PrinterState::REGISTERED; 164 state->user = user; 165 state->device_id = device_id; 166 state->refresh_token = refresh_token; 167 state->xmpp_jid = xmpp_jid; 168 state->access_token = access_token; 169 state->access_token_update = base::Time::FromTimeT(access_token_update); 170 state->local_settings = local_settings; 171 if (cdd_dict) 172 state->cdd.reset(cdd_dict->DeepCopy()); 173 return true; 174 } 175 176 } // namespace printer_state 177 178