| Index: services/authenticating_url_loader/authenticating_url_loader_interceptor.cc
|
| diff --git a/services/authenticating_url_loader/authenticating_url_loader_impl.cc b/services/authenticating_url_loader/authenticating_url_loader_interceptor.cc
|
| similarity index 41%
|
| rename from services/authenticating_url_loader/authenticating_url_loader_impl.cc
|
| rename to services/authenticating_url_loader/authenticating_url_loader_interceptor.cc
|
| index 6de8ba605f73ac0c9bab252a33b4f41cc70e4537..7e0f574a0e25e0fd63a918a018103caa2f0d7263 100644
|
| --- a/services/authenticating_url_loader/authenticating_url_loader_impl.cc
|
| +++ b/services/authenticating_url_loader/authenticating_url_loader_interceptor.cc
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "services/authenticating_url_loader/authenticating_url_loader_impl.h"
|
| +#include "services/authenticating_url_loader/authenticating_url_loader_interceptor.h"
|
|
|
| #include "base/bind.h"
|
| #include "base/logging.h"
|
| @@ -10,34 +10,36 @@
|
|
|
| namespace mojo {
|
|
|
| -AuthenticatingURLLoaderImpl::AuthenticatingURLLoaderImpl(
|
| - InterfaceRequest<AuthenticatingURLLoader> request,
|
| - AuthenticatingURLLoaderFactoryImpl* factory)
|
| +AuthenticatingURLLoaderInterceptor::AuthenticatingURLLoaderInterceptor(
|
| + mojo::InterfaceRequest<URLLoaderInterceptor> request,
|
| + AuthenticatingURLLoaderInterceptorFactory* factory)
|
| : binding_(this, request.Pass()),
|
| factory_(factory),
|
| request_authorization_state_(REQUEST_INITIAL) {
|
| binding_.set_error_handler(this);
|
| }
|
|
|
| -AuthenticatingURLLoaderImpl::~AuthenticatingURLLoaderImpl() {
|
| +AuthenticatingURLLoaderInterceptor::~AuthenticatingURLLoaderInterceptor() {
|
| }
|
|
|
| -void AuthenticatingURLLoaderImpl::Start(
|
| - URLRequestPtr request,
|
| - const Callback<void(URLResponsePtr)>& callback) {
|
| +void AuthenticatingURLLoaderInterceptor::InterceptRequest(
|
| + mojo::URLRequestPtr request,
|
| + const InterceptRequestCallback& callback) {
|
| // TODO(blundell): If we need to handle requests with bodies, we'll need to
|
| // do something here.
|
| if (request->body) {
|
| - LOG(ERROR)
|
| - << "Cannot pass a request to AuthenticatingURLLoader that has a body";
|
| + LOG(ERROR) << "Cannot pass a request to AuthenticatingURLLoaderInterceptor"
|
| + "that has a body";
|
| callback.Run(nullptr);
|
| return;
|
| }
|
| + pending_interception_callback_ = callback;
|
| url_ = GURL(request->url);
|
| auto_follow_redirects_ = request->auto_follow_redirects;
|
| + request->auto_follow_redirects = false;
|
| bypass_cache_ = request->bypass_cache;
|
| headers_ = request->headers.Clone();
|
| - pending_request_callback_ = callback;
|
| +
|
| std::string token = factory_->GetCachedToken(url_);
|
| if (token != "") {
|
| auto auth_header = HttpHeader::New();
|
| @@ -45,20 +47,21 @@ void AuthenticatingURLLoaderImpl::Start(
|
| auth_header->value = "Bearer " + token;
|
| request->headers.push_back(auth_header.Pass());
|
| }
|
| - StartNetworkRequest(request.Pass());
|
| +
|
| + StartRequest(request.Pass());
|
| }
|
|
|
| -void AuthenticatingURLLoaderImpl::FollowRedirect(
|
| - const Callback<void(URLResponsePtr)>& callback) {
|
| +void AuthenticatingURLLoaderInterceptor::InterceptFollowRedirect(
|
| + const InterceptResponseCallback& callback) {
|
| mojo::String error;
|
|
|
| - if (!url_.is_valid() || !url_loader_) {
|
| + if (!url_.is_valid()) {
|
| error = "No redirect to follow";
|
| }
|
|
|
| if (auto_follow_redirects_) {
|
| error =
|
| - "FollowRedirect() should not be "
|
| + "InterceptFollowRedirect() should not be "
|
| "called when auto_follow_redirects has been set";
|
| }
|
|
|
| @@ -67,52 +70,53 @@ void AuthenticatingURLLoaderImpl::FollowRedirect(
|
| }
|
|
|
| if (!error.is_null()) {
|
| - LOG(ERROR) << "AuthenticatingURLLoader: " << error;
|
| + LOG(ERROR) << "AuthenticatingURLLoaderInterceptor: " << error;
|
| callback.Run(nullptr);
|
| return;
|
| }
|
|
|
| - pending_request_callback_ = callback;
|
| - FollowRedirectInternal();
|
| -}
|
| + // If there is no cached token, have the URLLoader follow the redirect
|
| + // itself.
|
| + std::string token = factory_->GetCachedToken(url_);
|
| + if (token == "") {
|
| + callback.Run(nullptr);
|
| + return;
|
| + }
|
|
|
| -void AuthenticatingURLLoaderImpl::StartNetworkRequest(URLRequestPtr request) {
|
| - factory_->network_service()->CreateURLLoader(mojo::GetProxy(&url_loader_));
|
| - url_loader_->Start(request.Pass(),
|
| - base::Bind(&AuthenticatingURLLoaderImpl::OnLoadComplete,
|
| - base::Unretained(this)));
|
| + pending_interception_callback_ = callback;
|
| + StartRequest(BuildRequest(token));
|
| }
|
|
|
| -void AuthenticatingURLLoaderImpl::OnConnectionError() {
|
| - factory_->OnURLLoaderError(this);
|
| - // The factory deleted this object.
|
| -}
|
| +void AuthenticatingURLLoaderInterceptor::InterceptResponse(
|
| + mojo::URLResponsePtr response,
|
| + const InterceptResponseCallback& callback) {
|
| + pending_interception_callback_ = callback;
|
| + pending_response_ = response.Pass();
|
|
|
| -void AuthenticatingURLLoaderImpl::OnLoadComplete(URLResponsePtr response) {
|
| - if (response->redirect_url) {
|
| - url_ = GURL(response->redirect_url);
|
| - request_authorization_state_ = REQUEST_INITIAL;
|
| -
|
| - if (auto_follow_redirects_) {
|
| - FollowRedirectInternal();
|
| - } else {
|
| - // NOTE: We do not reset |url_loader_| here as it will be needed if the
|
| - // client calls |FollowRedirect()|.
|
| - pending_request_callback_.Run(response.Pass());
|
| + if (pending_response_->status_code != 401 ||
|
| + request_authorization_state_ == REQUEST_USED_FRESH_AUTH_SERVICE_TOKEN) {
|
| + if (pending_response_->redirect_url) {
|
| + url_ = GURL(pending_response_->redirect_url);
|
| + request_authorization_state_ = REQUEST_INITIAL;
|
| + if (auto_follow_redirects_) {
|
| + // If there is no cached token, have the URLLoader follow the
|
| + // redirect itself.
|
| + std::string token = factory_->GetCachedToken(url_);
|
| + if (token == "")
|
| + pending_interception_callback_.Run(nullptr);
|
| + else
|
| + StartRequest(BuildRequest(token));
|
| + return;
|
| + }
|
| }
|
| - return;
|
| - }
|
|
|
| - url_loader_.reset();
|
| -
|
| - if (response->status_code != 401 ||
|
| - request_authorization_state_ == REQUEST_USED_FRESH_AUTH_SERVICE_TOKEN) {
|
| - pending_request_callback_.Run(response.Pass());
|
| + URLLoaderInterceptorResponsePtr interceptor_response =
|
| + URLLoaderInterceptorResponse::New();
|
| + interceptor_response->response = pending_response_.Pass();
|
| + pending_interception_callback_.Run(interceptor_response.Pass());
|
| return;
|
| }
|
|
|
| - pending_response_ = response.Pass();
|
| -
|
| DCHECK(request_authorization_state_ == REQUEST_INITIAL ||
|
| request_authorization_state_ ==
|
| REQUEST_USED_CURRENT_AUTH_SERVICE_TOKEN);
|
| @@ -122,34 +126,31 @@ void AuthenticatingURLLoaderImpl::OnLoadComplete(URLResponsePtr response) {
|
| request_authorization_state_ = REQUEST_USED_FRESH_AUTH_SERVICE_TOKEN;
|
| }
|
| factory_->RetrieveToken(
|
| - url_, base::Bind(&AuthenticatingURLLoaderImpl::OnOAuth2TokenReceived,
|
| - base::Unretained(this)));
|
| - return;
|
| + url_,
|
| + base::Bind(&AuthenticatingURLLoaderInterceptor::OnOAuth2TokenReceived,
|
| + base::Unretained(this)));
|
| }
|
|
|
| -void AuthenticatingURLLoaderImpl::FollowRedirectInternal() {
|
| - DCHECK(url_.is_valid());
|
| - DCHECK(url_loader_);
|
| - DCHECK(request_authorization_state_ == REQUEST_INITIAL);
|
| -
|
| - url_loader_->FollowRedirect(base::Bind(
|
| - &AuthenticatingURLLoaderImpl::OnLoadComplete, base::Unretained(this)));
|
| +void AuthenticatingURLLoaderInterceptor::OnConnectionError() {
|
| + factory_->OnInterceptorError(this);
|
| + // The factory deleted this object.
|
| }
|
|
|
| -void AuthenticatingURLLoaderImpl::OnOAuth2TokenReceived(std::string token) {
|
| - if (token.empty()) {
|
| - LOG(ERROR) << "Error while getting token";
|
| - pending_request_callback_.Run(pending_response_.Pass());
|
| - return;
|
| - }
|
| -
|
| - auto auth_header = HttpHeader::New();
|
| - auth_header->name = "Authorization";
|
| - auth_header->value = "Bearer " + token;
|
| +URLRequestPtr AuthenticatingURLLoaderInterceptor::BuildRequest(
|
| + std::string token) {
|
| Array<HttpHeaderPtr> headers;
|
| if (headers_)
|
| headers = headers_.Clone();
|
| - headers.push_back(auth_header.Pass());
|
| +
|
| + if (token == "")
|
| + token = factory_->GetCachedToken(url_);
|
| +
|
| + if (token != "") {
|
| + auto auth_header = HttpHeader::New();
|
| + auth_header->name = "Authorization";
|
| + auth_header->value = "Bearer " + token;
|
| + headers.push_back(auth_header.Pass());
|
| + }
|
|
|
| URLRequestPtr request(mojo::URLRequest::New());
|
| request->url = url_.spec();
|
| @@ -157,7 +158,30 @@ void AuthenticatingURLLoaderImpl::OnOAuth2TokenReceived(std::string token) {
|
| request->bypass_cache = bypass_cache_;
|
| request->headers = headers.Pass();
|
|
|
| - StartNetworkRequest(request.Pass());
|
| + return request.Pass();
|
| +}
|
| +
|
| +void AuthenticatingURLLoaderInterceptor::StartRequest(
|
| + mojo::URLRequestPtr request) {
|
| + URLLoaderInterceptorResponsePtr interceptor_response =
|
| + URLLoaderInterceptorResponse::New();
|
| + interceptor_response->request = request.Pass();
|
| + pending_interception_callback_.Run(interceptor_response.Pass());
|
| +}
|
| +
|
| +void AuthenticatingURLLoaderInterceptor::OnOAuth2TokenReceived(
|
| + std::string token) {
|
| + URLLoaderInterceptorResponsePtr interceptor_response =
|
| + URLLoaderInterceptorResponse::New();
|
| +
|
| + if (token.empty()) {
|
| + LOG(ERROR) << "Error while getting token";
|
| + interceptor_response->response = pending_response_.Pass();
|
| + } else {
|
| + interceptor_response->request = BuildRequest(token);
|
| + }
|
| +
|
| + pending_interception_callback_.Run(interceptor_response.Pass());
|
| }
|
|
|
| } // namespace mojo
|
|
|