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/callback_helpers.h"
9 #include "base/json/json_reader.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/values.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "remoting/base/url_request_context_getter.h"
20 const char kRequestTestOrigin
[] =
21 "Origin: chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk";
27 RemoteHostInfoFetcher::RemoteHostInfoFetcher() {
30 RemoteHostInfoFetcher::~RemoteHostInfoFetcher() {
33 bool RemoteHostInfoFetcher::RetrieveRemoteHostInfo(
34 const std::string
& application_id
,
35 const std::string
& access_token
,
36 ServiceEnvironment service_environment
,
37 const RemoteHostInfoCallback
& callback
) {
38 DCHECK(!application_id
.empty());
39 DCHECK(!access_token
.empty());
40 DCHECK(!callback
.is_null());
41 DCHECK(remote_host_info_callback_
.is_null());
43 VLOG(2) << "RemoteHostInfoFetcher::RetrieveRemoteHostInfo() called";
45 std::string
service_url(
46 GetRunApplicationUrl(application_id
, service_environment
));
47 if (service_url
.empty()) {
48 LOG(ERROR
) << "Unrecognized service type: " << service_environment
;
51 VLOG(1) << "Using remote host service request url: " << service_url
;
53 remote_host_info_callback_
= callback
;
55 request_context_getter_
= new remoting::URLRequestContextGetter(
56 base::ThreadTaskRunnerHandle::Get(), // network_runner
57 base::ThreadTaskRunnerHandle::Get()); // file_runner
60 net::URLFetcher::Create(GURL(service_url
), net::URLFetcher::POST
, this);
61 request_
->SetRequestContext(request_context_getter_
.get());
62 request_
->AddExtraRequestHeader("Authorization: OAuth " + access_token
);
63 request_
->AddExtraRequestHeader(kRequestTestOrigin
);
64 request_
->SetUploadData("application/json; charset=UTF-8", "{}");
70 void RemoteHostInfoFetcher::OnURLFetchComplete(const net::URLFetcher
* source
) {
72 VLOG(2) << "URL Fetch Completed for: " << source
->GetOriginalURL();
74 RemoteHostInfo remote_host_info
;
75 int response_code
= request_
->GetResponseCode();
76 if (response_code
!= net::HTTP_OK
) {
77 LOG(ERROR
) << "RemoteHostInfo request failed with error code: "
79 base::ResetAndReturn(&remote_host_info_callback_
).Run(remote_host_info
);
83 std::string response_string
;
84 if (!request_
->GetResponseAsString(&response_string
)) {
85 LOG(ERROR
) << "Failed to retrieve RemoteHostInfo response data";
86 base::ResetAndReturn(&remote_host_info_callback_
).Run(remote_host_info
);
90 scoped_ptr
<base::Value
> response_value(
91 base::JSONReader::Read(response_string
));
92 if (!response_value
||
93 !response_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
94 LOG(ERROR
) << "Failed to parse response string to JSON";
95 base::ResetAndReturn(&remote_host_info_callback_
).Run(remote_host_info
);
99 std::string remote_host_status
;
100 const base::DictionaryValue
* response
;
101 if (response_value
->GetAsDictionary(&response
)) {
102 response
->GetString("status", &remote_host_status
);
104 LOG(ERROR
) << "Failed to convert parsed JSON to a dictionary object";
105 base::ResetAndReturn(&remote_host_info_callback_
).Run(remote_host_info
);
109 remote_host_info
.SetRemoteHostStatusFromString(remote_host_status
);
111 if (remote_host_info
.IsReadyForConnection()) {
112 response
->GetString("host.applicationId", &remote_host_info
.application_id
);
113 response
->GetString("host.hostId", &remote_host_info
.host_id
);
114 response
->GetString("hostJid", &remote_host_info
.host_jid
);
115 response
->GetString("authorizationCode",
116 &remote_host_info
.authorization_code
);
117 response
->GetString("sharedSecret", &remote_host_info
.shared_secret
);
120 base::ResetAndReturn(&remote_host_info_callback_
).Run(remote_host_info
);
124 } // namespace remoting