| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "services/authenticating_url_loader/authenticating_url_loader_factory_i
mpl.h" | 5 #include "services/authenticating_url_loader/authenticating_url_loader_intercept
or_factory.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" | 10 #include "mojo/public/cpp/application/application_impl.h" |
| 11 #include "mojo/services/authenticating_url_loader/public/interfaces/authenticati
ng_url_loader.mojom.h" | 11 #include "services/authenticating_url_loader/authenticating_url_loader_intercept
or.h" |
| 12 #include "services/authenticating_url_loader/authenticating_url_loader_impl.h" | |
| 13 | 12 |
| 14 namespace mojo { | 13 namespace mojo { |
| 15 | 14 |
| 16 AuthenticatingURLLoaderFactoryImpl::AuthenticatingURLLoaderFactoryImpl( | 15 AuthenticatingURLLoaderInterceptorFactory:: |
| 17 mojo::InterfaceRequest<AuthenticatingURLLoaderFactory> request, | 16 AuthenticatingURLLoaderInterceptorFactory( |
| 18 mojo::ApplicationImpl* app, | 17 mojo::InterfaceRequest<URLLoaderInterceptorFactory> request, |
| 19 std::map<GURL, std::string>* cached_tokens) | 18 authentication::AuthenticationServicePtr authentication_service, |
| 20 : binding_(this, request.Pass()), app_(app), cached_tokens_(cached_tokens) { | 19 mojo::ApplicationImpl* app, |
| 20 std::map<GURL, std::string>* cached_tokens) |
| 21 : binding_(this, request.Pass()), |
| 22 authentication_service_(authentication_service.Pass()), |
| 23 app_(app), |
| 24 cached_tokens_(cached_tokens) { |
| 21 app_->ConnectToService("mojo:network_service", &network_service_); | 25 app_->ConnectToService("mojo:network_service", &network_service_); |
| 26 authentication_service_.set_error_handler(this); |
| 22 } | 27 } |
| 23 | 28 |
| 24 AuthenticatingURLLoaderFactoryImpl::~AuthenticatingURLLoaderFactoryImpl() { | 29 AuthenticatingURLLoaderInterceptorFactory:: |
| 30 ~AuthenticatingURLLoaderInterceptorFactory() { |
| 25 } | 31 } |
| 26 | 32 |
| 27 std::string AuthenticatingURLLoaderFactoryImpl::GetCachedToken( | 33 std::string AuthenticatingURLLoaderInterceptorFactory::GetCachedToken( |
| 28 const GURL& url) { | 34 const GURL& url) { |
| 29 GURL origin = url.GetOrigin(); | 35 GURL origin = url.GetOrigin(); |
| 30 if (cached_tokens_->find(origin) != cached_tokens_->end()) { | 36 if (cached_tokens_->find(origin) != cached_tokens_->end()) { |
| 31 return (*cached_tokens_)[origin]; | 37 return (*cached_tokens_)[origin]; |
| 32 } | 38 } |
| 33 return ""; | 39 return ""; |
| 34 } | 40 } |
| 35 | 41 |
| 36 void AuthenticatingURLLoaderFactoryImpl::RetrieveToken( | 42 void AuthenticatingURLLoaderInterceptorFactory::RetrieveToken( |
| 37 const GURL& url, | 43 const GURL& url, |
| 38 const base::Callback<void(std::string)>& callback) { | 44 const base::Callback<void(std::string)>& callback) { |
| 39 if (!authentication_service_) { | 45 if (!authentication_service_) { |
| 40 callback.Run(""); | 46 callback.Run(""); |
| 41 return; | 47 return; |
| 42 } | 48 } |
| 43 GURL origin = url.GetOrigin(); | 49 GURL origin = url.GetOrigin(); |
| 44 if (pendings_retrieve_token_.find(origin) == pendings_retrieve_token_.end()) { | 50 if (pendings_retrieve_token_.find(origin) == pendings_retrieve_token_.end()) { |
| 45 if (cached_tokens_->find(origin) != cached_tokens_->end()) { | 51 if (cached_tokens_->find(origin) != cached_tokens_->end()) { |
| 46 // Clear the cached token in case the request is due to that token being | 52 // Clear the cached token in case the request is due to that token being |
| 47 // stale. | 53 // stale. |
| 48 authentication_service_->ClearOAuth2Token((*cached_tokens_)[origin]); | 54 authentication_service_->ClearOAuth2Token((*cached_tokens_)[origin]); |
| 49 cached_tokens_->erase(origin); | 55 cached_tokens_->erase(origin); |
| 50 } | 56 } |
| 51 if (cached_accounts_.find(origin) != cached_accounts_.end()) { | 57 if (cached_accounts_.find(origin) != cached_accounts_.end()) { |
| 52 OnAccountSelected(origin, cached_accounts_[origin], mojo::String()); | 58 OnAccountSelected(origin, cached_accounts_[origin], mojo::String()); |
| 53 return; | 59 return; |
| 54 } | 60 } |
| 55 authentication_service_->SelectAccount( | 61 authentication_service_->SelectAccount( |
| 56 true, base::Bind(&AuthenticatingURLLoaderFactoryImpl::OnAccountSelected, | 62 true, base::Bind( |
| 57 base::Unretained(this), origin)); | 63 &AuthenticatingURLLoaderInterceptorFactory::OnAccountSelected, |
| 64 base::Unretained(this), origin)); |
| 58 } | 65 } |
| 59 pendings_retrieve_token_[origin].push_back(callback); | 66 pendings_retrieve_token_[origin].push_back(callback); |
| 60 } | 67 } |
| 61 | 68 |
| 62 void AuthenticatingURLLoaderFactoryImpl::OnURLLoaderError( | 69 void AuthenticatingURLLoaderInterceptorFactory::OnInterceptorError( |
| 63 AuthenticatingURLLoaderImpl* url_loader) { | 70 AuthenticatingURLLoaderInterceptor* interceptor) { |
| 64 auto it = std::find_if( | 71 auto it = std::find_if( |
| 65 url_loaders_.begin(), url_loaders_.end(), | 72 interceptors_.begin(), interceptors_.end(), |
| 66 [url_loader](const std::unique_ptr<AuthenticatingURLLoaderImpl>& p) { | 73 [interceptor]( |
| 67 return p.get() == url_loader; | 74 const std::unique_ptr<AuthenticatingURLLoaderInterceptor>& p) { |
| 75 return p.get() == interceptor; |
| 68 }); | 76 }); |
| 69 DCHECK(it != url_loaders_.end()); | 77 DCHECK(it != interceptors_.end()); |
| 70 url_loaders_.erase(it); | 78 interceptors_.erase(it); |
| 71 } | 79 } |
| 72 | 80 |
| 73 void AuthenticatingURLLoaderFactoryImpl::CreateAuthenticatingURLLoader( | 81 void AuthenticatingURLLoaderInterceptorFactory::OnConnectionError() { |
| 74 mojo::InterfaceRequest<AuthenticatingURLLoader> loader_request) { | 82 authentication_service_ = nullptr; |
| 75 url_loaders_.push_back(std::unique_ptr<AuthenticatingURLLoaderImpl>( | |
| 76 new AuthenticatingURLLoaderImpl(loader_request.Pass(), this))); | |
| 77 } | |
| 78 | 83 |
| 79 void AuthenticatingURLLoaderFactoryImpl::SetAuthenticationService( | 84 // All pending requests need to fail. |
| 80 authentication::AuthenticationServicePtr authentication_service) { | |
| 81 // If the authentication service changes, all pending request needs to fail. | |
| 82 for (const auto& callbacks : pendings_retrieve_token_) { | 85 for (const auto& callbacks : pendings_retrieve_token_) { |
| 83 for (const auto& callback : callbacks.second) { | 86 for (const auto& callback : callbacks.second) { |
| 84 callback.Run(""); | 87 callback.Run(""); |
| 85 } | 88 } |
| 86 } | 89 } |
| 87 pendings_retrieve_token_.clear(); | 90 pendings_retrieve_token_.clear(); |
| 88 authentication_service_ = authentication_service.Pass(); | |
| 89 if (authentication_service_) | |
| 90 authentication_service_.set_error_handler(this); | |
| 91 } | 91 } |
| 92 | 92 |
| 93 void AuthenticatingURLLoaderFactoryImpl::OnConnectionError() { | 93 void AuthenticatingURLLoaderInterceptorFactory::Create( |
| 94 SetAuthenticationService(nullptr); | 94 mojo::InterfaceRequest<URLLoaderInterceptor> interceptor) { |
| 95 interceptors_.push_back(std::unique_ptr<AuthenticatingURLLoaderInterceptor>( |
| 96 new AuthenticatingURLLoaderInterceptor(interceptor.Pass(), this))); |
| 95 } | 97 } |
| 96 | 98 |
| 97 void AuthenticatingURLLoaderFactoryImpl::OnAccountSelected(const GURL& origin, | 99 void AuthenticatingURLLoaderInterceptorFactory::OnAccountSelected( |
| 98 mojo::String account, | 100 const GURL& origin, |
| 99 mojo::String error) { | 101 mojo::String account, |
| 102 mojo::String error) { |
| 100 DCHECK(authentication_service_); | 103 DCHECK(authentication_service_); |
| 101 if (error) { | 104 if (error) { |
| 102 LOG(WARNING) << "Error (" << error << ") while selecting account"; | 105 LOG(WARNING) << "Error (" << error << ") while selecting account"; |
| 103 ExecuteCallbacks(origin, ""); | 106 ExecuteCallbacks(origin, ""); |
| 104 return; | 107 return; |
| 105 } | 108 } |
| 106 cached_accounts_[origin] = account; | 109 cached_accounts_[origin] = account; |
| 107 mojo::Array<mojo::String> scopes(1); | 110 mojo::Array<mojo::String> scopes(1); |
| 108 scopes[0] = "https://www.googleapis.com/auth/userinfo.email"; | 111 scopes[0] = "https://www.googleapis.com/auth/userinfo.email"; |
| 109 authentication_service_->GetOAuth2Token( | 112 authentication_service_->GetOAuth2Token( |
| 110 account, scopes.Pass(), | 113 account, scopes.Pass(), |
| 111 base::Bind(&AuthenticatingURLLoaderFactoryImpl::OnOAuth2TokenReceived, | 114 base::Bind( |
| 112 base::Unretained(this), origin)); | 115 &AuthenticatingURLLoaderInterceptorFactory::OnOAuth2TokenReceived, |
| 116 base::Unretained(this), origin)); |
| 113 } | 117 } |
| 114 | 118 |
| 115 void AuthenticatingURLLoaderFactoryImpl::OnOAuth2TokenReceived( | 119 void AuthenticatingURLLoaderInterceptorFactory::OnOAuth2TokenReceived( |
| 116 const GURL& origin, | 120 const GURL& origin, |
| 117 mojo::String token, | 121 mojo::String token, |
| 118 mojo::String error) { | 122 mojo::String error) { |
| 119 if (error) { | 123 if (error) { |
| 120 LOG(WARNING) << "Error (" << error << ") while getting token"; | 124 LOG(WARNING) << "Error (" << error << ") while getting token"; |
| 121 ExecuteCallbacks(origin, ""); | 125 ExecuteCallbacks(origin, ""); |
| 122 return; | 126 return; |
| 123 } | 127 } |
| 124 std::string string_token(token); | 128 std::string string_token(token); |
| 125 (*cached_tokens_)[origin] = string_token; | 129 (*cached_tokens_)[origin] = string_token; |
| 126 ExecuteCallbacks(origin, string_token); | 130 ExecuteCallbacks(origin, string_token); |
| 127 } | 131 } |
| 128 | 132 |
| 129 void AuthenticatingURLLoaderFactoryImpl::ExecuteCallbacks( | 133 void AuthenticatingURLLoaderInterceptorFactory::ExecuteCallbacks( |
| 130 const GURL& origin, | 134 const GURL& origin, |
| 131 const std::string& result) { | 135 const std::string& result) { |
| 132 for (auto& callback : pendings_retrieve_token_[origin]) { | 136 for (auto& callback : pendings_retrieve_token_[origin]) { |
| 133 callback.Run(result); | 137 callback.Run(result); |
| 134 } | 138 } |
| 135 pendings_retrieve_token_.erase(origin); | 139 pendings_retrieve_token_.erase(origin); |
| 136 } | 140 } |
| 137 | 141 |
| 138 } // namespace mojo | 142 } // namespace mojo |
| OLD | NEW |