diff options
author | joedow <joedow@chromium.org> | 2015-03-05 18:41:02 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-06 02:41:58 +0000 |
commit | dd5660bd5b757e906eee390b83c8a7585822a60d (patch) | |
tree | 32592da2e604f96a50e180e2e0cdecc60c56eb29 /remoting/test/remote_host_info_fetcher.cc | |
parent | dbf8bfd7fa34d69e880f0b8073a0441f439bf08a (diff) | |
download | chromium_src-dd5660bd5b757e906eee390b83c8a7585822a60d.zip chromium_src-dd5660bd5b757e906eee390b83c8a7585822a60d.tar.gz chromium_src-dd5660bd5b757e906eee390b83c8a7585822a60d.tar.bz2 |
Adding the RemoteHostInfo and RemoteHostInfoFetcher classes and unittests for use with the App Remoting Test Driver.
This changelist also includes some lint error cleanup and updating of
assertions in the unittest code to remove the c_str() calls.
BUG=462307
Review URL: https://codereview.chromium.org/962763002
Cr-Commit-Position: refs/heads/master@{#319396}
Diffstat (limited to 'remoting/test/remote_host_info_fetcher.cc')
-rw-r--r-- | remoting/test/remote_host_info_fetcher.cc | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/remoting/test/remote_host_info_fetcher.cc b/remoting/test/remote_host_info_fetcher.cc new file mode 100644 index 0000000..e7dc095 --- /dev/null +++ b/remoting/test/remote_host_info_fetcher.cc @@ -0,0 +1,140 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "remoting/test/remote_host_info_fetcher.h" + +#include "base/bind.h" +#include "base/json/json_reader.h" +#include "base/json/json_writer.h" +#include "base/logging.h" +#include "base/message_loop/message_loop.h" +#include "base/strings/stringprintf.h" +#include "base/thread_task_runner_handle.h" +#include "base/values.h" +#include "net/http/http_response_headers.h" +#include "net/http/http_status_code.h" +#include "net/url_request/url_fetcher.h" +#include "remoting/base/url_request_context_getter.h" + +namespace { +const char kRequestTestOrigin[] = + "Origin: chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk"; +} + +namespace remoting { +namespace test { + +RemoteHostInfoFetcher::RemoteHostInfoFetcher() {} + +RemoteHostInfoFetcher::~RemoteHostInfoFetcher() {} + +bool RemoteHostInfoFetcher::RetrieveRemoteHostInfo( + const std::string& application_id, + const std::string& access_token, + ServiceEnvironment service_environment, + const RemoteHostInfoCallback& callback) { + DCHECK(!application_id.empty()); + DCHECK(!access_token.empty()); + DCHECK(!callback.is_null()); + DCHECK(remote_host_info_callback_.is_null()); + + DVLOG(2) << "RemoteHostInfoFetcher::RetrieveRemoteHostInfo() called"; + + std::string service_url; + switch (service_environment) { + case kDeveloperEnvironment: + DVLOG(1) << "Configuring service request for dev environment"; + service_url = base::StringPrintf(kDevServiceEnvironmentUrlFormat, + application_id.c_str()); + break; + + case kTestingEnvironment: + DVLOG(1) << "Configuring service request for test environment"; + service_url = base::StringPrintf(kTestServiceEnvironmentUrlFormat, + application_id.c_str()); + break; + + default: + LOG(ERROR) << "Unrecognized service type: " << service_environment; + return false; + } + + remote_host_info_callback_ = callback; + + scoped_refptr<remoting::URLRequestContextGetter> request_context_getter; + request_context_getter = new remoting::URLRequestContextGetter( + base::ThreadTaskRunnerHandle::Get(), // network_runner + base::ThreadTaskRunnerHandle::Get()); // file_runner + + request_.reset( + net::URLFetcher::Create(GURL(service_url), net::URLFetcher::POST, this)); + request_->SetRequestContext(request_context_getter.get()); + request_->AddExtraRequestHeader("Authorization: OAuth " + access_token); + request_->AddExtraRequestHeader(kRequestTestOrigin); + request_->SetUploadData("application/json; charset=UTF-8", "{}"); + request_->Start(); + + return true; +} + +void RemoteHostInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) { + DCHECK(source); + DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); + + RemoteHostInfo remote_host_info; + int response_code = request_->GetResponseCode(); + if (response_code != net::HTTP_OK) { + LOG(ERROR) << "RemoteHostInfo request failed with error code: " + << response_code; + remote_host_info_callback_.Run(remote_host_info); + remote_host_info_callback_.Reset(); + return; + } + + std::string response_string; + if (!request_->GetResponseAsString(&response_string)) { + LOG(ERROR) << "Failed to retrieve RemoteHostInfo response data"; + remote_host_info_callback_.Run(remote_host_info); + remote_host_info_callback_.Reset(); + return; + } + + scoped_ptr<base::Value> response_value( + base::JSONReader::Read(response_string)); + if (!response_value || + !response_value->IsType(base::Value::TYPE_DICTIONARY)) { + LOG(ERROR) << "Failed to parse response string to JSON"; + remote_host_info_callback_.Run(remote_host_info); + remote_host_info_callback_.Reset(); + return; + } + + std::string remote_host_status; + const base::DictionaryValue* response; + if (response_value->GetAsDictionary(&response)) { + response->GetString("status", &remote_host_status); + } else { + LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; + remote_host_info_callback_.Run(remote_host_info); + remote_host_info_callback_.Reset(); + return; + } + + remote_host_info.SetRemoteHostStatusFromString(remote_host_status); + + if (remote_host_info.IsReadyForConnection()) { + response->GetString("host.applicationId", &remote_host_info.application_id); + response->GetString("host.hostId", &remote_host_info.host_id); + response->GetString("hostJid", &remote_host_info.host_jid); + response->GetString("authorizationCode", + &remote_host_info.authorization_code); + response->GetString("sharedSecret", &remote_host_info.shared_secret); + } + + remote_host_info_callback_.Run(remote_host_info); + remote_host_info_callback_.Reset(); +} + +} // namespace test +} // namespace remoting |