diff options
author | sergeyu <sergeyu@chromium.org> | 2016-02-11 12:58:09 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-11 21:00:09 +0000 |
commit | 9b3d516c34b2ffe9ea4b4c40102dd14eb8f04a3f (patch) | |
tree | 8e8c3ee2bc588049e78741717349602b1924cba0 /remoting/base | |
parent | 53c91f2ffde1c62ad55ad4acb6101b60a647640e (diff) | |
download | chromium_src-9b3d516c34b2ffe9ea4b4c40102dd14eb8f04a3f.zip chromium_src-9b3d516c34b2ffe9ea4b4c40102dd14eb8f04a3f.tar.gz chromium_src-9b3d516c34b2ffe9ea4b4c40102dd14eb8f04a3f.tar.bz2 |
Add remoting::UrlRequest interface with 2 implementations
PepperPortAllocator and ChromiumPortAllocator are largely the same but
use different APIs to sent HTTP requests to allocate relay session.
Specifically they use pp::URLLoader and net::URLFetcher respectively.
This CL adds an abstract interface for URL request with two
implementations:
- PepperUrlRequest - to be used in the plugin,
- ChromiumUrlRequest - to be used everywhere else.
BUG=577954
Review URL: https://codereview.chromium.org/1679023009
Cr-Commit-Position: refs/heads/master@{#374975}
Diffstat (limited to 'remoting/base')
-rw-r--r-- | remoting/base/BUILD.gn | 5 | ||||
-rw-r--r-- | remoting/base/chromium_url_request.cc | 60 | ||||
-rw-r--r-- | remoting/base/chromium_url_request.h | 55 | ||||
-rw-r--r-- | remoting/base/url_request.h | 58 |
4 files changed, 177 insertions, 1 deletions
diff --git a/remoting/base/BUILD.gn b/remoting/base/BUILD.gn index dc9b658d..6d83dbc 100644 --- a/remoting/base/BUILD.gn +++ b/remoting/base/BUILD.gn @@ -24,7 +24,10 @@ source_set("base") { "//third_party/webrtc/modules/desktop_capture:primitives", ] if (is_nacl) { - sources -= [ "url_request_context_getter.cc" ] + sources -= [ + "chromium_url_request.cc", + "url_request_context_getter.cc", + ] } } diff --git a/remoting/base/chromium_url_request.cc b/remoting/base/chromium_url_request.cc new file mode 100644 index 0000000..36dd7be --- /dev/null +++ b/remoting/base/chromium_url_request.cc @@ -0,0 +1,60 @@ +// Copyright 2016 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/base/chromium_url_request.h" + +#include "base/callback_helpers.h" +#include "net/url_request/url_fetcher.h" +#include "net/url_request/url_request_context_getter.h" + +namespace remoting { + +ChromiumUrlRequest::ChromiumUrlRequest( + scoped_refptr<net::URLRequestContextGetter> url_context, + const std::string& url) { + url_fetcher_ = net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this); + url_fetcher_->SetRequestContext(url_context.get()); +} + +ChromiumUrlRequest::~ChromiumUrlRequest() {} + +void ChromiumUrlRequest::AddHeader(const std::string& value) { + url_fetcher_->AddExtraRequestHeader(value); +} + +void ChromiumUrlRequest::Start(const OnResultCallback& on_result_callback) { + DCHECK(!on_result_callback.is_null()); + DCHECK(on_result_callback_.is_null()); + + on_result_callback_ = on_result_callback; + url_fetcher_->Start(); +} + +void ChromiumUrlRequest::OnURLFetchComplete( + const net::URLFetcher* url_fetcher) { + DCHECK_EQ(url_fetcher, url_fetcher_.get()); + + Result result; + result.success = + url_fetcher_->GetResponseCode() != net::URLFetcher::RESPONSE_CODE_INVALID; + if (result.success) { + result.status = url_fetcher_->GetResponseCode(); + url_fetcher_->GetResponseAsString(&result.response_body); + } + + DCHECK(!on_result_callback_.is_null()); + base::ResetAndReturn(&on_result_callback_).Run(result); +} + +ChromiumUrlRequestFactory::ChromiumUrlRequestFactory( + scoped_refptr<net::URLRequestContextGetter> url_context) + : url_context_(url_context) {} +ChromiumUrlRequestFactory::~ChromiumUrlRequestFactory() {} + +scoped_ptr<UrlRequest> ChromiumUrlRequestFactory::CreateUrlRequest( + const std::string& url) { + return make_scoped_ptr(new ChromiumUrlRequest(url_context_, url)); +} + +} // namespace remoting diff --git a/remoting/base/chromium_url_request.h b/remoting/base/chromium_url_request.h new file mode 100644 index 0000000..fe842c7 --- /dev/null +++ b/remoting/base/chromium_url_request.h @@ -0,0 +1,55 @@ +// Copyright 2016 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. + +#ifndef REMOTING_BASE_CHROMIUM_URL_REQUEST_H_ +#define REMOTING_BASE_CHROMIUM_URL_REQUEST_H_ + +#include <string> + +#include "base/callback.h" +#include "base/memory/ref_counted.h" +#include "net/url_request/url_fetcher_delegate.h" +#include "remoting/base/url_request.h" + +namespace net { +class URLRequestContextGetter; +} // namespace net + +namespace remoting { + +// UrlRequest implementation based on net::URLFetcher. +class ChromiumUrlRequest : public UrlRequest, public net::URLFetcherDelegate { + public: + ChromiumUrlRequest(scoped_refptr<net::URLRequestContextGetter> url_context, + const std::string& url); + ~ChromiumUrlRequest() override; + + // UrlRequest interface. + void AddHeader(const std::string& value) override; + void Start(const OnResultCallback& on_result_callback) override; + + private: + // net::URLFetcherDelegate interface. + void OnURLFetchComplete(const net::URLFetcher* url_fetcher) override; + + scoped_ptr<net::URLFetcher> url_fetcher_; + OnResultCallback on_result_callback_; +}; + +class ChromiumUrlRequestFactory : public UrlRequestFactory { + public: + ChromiumUrlRequestFactory( + scoped_refptr<net::URLRequestContextGetter> url_context); + ~ChromiumUrlRequestFactory() override; + + // UrlRequestFactory interface. + scoped_ptr<UrlRequest> CreateUrlRequest(const std::string& url) override; + + private: + scoped_refptr<net::URLRequestContextGetter> url_context_; +}; + +} // namespace remoting + +#endif // REMOTING_BASE_CHROMIUM_URL_REQUEST_H_ diff --git a/remoting/base/url_request.h b/remoting/base/url_request.h new file mode 100644 index 0000000..ab1d674 --- /dev/null +++ b/remoting/base/url_request.h @@ -0,0 +1,58 @@ +// Copyright 2016 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. + +#ifndef REMOTING_BASE_URL_REQUEST_H_ +#define REMOTING_BASE_URL_REQUEST_H_ + +#include <string> + +#include "base/callback_forward.h" +#include "base/memory/scoped_ptr.h" + +namespace remoting { + +// Abstract interface for URL requests. +class UrlRequest { + public: + struct Result { + Result() = default; + Result(int status, std::string response_body) + : success(true), status(status), response_body(response_body) {} + + static Result Failed() { return Result(); } + + // Set to true when the URL has been fetched successfully. + bool success = false; + + // HTTP status code received from the server. Valid only when |success| is + // set to true. + int status = 0; + + // Body of the response received from the server. Valid only when |success| + // is set to true. + std::string response_body; + }; + + typedef base::Callback<void(const Result& result)> OnResultCallback; + + virtual ~UrlRequest() {} + + // Adds an HTTP header to the request. Has no effect if called after Start(). + virtual void AddHeader(const std::string& value) = 0; + + // Sends a request to the server. |on_response_callback| will be called to + // return result of the request. + virtual void Start(const OnResultCallback& on_result_callback) = 0; +}; + +// Factory for UrlRequest instances. +class UrlRequestFactory { + public: + virtual ~UrlRequestFactory() {} + virtual scoped_ptr<UrlRequest> CreateUrlRequest(const std::string& url) = 0; +}; + +} // namespace remoting + +#endif // REMOTING_BASE_URL_REQUEST_H_ |