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.
5 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h"
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"
16 namespace local_discovery
{
20 const char kGCDAutomatedClaimUploadData
[] = "{ \"userEmail\": \"me\" }";
21 const char kGCDKindRegistrationTicket
[] = "clouddevices#registrationTicket";
23 GURL
GetConfirmFlowUrl(bool is_cloud_print
, const std::string
& token
) {
25 return net::AppendQueryParameter(
26 cloud_devices::GetCloudPrintRelativeURL("confirm"), "token", token
);
28 return cloud_devices::GetCloudDevicesRelativeURL("registrationTickets/" +
34 PrivetConfirmApiCallFlow::PrivetConfirmApiCallFlow(
35 net::URLRequestContextGetter
* request_context
,
36 OAuth2TokenService
* token_service
,
37 const std::string
& account_id
,
39 const std::string
& token
,
40 const ResponseCallback
& callback
)
41 : is_cloud_print_(is_cloud_print
),
42 flow_(request_context
,
45 GetConfirmFlowUrl(is_cloud_print
, token
),
50 PrivetConfirmApiCallFlow::~PrivetConfirmApiCallFlow() {
53 void PrivetConfirmApiCallFlow::Start() {
57 void PrivetConfirmApiCallFlow::OnGCDAPIFlowError(
59 GCDBaseApiFlow::Status status
) {
60 callback_
.Run(status
);
63 void PrivetConfirmApiCallFlow::OnGCDAPIFlowComplete(
65 const base::DictionaryValue
* value
) {
68 if (is_cloud_print_
) {
69 if (!value
->GetBoolean(cloud_print::kSuccessValue
, &success
)) {
70 callback_
.Run(GCDBaseApiFlow::ERROR_MALFORMED_RESPONSE
);
75 value
->GetString(kGCDKeyKind
, &kind
);
76 success
= (kind
== kGCDKindRegistrationTicket
);
80 callback_
.Run(GCDBaseApiFlow::SUCCESS
);
82 callback_
.Run(GCDBaseApiFlow::ERROR_FROM_SERVER
);
86 bool PrivetConfirmApiCallFlow::GCDIsCloudPrint() { return is_cloud_print_
; }
88 net::URLFetcher::RequestType
PrivetConfirmApiCallFlow::GetRequestType() {
89 return (is_cloud_print_
) ? net::URLFetcher::GET
: net::URLFetcher::PATCH
;
92 void PrivetConfirmApiCallFlow::GetUploadData(std::string
* upload_type
,
93 std::string
* upload_data
) {
94 if (is_cloud_print_
) {
98 *upload_type
= cloud_print::kContentTypeJSON
;
99 *upload_data
= kGCDAutomatedClaimUploadData
;
103 } // namespace local_discovery