1 // Copyright 2015 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 "remoting/test/remote_host_info_fetcher.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/values.h"
15 #include "net/http/http_response_headers.h"
16 #include "net/http/http_status_code.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "remoting/base/url_request_context_getter.h"
21 const char kRequestTestOrigin
[] =
22 "Origin: chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk";
28 RemoteHostInfoFetcher::RemoteHostInfoFetcher() {
31 RemoteHostInfoFetcher::~RemoteHostInfoFetcher() {
34 bool RemoteHostInfoFetcher::RetrieveRemoteHostInfo(
35 const std::string
& application_id
,
36 const std::string
& access_token
,
37 ServiceEnvironment service_environment
,
38 const RemoteHostInfoCallback
& callback
) {
39 DCHECK(!application_id
.empty());
40 DCHECK(!access_token
.empty());
41 DCHECK(!callback
.is_null());
42 DCHECK(remote_host_info_callback_
.is_null());
44 DVLOG(2) << "RemoteHostInfoFetcher::RetrieveRemoteHostInfo() called";
46 std::string service_url
;
47 switch (service_environment
) {
48 case kDeveloperEnvironment
:
49 DVLOG(1) << "Configuring service request for dev environment";
50 service_url
= base::StringPrintf(kDevServiceEnvironmentUrlFormat
,
51 application_id
.c_str());
54 case kTestingEnvironment
:
55 DVLOG(1) << "Configuring service request for test environment";
56 service_url
= base::StringPrintf(kTestServiceEnvironmentUrlFormat
,
57 application_id
.c_str());
61 LOG(ERROR
) << "Unrecognized service type: " << service_environment
;
65 remote_host_info_callback_
= callback
;
67 request_context_getter_
= new remoting::URLRequestContextGetter(
68 base::ThreadTaskRunnerHandle::Get(), // network_runner
69 base::ThreadTaskRunnerHandle::Get()); // file_runner
72 net::URLFetcher::Create(GURL(service_url
), net::URLFetcher::POST
, this));
73 request_
->SetRequestContext(request_context_getter_
.get());
74 request_
->AddExtraRequestHeader("Authorization: OAuth " + access_token
);
75 request_
->AddExtraRequestHeader(kRequestTestOrigin
);
76 request_
->SetUploadData("application/json; charset=UTF-8", "{}");
82 void RemoteHostInfoFetcher::OnURLFetchComplete(const net::URLFetcher
* source
) {
84 DVLOG(2) << "URL Fetch Completed for: " << source
->GetOriginalURL();
86 RemoteHostInfo remote_host_info
;
87 int response_code
= request_
->GetResponseCode();
88 if (response_code
!= net::HTTP_OK
) {
89 LOG(ERROR
) << "RemoteHostInfo request failed with error code: "
91 remote_host_info_callback_
.Run(remote_host_info
);
92 remote_host_info_callback_
.Reset();
96 std::string response_string
;
97 if (!request_
->GetResponseAsString(&response_string
)) {
98 LOG(ERROR
) << "Failed to retrieve RemoteHostInfo response data";
99 remote_host_info_callback_
.Run(remote_host_info
);
100 remote_host_info_callback_
.Reset();
104 scoped_ptr
<base::Value
> response_value(
105 base::JSONReader::Read(response_string
));
106 if (!response_value
||
107 !response_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
108 LOG(ERROR
) << "Failed to parse response string to JSON";
109 remote_host_info_callback_
.Run(remote_host_info
);
110 remote_host_info_callback_
.Reset();
114 std::string remote_host_status
;
115 const base::DictionaryValue
* response
;
116 if (response_value
->GetAsDictionary(&response
)) {
117 response
->GetString("status", &remote_host_status
);
119 LOG(ERROR
) << "Failed to convert parsed JSON to a dictionary object";
120 remote_host_info_callback_
.Run(remote_host_info
);
121 remote_host_info_callback_
.Reset();
125 remote_host_info
.SetRemoteHostStatusFromString(remote_host_status
);
127 if (remote_host_info
.IsReadyForConnection()) {
128 response
->GetString("host.applicationId", &remote_host_info
.application_id
);
129 response
->GetString("host.hostId", &remote_host_info
.host_id
);
130 response
->GetString("hostJid", &remote_host_info
.host_jid
);
131 response
->GetString("authorizationCode",
132 &remote_host_info
.authorization_code
);
133 response
->GetString("sharedSecret", &remote_host_info
.shared_secret
);
136 remote_host_info_callback_
.Run(remote_host_info
);
137 remote_host_info_callback_
.Reset();
141 } // namespace remoting