OLD | NEW |
---|---|
(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 (!refresh_token_store_) { | |
37 refresh_token_store_ = GetRefreshTokenStoreForDisk(); | |
38 } | |
39 | |
40 // Check to see if we have a refresh token stored for this user. | |
41 refresh_token_ = refresh_token_store_->ReadRefreshTokenFromDisk(user_name_); | |
42 if (refresh_token_.empty()) { | |
43 // This isn't necessarily an error as this might be a first run scenario. | |
44 DVLOG(1) << "No refresh token stored for " << user_name_; | |
45 | |
46 if (auth_code.empty()) { | |
47 // No token and no Auth code means no service connectivity, bail! | |
48 LOG(ERROR) << "Cannot retrieve an access token without a stored refresh" | |
49 << " token on disk or an auth_code passed into the tool"; | |
50 return false; | |
51 } | |
52 } | |
53 | |
54 if (!RetrieveAccessToken(auth_code)) { | |
55 // If we cannot retrieve an access token, then nothing is going to work and | |
56 // we should let the caller know that our object is not ready to be used. | |
57 return false; | |
58 } | |
59 | |
60 return true; | |
61 } | |
62 | |
63 bool AppRemotingTestDriverEnvironment::RefreshAccessToken() { | |
64 DCHECK(!refresh_token_.empty()); | |
65 | |
66 // Empty auth code is used when refreshing. | |
67 return RetrieveAccessToken(std::string()); | |
68 } | |
69 | |
70 void AppRemotingTestDriverEnvironment::SetAccessTokenFetcherForTest( | |
71 scoped_ptr<remoting::test::AccessTokenFetcher> mock_fetcher) { | |
72 mock_access_token_fetcher_ = mock_fetcher.Pass(); | |
73 } | |
74 | |
75 void AppRemotingTestDriverEnvironment::SetRefreshTokenStoreForTest( | |
76 scoped_ptr<remoting::test::RefreshTokenStoreInterface> mock_token_store) { | |
77 refresh_token_store_ = mock_token_store.Pass(); | |
78 } | |
79 | |
80 void AppRemotingTestDriverEnvironment::SetUp() { | |
Wez
2015/02/19 22:00:23
Do you need these at all, now that they are empty?
joedow
2015/02/20 02:58:36
Done.
| |
81 } | |
82 | |
83 void AppRemotingTestDriverEnvironment::TearDown() { | |
84 } | |
85 | |
86 bool AppRemotingTestDriverEnvironment::RetrieveAccessToken( | |
87 const std::string& auth_code) { | |
88 scoped_ptr<base::MessageLoopForIO> message_loop; | |
89 | |
90 if (!base::MessageLoop::current()) { | |
91 // Create a temporary message loop if the current thread does not already | |
92 // have one so we can use its task runner for our network request. | |
93 message_loop.reset(new base::MessageLoopForIO); | |
94 } | |
95 | |
96 base::RunLoop network_request_run_loop; | |
Wez
2015/02/19 22:00:23
nit: Just run_loop?
joedow
2015/02/20 02:58:36
Done.
| |
97 | |
98 access_token_.clear(); | |
99 | |
100 AccessTokenCallback access_token_callback = | |
101 base::Bind(&AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved, | |
102 base::Unretained(this), | |
103 network_request_run_loop.QuitClosure()); | |
104 | |
105 scoped_ptr<remoting::test::AccessTokenFetcher> access_token_fetcher; | |
106 if (!mock_access_token_fetcher_) { | |
107 access_token_fetcher.reset(new remoting::test::AccessTokenFetcher()); | |
108 } else { | |
109 access_token_fetcher.reset(mock_access_token_fetcher_.get()); | |
Wez
2015/02/19 22:00:23
This is a risky construct; instead of taking owner
joedow
2015/02/20 02:58:36
Done.
| |
110 } | |
111 | |
112 if (!auth_code.empty()) { | |
113 // If the user passed in an authcode, then use it to retrieve an | |
114 // updated access/refresh token. | |
115 access_token_fetcher->GetAccessTokenFromAuthCode( | |
116 auth_code, | |
117 access_token_callback); | |
118 } else { | |
119 DCHECK(!refresh_token_.empty()); | |
120 | |
121 access_token_fetcher->GetAccessTokenFromRefreshToken( | |
122 refresh_token_, | |
123 access_token_callback); | |
124 } | |
125 | |
126 network_request_run_loop.Run(); | |
127 | |
128 // If we are using the mock access token fetcher, then release it here so it | |
129 // is not destroyed at the end of the function. | |
130 if (mock_access_token_fetcher_) { | |
131 ignore_result(access_token_fetcher.release()); | |
132 } | |
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 (!refresh_token_store_->WriteRefreshTokenToDisk( | |
140 user_name_, | |
141 refresh_token_)) { | |
142 // If we failed to persist the refresh token, then we should let the | |
143 // user sort out the issue before continuing. | |
144 return false; | |
145 } | |
146 } else { | |
147 LOG(ERROR) << "Failed to use AUTH CODE to retrieve a refresh token.\n" | |
148 << "Was the one-time use AUTH CODE used more than once?"; | |
149 return false; | |
150 } | |
151 } | |
152 | |
153 if (access_token_.empty()) { | |
154 LOG(ERROR) << "Failed to retrieve access token."; | |
155 return false; | |
156 } | |
157 | |
158 return true; | |
159 } | |
160 | |
161 void AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved( | |
162 base::Closure run_loop_quit_closure, | |
163 const std::string& access_token, | |
164 const std::string& refresh_token) { | |
165 access_token_ = access_token; | |
166 refresh_token_ = refresh_token; | |
167 | |
168 run_loop_quit_closure.Run(); | |
169 } | |
170 | |
171 } // namespace test | |
172 } // namespace remoting | |
OLD | NEW |