summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/printer_state.cc
blob: 0e12eee0da2e397fe6e1d71419737bdfcee102f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cloud_print/gcp20/prototype/printer_state.h"

#include "base/file_util.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/values.h"

namespace {

const char kRegistered[] = "registered";
const char kUser[] = "user";
const char kDeviceId[] = "device_id";
const char kRefreshToken[] = "refresh_token";
const char kXmppJid[] = "xmpp_jid";
const char kAccessToken[] = "access_token";
const char kAccessTokenUpdate[] = "access_token_update";

const char kLocalSettings[] = "local_settings";
const char kCdd[] = "cdd";
const char kLocalSettingsLocalDiscovery[] = "local_discovery";
const char kLocalSettingsAccessTokenEnabled[] = "access_token_enabled";
const char kLocalSettingsLocalPrintingEnabled[] =
    "printer/local_printing_enabled";
const char kLocalSettingsXmppTimeoutValue[] = "xmpp_timeout_value";

}  // namespace

PrinterState::PrinterState()
    : registration_state(UNREGISTERED),
      confirmation_state(CONFIRMATION_PENDING) {
}

PrinterState::~PrinterState() {
}

namespace printer_state {

bool SaveToFile(const base::FilePath& path, const PrinterState& state) {
  base::DictionaryValue json;
  if (state.registration_state == PrinterState::REGISTERED) {
    json.SetBoolean(kRegistered, true);
    json.SetString(kUser, state.user);
    json.SetString(kDeviceId, state.device_id);
    json.SetString(kRefreshToken, state.refresh_token);
    json.SetString(kXmppJid, state.xmpp_jid);
    json.SetString(kAccessToken, state.access_token);
    json.SetInteger(kAccessTokenUpdate,
                    static_cast<int>(state.access_token_update.ToTimeT()));

    scoped_ptr<base::DictionaryValue> local_settings(new base::DictionaryValue);
    local_settings->SetBoolean(kLocalSettingsLocalDiscovery,
                               state.local_settings.local_discovery);
    local_settings->SetBoolean(kLocalSettingsAccessTokenEnabled,
                               state.local_settings.access_token_enabled);
    local_settings->SetBoolean(kLocalSettingsLocalPrintingEnabled,
                               state.local_settings.local_printing_enabled);
    local_settings->SetInteger(kLocalSettingsXmppTimeoutValue,
                               state.local_settings.xmpp_timeout_value);
    json.Set(kLocalSettings, local_settings.release());
  } else {
    json.SetBoolean(kRegistered, false);
  }

  if (state.cdd.get())
    json.Set(kCdd, state.cdd->DeepCopy());

  std::string json_str;
  base::JSONWriter::WriteWithOptions(&json,
                                     base::JSONWriter::OPTIONS_PRETTY_PRINT,
                                     &json_str);
  int size = base::checked_cast<int>(json_str.size());
  return (file_util::WriteFile(path, json_str.data(), size) == size);
}

bool LoadFromFile(const base::FilePath& path, PrinterState* state) {
  std::string json_str;
  if (!base::ReadFileToString(path, &json_str)) {
    LOG(ERROR) << "Cannot open file.";
    return false;
  }

  scoped_ptr<base::Value> json_val(base::JSONReader::Read(json_str));
  base::DictionaryValue* json = NULL;
  if (!json_val || !json_val->GetAsDictionary(&json)) {
    LOG(ERROR) << "Cannot read JSON dictionary from file.";
    return false;
  }

  bool registered = false;
  if (!json->GetBoolean(kRegistered, &registered)) {
    LOG(ERROR) << "Cannot parse |registered| state.";
    return false;
  }

  if (!registered)
    return true;

  std::string user;
  if (!json->GetString(kUser, &user)) {
    LOG(ERROR) << "Cannot parse |user|.";
    return false;
  }

  std::string device_id;
  if (!json->GetString(kDeviceId, &device_id)) {
    LOG(ERROR) << "Cannot parse |device_id|.";
    return false;
  }

  std::string refresh_token;
  if (!json->GetString(kRefreshToken, &refresh_token)) {
    LOG(ERROR) << "Cannot parse |refresh_token|.";
    return false;
  }

  std::string xmpp_jid;
  if (!json->GetString(kXmppJid, &xmpp_jid)) {
    LOG(ERROR) << "Cannot parse |xmpp_jid|.";
    return false;
  }

  std::string access_token;
  if (!json->GetString(kAccessToken, &access_token)) {
    LOG(ERROR) << "Cannot parse |access_token|.";
    return false;
  }

  int access_token_update;
  if (!json->GetInteger(kAccessTokenUpdate, &access_token_update)) {
    LOG(ERROR) << "Cannot parse |access_token_update|.";
    return false;
  }

  LocalSettings local_settings;
  base::DictionaryValue* settings_dict;
  if (!json->GetDictionary(kLocalSettings, &settings_dict)) {
    LOG(WARNING) << "Cannot read |local_settings|. Reset to default.";
  } else {
    if (!settings_dict->GetBoolean(kLocalSettingsLocalDiscovery,
                                   &local_settings.local_discovery) ||
        !settings_dict->GetBoolean(kLocalSettingsAccessTokenEnabled,
                                   &local_settings.access_token_enabled) ||
        !settings_dict->GetBoolean(kLocalSettingsLocalPrintingEnabled,
                                   &local_settings.local_printing_enabled) ||
        !settings_dict->GetInteger(kLocalSettingsXmppTimeoutValue,
                                   &local_settings.xmpp_timeout_value)) {
      LOG(WARNING) << "Cannot parse |local_settings|. Reset to default.";
      local_settings = LocalSettings();
    }
  }

  base::DictionaryValue* cdd_dict = NULL;
  if (!json->GetDictionary(kCdd, &cdd_dict))
    LOG(WARNING) << "Cannot read |cdd|. Reset to default.";

  *state = PrinterState();
  state->registration_state = PrinterState::REGISTERED;
  state->user = user;
  state->device_id = device_id;
  state->refresh_token = refresh_token;
  state->xmpp_jid = xmpp_jid;
  state->access_token = access_token;
  state->access_token_update = base::Time::FromTimeT(access_token_update);
  state->local_settings = local_settings;
  if (cdd_dict)
    state->cdd.reset(cdd_dict->DeepCopy());
  return true;
}

}  // namespace printer_state