summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/printer.cc
blob: aa6bcd5c0f72e79a8577aae29349d6d2db48298e (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// 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.h"

#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/file_util.h"
#include "base/guid.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "cloud_print/gcp20/prototype/command_line_reader.h"
#include "cloud_print/gcp20/prototype/service_parameters.h"
#include "net/base/net_util.h"
#include "net/base/url_util.h"

const base::FilePath::CharType kPrinterStatePath[] =
    FILE_PATH_LITERAL("printer_state.json");

namespace {

const char* kServiceType = "_privet._tcp.local";
const char* kServiceNamePrefix = "first_gcp20_device";
const char* kServiceDomainName = "my-privet-device.local";

const char* kPrinterName = "Google GCP2.0 Prototype";
const char* kPrinterDescription = "Printer emulator";

const char* kCdd =
"{\n"
" 'version': '1.0',\n"
"  'printer': {\n"
"    'vendor_capability': [\n"
"      {\n"
"        'id': 'psk:MediaType',\n"
"        'display_name': 'Media Type',\n"
"        'type': 'SELECT',\n"
"        'select_cap': {\n"
"          'option': [\n"
"            {\n"
"              'value': 'psk:Plain',\n"
"              'display_name': 'Plain Paper',\n"
"              'is_default': true\n"
"            },\n"
"            {\n"
"              'value': 'ns0000:Glossy',\n"
"              'display_name': 'Glossy Photo',\n"
"              'is_default': false\n"
"            }\n"
"          ]\n"
"        }\n"
"      }\n"
"    ],\n"
"    'reverse_order': { 'default': false }\n"
"  }\n"
"}\n";

// Returns local IP address number of first interface found (except loopback).
// Return value is empty if no interface found. Possible interfaces names are
// "eth0", "wlan0" etc. If interface name is empty, function will return IP
// address of first interface found.
net::IPAddressNumber GetLocalIp(const std::string& interface_name,
                                bool return_ipv6_number) {
  net::NetworkInterfaceList interfaces;
  bool success = net::GetNetworkList(&interfaces);
  DCHECK(success);

  size_t expected_address_size = return_ipv6_number ? net::kIPv6AddressSize
                                                    : net::kIPv4AddressSize;

  for (net::NetworkInterfaceList::iterator iter = interfaces.begin();
       iter != interfaces.end(); ++iter) {
    if (iter->address.size() == expected_address_size &&
        (interface_name.empty() || interface_name == iter->name)) {
      LOG(INFO) << net::IPAddressToString(iter->address);
      return iter->address;
    }
  }

  return net::IPAddressNumber();
}

}  // namespace

Printer::RegistrationInfo::RegistrationInfo() : state(DEV_REG_UNREGISTERED) {
}

Printer::RegistrationInfo::~RegistrationInfo() {
}

Printer::Printer() : http_server_(this) {
}

Printer::~Printer() {
  Stop();
}

bool Printer::Start() {
  if (IsOnline())
    return true;

  // TODO(maksymb): Add switch for command line to control interface name.
  net::IPAddressNumber ip = GetLocalIp("", false);
  if (ip.empty()) {
    LOG(ERROR) << "No local IP found. Cannot start printer.";
    return false;
  }
  VLOG(1) << "Local address: " << net::IPAddressToString(ip);

  uint16 port = command_line_reader::ReadHttpPort();

  // Starting HTTP server.
  if (!http_server_.Start(port))
    return false;

  if (!LoadFromFile(base::FilePath(kPrinterStatePath)))
    reg_info_ = RegistrationInfo();

  // Starting DNS-SD server.
  if (!dns_server_.Start(
      ServiceParameters(kServiceType, kServiceNamePrefix, kServiceDomainName,
                        ip, port),
      command_line_reader::ReadTtl(),
      CreateTxt())) {
    http_server_.Shutdown();
    return false;
  }

  // Creating Cloud Requester.
  requester_.reset(
      new CloudPrintRequester(
          base::MessageLoop::current()->message_loop_proxy(),
          this));

  xtoken_ = XPrivetToken();
  starttime_ = base::Time::Now();

  return true;
}

bool Printer::IsOnline() const {
  return requester_;
}

void Printer::Stop() {
  dns_server_.Shutdown();
  http_server_.Shutdown();
  requester_.reset();
}

PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart(
    const std::string& user) {
  PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user);
  if (status != PrivetHttpServer::REG_ERROR_OK)
    return status;

  if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED)
    return PrivetHttpServer::REG_ERROR_INVALID_ACTION;

  reg_info_ = RegistrationInfo();
  reg_info_.user = user;
  reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED;

  requester_->StartRegistration(GenerateProxyId(), kPrinterName, user, kCdd);

  return PrivetHttpServer::REG_ERROR_OK;
}

bool Printer::CheckXPrivetTokenHeader(const std::string& token) const {
  return xtoken_.CheckValidXToken(token);
}

PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationGetClaimToken(
    const std::string& user,
    std::string* token,
    std::string* claim_url) {
  PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user);
  if (status != PrivetHttpServer::REG_ERROR_OK)
    return status;

  // TODO(maksymb): Add user confirmation.

  if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_STARTED)
    return PrivetHttpServer::REG_ERROR_DEVICE_BUSY;

  if (reg_info_.state ==
      RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) {
    *token = reg_info_.registration_token;
    *claim_url = reg_info_.complete_invite_url;
    return PrivetHttpServer::REG_ERROR_OK;
  }

  return PrivetHttpServer::REG_ERROR_INVALID_ACTION;
}

PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationComplete(
    const std::string& user,
    std::string* device_id) {
  PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user);
  if (status != PrivetHttpServer::REG_ERROR_OK)
    return status;

  if (reg_info_.state !=
      RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) {
    return PrivetHttpServer::REG_ERROR_INVALID_ACTION;
  }

  reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_COMPLETING;
  requester_->CompleteRegistration();

  *device_id = reg_info_.device_id;

  return PrivetHttpServer::REG_ERROR_OK;
}

PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationCancel(
    const std::string& user) {
  PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user);
  if (status != PrivetHttpServer::REG_ERROR_OK &&
      status != PrivetHttpServer::REG_ERROR_SERVER_ERROR) {
    return status;
  }

  if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED)
    return PrivetHttpServer::REG_ERROR_INVALID_ACTION;

  reg_info_ = RegistrationInfo();
  return PrivetHttpServer::REG_ERROR_OK;
}

void Printer::GetRegistrationServerError(std::string* description) {
  DCHECK_EQ(reg_info_.state, RegistrationInfo::DEV_REG_REGISTRATION_ERROR) <<
      "Method shouldn't be called when not needed.";

  *description = reg_info_.error_description;
}

void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) {
  // TODO(maksymb): Replace "text" with constants.

  *info = PrivetHttpServer::DeviceInfo();
  info->version = "1.0";
  info->name = kPrinterName;
  info->description = kPrinterDescription;
  info->url = kCloudPrintUrl;
  info->id = reg_info_.device_id;
  info->device_state = "idle";
  info->connection_state = "offline";
  info->manufacturer = "Google";
  info->model = "Prototype";
  info->serial_number = "2.3.5.7.13.17.19.31.61.89.107.127.521.607.1279.2203";
  info->firmware = "3.7.31.127.8191.131071.524287.2147483647";
  info->uptime = static_cast<int>((base::Time::Now() - starttime_).InSeconds());

  info->x_privet_token = xtoken_.GenerateXToken();

  if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED)
    info->api.push_back("/privet/register");

  info->type.push_back("printer");
}

void Printer::OnRegistrationStartResponseParsed(
    const std::string& registration_token,
    const std::string& complete_invite_url,
    const std::string& device_id) {
  reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY;
  reg_info_.device_id = device_id;
  reg_info_.registration_token = registration_token;
  reg_info_.complete_invite_url = complete_invite_url;
}

void Printer::OnGetAuthCodeResponseParsed(const std::string& refresh_token) {
  reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED;
  reg_info_.refresh_token = refresh_token;
  SaveToFile(base::FilePath(kPrinterStatePath));
}

void Printer::OnRegistrationError(const std::string& description) {
  LOG(ERROR) << "server_error: " << description;

  // TODO(maksymb): Implement waiting after error and timeout of registration.
  reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR;
  reg_info_.error_description = description;
}

PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors(
    const std::string& user) const {
  if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTERED)
    return PrivetHttpServer::REG_ERROR_REGISTERED;

  if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED &&
      user != reg_info_.user) {
    return PrivetHttpServer::REG_ERROR_DEVICE_BUSY;
  }

  if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR)
    return PrivetHttpServer::REG_ERROR_SERVER_ERROR;

  return PrivetHttpServer::REG_ERROR_OK;
}

std::string Printer::GenerateProxyId() const {
  return "{" + base::GenerateGUID() +"}";
}

std::vector<std::string> Printer::CreateTxt() const {
  std::vector<std::string> txt;
  txt.push_back("txtvers=1");
  txt.push_back("ty=" + std::string(kPrinterName));
  txt.push_back("note=" + std::string(kPrinterDescription));
  txt.push_back("url=" + std::string(kCloudPrintUrl));
  txt.push_back("type=printer");
  txt.push_back("id=" + reg_info_.device_id);
  txt.push_back("cs=offline");

  return txt;
}

void Printer::SaveToFile(const base::FilePath& file_path) const {
  base::DictionaryValue json;
  // TODO(maksymb): Get rid of in-place constants.
  if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTERED) {
    json.SetBoolean("registered", true);
    json.SetString("user", reg_info_.user);
    json.SetString("device_id", reg_info_.device_id);
    json.SetString("refresh_token", reg_info_.refresh_token);
  } else {
    json.SetBoolean("registered", false);
  }

  std::string json_str;
  base::JSONWriter::WriteWithOptions(&json,
                                     base::JSONWriter::OPTIONS_PRETTY_PRINT,
                                     &json_str);
  if (!file_util::WriteFile(file_path, json_str.data(),
                            static_cast<int>(json_str.size()))) {
    LOG(ERROR) << "Cannot write state.";
  }
  LOG(INFO) << "State written to file.";
}

bool Printer::LoadFromFile(const base::FilePath& file_path) {
  if (!base::PathExists(file_path)) {
    LOG(INFO) << "Registration info is not found. Printer is unregistered.";
    return false;
  }

  LOG(INFO) << "Loading registration info from file.";
  std::string json_str;
  if (!file_util::ReadFileToString(file_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("registered", &registered)) {
    LOG(ERROR) << "Cannot parse |registered| state.";
    return false;
  }

  if (!registered) {
    reg_info_ = RegistrationInfo();
    return true;
  }

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

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

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

  reg_info_ = RegistrationInfo();
  reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED;
  reg_info_.user = user;
  reg_info_.device_id = device_id;
  reg_info_.refresh_token = refresh_token;

  return true;
}