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

Side by Side Diff: remoting/test/app_remoting_test_driver_environment.cc

Issue 880273006: Adding the AccessTokenFetcher and Environment class to the app remoting test (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR feedback Created 5 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "remoting/test/app_remoting_test_driver_environment.h"
6
7 #include "base/bind.h"
8 #include "base/callback_forward.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12
13 namespace remoting {
14 namespace test {
15
16 AppRemotingTestDriverEnvironment* AppRemotingSharedData;
17
18 AppRemotingTestDriverEnvironment::AppRemotingTestDriverEnvironment(
19 const std::string& user_name,
20 const std::string& service_environment)
21 : user_name_(user_name),
22 service_environment_(service_environment) {
23 DCHECK(!user_name_.empty());
24 DCHECK(!service_environment.empty());
25 }
26
27 AppRemotingTestDriverEnvironment::~AppRemotingTestDriverEnvironment() {
28 }
29
30 bool AppRemotingTestDriverEnvironment::Initialize(
31 const std::string& auth_code) {
32 if (!access_token_.empty()) {
33 return true;
34 }
35
36 // If a unit test has set |test_refresh_token_store_| then we should use it
37 // below. Note that we do not want to destroy the test object.
38 scoped_ptr<RefreshTokenStore> temporary_refresh_token_store;
39 RefreshTokenStore* refresh_token_store = test_refresh_token_store_;
40 if (!refresh_token_store) {
41 temporary_refresh_token_store = RefreshTokenStore::OnDisk(user_name_);
42 refresh_token_store = temporary_refresh_token_store.get();
43 }
44
45 // Check to see if we have a refresh token stored for this user.
46 refresh_token_ = refresh_token_store->FetchRefreshToken();
47 if (refresh_token_.empty()) {
48 // This isn't necessarily an error as this might be a first run scenario.
49 DVLOG(1) << "No refresh token stored for " << user_name_;
50
51 if (auth_code.empty()) {
52 // No token and no Auth code means no service connectivity, bail!
53 LOG(ERROR) << "Cannot retrieve an access token without a stored refresh"
54 << " token on disk or an auth_code passed into the tool";
55 return false;
56 }
57 }
58
59 if (!RetrieveAccessToken(auth_code)) {
60 // If we cannot retrieve an access token, then nothing is going to work and
61 // we should let the caller know that our object is not ready to be used.
62 return false;
63 }
64
65 return true;
66 }
67
68 bool AppRemotingTestDriverEnvironment::RefreshAccessToken() {
69 DCHECK(!refresh_token_.empty());
70
71 // Empty auth code is used when refreshing.
72 return RetrieveAccessToken(std::string());
73 }
74
75 void AppRemotingTestDriverEnvironment::SetAccessTokenFetcherForTest(
76 AccessTokenFetcher* access_token_fetcher) {
77 DCHECK(access_token_fetcher);
78
79 test_access_token_fetcher_ = access_token_fetcher;
80 }
81
82 void AppRemotingTestDriverEnvironment::SetRefreshTokenStoreForTest(
83 RefreshTokenStore* refresh_token_store) {
84 DCHECK(refresh_token_store);
85
86 test_refresh_token_store_ = refresh_token_store;
87 }
88
89 bool AppRemotingTestDriverEnvironment::RetrieveAccessToken(
90 const std::string& auth_code) {
91 scoped_ptr<base::MessageLoopForIO> message_loop;
92
93 if (!base::MessageLoop::current()) {
94 // Create a temporary message loop if the current thread does not already
95 // have one so we can use its task runner for our network request.
96 message_loop.reset(new base::MessageLoopForIO);
97 }
98
99 base::RunLoop run_loop;
100
101 access_token_.clear();
102
103 AccessTokenCallback access_token_callback =
104 base::Bind(&AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved,
105 base::Unretained(this),
106 run_loop.QuitClosure());
107
108 // If a unit test has set |test_access_token_fetcher_| then we should use it
109 // below. Note that we do not want to destroy the test object at the end of
110 // the function which is why we have the dance below.
111 scoped_ptr<AccessTokenFetcher> temporary_access_token_fetcher;
112 AccessTokenFetcher* access_token_fetcher = test_access_token_fetcher_;
113 if (!access_token_fetcher) {
114 temporary_access_token_fetcher.reset(new AccessTokenFetcher());
115 access_token_fetcher = temporary_access_token_fetcher.get();
116 }
117
118 if (!auth_code.empty()) {
119 // If the user passed in an authcode, then use it to retrieve an
120 // updated access/refresh token.
121 access_token_fetcher->GetAccessTokenFromAuthCode(
122 auth_code,
123 access_token_callback);
124 } else {
125 DCHECK(!refresh_token_.empty());
126
127 access_token_fetcher->GetAccessTokenFromRefreshToken(
128 refresh_token_,
129 access_token_callback);
130 }
131
132 run_loop.Run();
133
134 // If we were using an auth_code and received a valid refresh token,
135 // then we want to store it locally. If we had an auth code and did not
136 // receive a refresh token, then we should let the user know and exit.
137 if (!auth_code.empty()) {
138 if (!refresh_token_.empty()) {
139 // If a unit test has set |test_refresh_token_store_| then we should use
140 // it below. Note that we do not want to destroy the test object.
141 scoped_ptr<RefreshTokenStore> temporary_refresh_token_store;
142 RefreshTokenStore* refresh_token_store = test_refresh_token_store_;
143 if (!refresh_token_store) {
144 temporary_refresh_token_store = RefreshTokenStore::OnDisk(user_name_);
145 refresh_token_store = temporary_refresh_token_store.get();
146 }
147
148 if (!refresh_token_store->StoreRefreshToken(refresh_token_)) {
149 // If we failed to persist the refresh token, then we should let the
150 // user sort out the issue before continuing.
151 return false;
152 }
153 } else {
154 LOG(ERROR) << "Failed to use AUTH CODE to retrieve a refresh token.\n"
155 << "Was the one-time use AUTH CODE used more than once?";
156 return false;
157 }
158 }
159
160 if (access_token_.empty()) {
161 LOG(ERROR) << "Failed to retrieve access token.";
162 return false;
163 }
164
165 return true;
166 }
167
168 void AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved(
169 base::Closure done_closure,
170 const std::string& access_token,
171 const std::string& refresh_token) {
172 access_token_ = access_token;
173 refresh_token_ = refresh_token;
174
175 done_closure.Run();
176 }
177
178 } // namespace test
179 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698