Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: services/authenticating_url_loader/authenticating_url_loader_factory_impl.cc

Issue 1155283003: Change AuthenticatingURLLoader to be a URLLoaderInterceptor (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Address review Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_factory_i mpl.h"
6 6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "mojo/public/cpp/application/application_impl.h" 7 #include "mojo/public/cpp/application/application_impl.h"
11 #include "mojo/services/authenticating_url_loader/public/interfaces/authenticati ng_url_loader.mojom.h" 8 #include "services/authenticating_url_loader/authenticating_url_loader_intercept or_factory.h"
12 #include "services/authenticating_url_loader/authenticating_url_loader_impl.h"
13 9
14 namespace mojo { 10 namespace mojo {
15 11
16 AuthenticatingURLLoaderFactoryImpl::AuthenticatingURLLoaderFactoryImpl( 12 AuthenticatingURLLoaderFactoryImpl::AuthenticatingURLLoaderFactoryImpl(
17 mojo::InterfaceRequest<AuthenticatingURLLoaderFactory> request, 13 mojo::InterfaceRequest<AuthenticatingURLLoaderFactory> request,
18 mojo::ApplicationImpl* app, 14 mojo::ApplicationImpl* app,
19 std::map<GURL, std::string>* cached_tokens) 15 std::map<GURL, std::string>* cached_tokens)
20 : binding_(this, request.Pass()), app_(app), cached_tokens_(cached_tokens) { 16 : binding_(this, request.Pass()), app_(app), cached_tokens_(cached_tokens) {
21 app_->ConnectToService("mojo:network_service", &network_service_);
22 } 17 }
23 18
24 AuthenticatingURLLoaderFactoryImpl::~AuthenticatingURLLoaderFactoryImpl() { 19 AuthenticatingURLLoaderFactoryImpl::~AuthenticatingURLLoaderFactoryImpl() {
25 } 20 }
26 21
27 std::string AuthenticatingURLLoaderFactoryImpl::GetCachedToken( 22 void AuthenticatingURLLoaderFactoryImpl::CreateURLLoaderInterceptorFactory(
28 const GURL& url) { 23 mojo::InterfaceRequest<URLLoaderInterceptorFactory> factory_request,
29 GURL origin = url.GetOrigin();
30 if (cached_tokens_->find(origin) != cached_tokens_->end()) {
31 return (*cached_tokens_)[origin];
32 }
33 return "";
34 }
35
36 void AuthenticatingURLLoaderFactoryImpl::RetrieveToken(
37 const GURL& url,
38 const base::Callback<void(std::string)>& callback) {
39 if (!authentication_service_) {
40 callback.Run("");
41 return;
42 }
43 GURL origin = url.GetOrigin();
44 if (pendings_retrieve_token_.find(origin) == pendings_retrieve_token_.end()) {
45 if (cached_tokens_->find(origin) != cached_tokens_->end()) {
46 // Clear the cached token in case the request is due to that token being
47 // stale.
48 authentication_service_->ClearOAuth2Token((*cached_tokens_)[origin]);
49 cached_tokens_->erase(origin);
50 }
51 if (cached_accounts_.find(origin) != cached_accounts_.end()) {
52 OnAccountSelected(origin, cached_accounts_[origin], mojo::String());
53 return;
54 }
55 authentication_service_->SelectAccount(
56 true, base::Bind(&AuthenticatingURLLoaderFactoryImpl::OnAccountSelected,
57 base::Unretained(this), origin));
58 }
59 pendings_retrieve_token_[origin].push_back(callback);
60 }
61
62 void AuthenticatingURLLoaderFactoryImpl::OnURLLoaderError(
63 AuthenticatingURLLoaderImpl* url_loader) {
64 auto it = std::find_if(
65 url_loaders_.begin(), url_loaders_.end(),
66 [url_loader](const std::unique_ptr<AuthenticatingURLLoaderImpl>& p) {
67 return p.get() == url_loader;
68 });
69 DCHECK(it != url_loaders_.end());
70 url_loaders_.erase(it);
71 }
72
73 void AuthenticatingURLLoaderFactoryImpl::CreateAuthenticatingURLLoader(
74 mojo::InterfaceRequest<AuthenticatingURLLoader> loader_request) {
75 url_loaders_.push_back(std::unique_ptr<AuthenticatingURLLoaderImpl>(
76 new AuthenticatingURLLoaderImpl(loader_request.Pass(), this)));
77 }
78
79 void AuthenticatingURLLoaderFactoryImpl::SetAuthenticationService(
80 authentication::AuthenticationServicePtr authentication_service) { 24 authentication::AuthenticationServicePtr authentication_service) {
81 // If the authentication service changes, all pending request needs to fail. 25 new AuthenticatingURLLoaderInterceptorFactory(factory_request.Pass(),
82 for (const auto& callbacks : pendings_retrieve_token_) { 26 authentication_service.Pass(),
83 for (const auto& callback : callbacks.second) { 27 app_, cached_tokens_);
84 callback.Run("");
85 }
86 }
87 pendings_retrieve_token_.clear();
88 authentication_service_ = authentication_service.Pass();
89 if (authentication_service_)
90 authentication_service_.set_error_handler(this);
91 }
92
93 void AuthenticatingURLLoaderFactoryImpl::OnConnectionError() {
94 SetAuthenticationService(nullptr);
95 }
96
97 void AuthenticatingURLLoaderFactoryImpl::OnAccountSelected(const GURL& origin,
98 mojo::String account,
99 mojo::String error) {
100 DCHECK(authentication_service_);
101 if (error) {
102 LOG(WARNING) << "Error (" << error << ") while selecting account";
103 ExecuteCallbacks(origin, "");
104 return;
105 }
106 cached_accounts_[origin] = account;
107 mojo::Array<mojo::String> scopes(1);
108 scopes[0] = "https://www.googleapis.com/auth/userinfo.email";
109 authentication_service_->GetOAuth2Token(
110 account, scopes.Pass(),
111 base::Bind(&AuthenticatingURLLoaderFactoryImpl::OnOAuth2TokenReceived,
112 base::Unretained(this), origin));
113 }
114
115 void AuthenticatingURLLoaderFactoryImpl::OnOAuth2TokenReceived(
116 const GURL& origin,
117 mojo::String token,
118 mojo::String error) {
119 if (error) {
120 LOG(WARNING) << "Error (" << error << ") while getting token";
121 ExecuteCallbacks(origin, "");
122 return;
123 }
124 std::string string_token(token);
125 (*cached_tokens_)[origin] = string_token;
126 ExecuteCallbacks(origin, string_token);
127 }
128
129 void AuthenticatingURLLoaderFactoryImpl::ExecuteCallbacks(
130 const GURL& origin,
131 const std::string& result) {
132 for (auto& callback : pendings_retrieve_token_[origin]) {
133 callback.Run(result);
134 }
135 pendings_retrieve_token_.erase(origin);
136 } 28 }
137 29
138 } // namespace mojo 30 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698