1a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
2a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch// found in the LICENSE file.
4a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
5a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "chrome/browser/local_discovery/privet_confirm_api_flow.h"
60529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/strings/stringprintf.h"
80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/values.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/local_discovery/gcd_api_flow.h"
100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/browser/local_discovery/gcd_constants.h"
110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/browser/local_discovery/privet_constants.h"
12a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "chrome/common/cloud_print/cloud_print_constants.h"
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/cloud_devices/common/cloud_devices_urls.h"
140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "net/base/url_util.h"
15a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
16a3f7b4e666c476898878fa745f637129375cd889Ben Murdochnamespace local_discovery {
17a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnamespace {
190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)GURL GetConfirmFlowUrl(const std::string& token) {
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return net::AppendQueryParameter(
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      cloud_devices::GetCloudPrintRelativeURL("confirm"), "token", token);
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}  // namespace
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
27a3f7b4e666c476898878fa745f637129375cd889Ben MurdochPrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow(
280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const std::string& token,
29a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    const ResponseCallback& callback)
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : callback_(callback), token_(token) {
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
33a3f7b4e666c476898878fa745f637129375cd889Ben MurdochPrivetConfirmApiCallFlow::~PrivetConfirmApiCallFlow() {
34a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch}
35a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void PrivetConfirmApiCallFlow::OnGCDAPIFlowError(GCDApiFlow::Status status) {
3758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  callback_.Run(status);
38a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch}
39a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid PrivetConfirmApiCallFlow::OnGCDAPIFlowComplete(
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::DictionaryValue& value) {
42a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  bool success = false;
43a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!value.GetBoolean(cloud_print::kSuccessValue, &success)) {
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    callback_.Run(GCDApiFlow::ERROR_MALFORMED_RESPONSE);
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
47a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  }
48a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
49a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  if (success) {
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    callback_.Run(GCDApiFlow::SUCCESS);
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  } else {
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    callback_.Run(GCDApiFlow::ERROR_FROM_SERVER);
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnet::URLFetcher::RequestType PrivetConfirmApiCallFlow::GetRequestType() {
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return net::URLFetcher::GET;
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)GURL PrivetConfirmApiCallFlow::GetURL() {
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return GetConfirmFlowUrl(token_);
62a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch}
63a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
64a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch}  // namespace local_discovery
65