Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / privet_confirm_api_flow.cc
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 "chrome/browser/local_discovery/privet_confirm_api_flow.h"
6
7 #include "base/strings/stringprintf.h"
8 #include "base/values.h"
9 #include "chrome/browser/local_discovery/gcd_base_api_flow.h"
10 #include "chrome/browser/local_discovery/gcd_constants.h"
11 #include "chrome/browser/local_discovery/privet_constants.h"
12 #include "chrome/common/cloud_print/cloud_print_constants.h"
13 #include "components/cloud_devices/common/cloud_devices_urls.h"
14 #include "net/base/url_util.h"
15
16 namespace local_discovery {
17
18 namespace {
19
20 const char kGCDAutomatedClaimUploadData[] = "{ \"userEmail\": \"me\" }";
21 const char kGCDKindRegistrationTicket[] = "clouddevices#registrationTicket";
22
23 GURL GetConfirmFlowUrl(bool is_cloud_print, const std::string& token) {
24   if (is_cloud_print) {
25     return net::AppendQueryParameter(
26         cloud_devices::GetCloudPrintRelativeURL("confirm"), "token", token);
27   }
28   return cloud_devices::GetCloudDevicesRelativeURL("registrationTickets/" +
29                                                    token);
30 }
31
32 }  // namespace
33
34 PrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow(
35     net::URLRequestContextGetter* request_context,
36     OAuth2TokenService* token_service,
37     const std::string& account_id,
38     bool is_cloud_print,
39     const std::string& token,
40     const ResponseCallback& callback)
41     : is_cloud_print_(is_cloud_print),
42       flow_(request_context,
43             token_service,
44             account_id,
45             GetConfirmFlowUrl(is_cloud_print, token),
46             this),
47       callback_(callback) {
48 }
49
50 PrivetConfirmApiCallFlow::~PrivetConfirmApiCallFlow() {
51 }
52
53 void PrivetConfirmApiCallFlow::Start() {
54   flow_.Start();
55 }
56
57 void PrivetConfirmApiCallFlow::OnGCDAPIFlowError(
58     GCDBaseApiFlow* flow,
59     GCDBaseApiFlow::Status status) {
60   callback_.Run(status);
61 }
62
63 void PrivetConfirmApiCallFlow::OnGCDAPIFlowComplete(
64     GCDBaseApiFlow* flow,
65     const base::DictionaryValue* value) {
66   bool success = false;
67
68   if (is_cloud_print_) {
69     if (!value->GetBoolean(cloud_print::kSuccessValue, &success)) {
70       callback_.Run(GCDBaseApiFlow::ERROR_MALFORMED_RESPONSE);
71       return;
72     }
73   } else {
74     std::string kind;
75     value->GetString(kGCDKeyKind, &kind);
76     success = (kind == kGCDKindRegistrationTicket);
77   }
78
79   if (success) {
80     callback_.Run(GCDBaseApiFlow::SUCCESS);
81   } else {
82     callback_.Run(GCDBaseApiFlow::ERROR_FROM_SERVER);
83   }
84 }
85
86 bool PrivetConfirmApiCallFlow::GCDIsCloudPrint() { return is_cloud_print_; }
87
88 net::URLFetcher::RequestType PrivetConfirmApiCallFlow::GetRequestType() {
89   return (is_cloud_print_) ? net::URLFetcher::GET : net::URLFetcher::PATCH;
90 }
91
92 void PrivetConfirmApiCallFlow::GetUploadData(std::string* upload_type,
93                                              std::string* upload_data) {
94   if (is_cloud_print_) {
95     *upload_type = "";
96     *upload_data = "";
97   } else {
98     *upload_type = cloud_print::kContentTypeJSON;
99     *upload_data = kGCDAutomatedClaimUploadData;
100   }
101 }
102
103 }  // namespace local_discovery