| 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 "remoting/test/app_remoting_test_driver_environment.h" | 5 #include "remoting/test/app_remoting_test_driver_environment.h" |
| 6 | 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 7 #include "base/bind.h" | 11 #include "base/bind.h" |
| 8 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 9 #include "base/logging.h" | 13 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/stringprintf.h" |
| 12 #include "remoting/test/access_token_fetcher.h" | 17 #include "remoting/test/access_token_fetcher.h" |
| 13 #include "remoting/test/refresh_token_store.h" | 18 #include "remoting/test/refresh_token_store.h" |
| 14 #include "remoting/test/remote_host_info.h" | 19 #include "remoting/test/remote_host_info.h" |
| 15 | 20 |
| 16 namespace remoting { | 21 namespace remoting { |
| 17 namespace test { | 22 namespace test { |
| 18 | 23 |
| 19 AppRemotingTestDriverEnvironment* AppRemotingSharedData; | 24 AppRemotingTestDriverEnvironment* AppRemotingSharedData; |
| 20 | 25 |
| 21 AppRemotingTestDriverEnvironment::AppRemotingTestDriverEnvironment( | 26 AppRemotingTestDriverEnvironment::AppRemotingTestDriverEnvironment( |
| 22 const std::string& user_name, | 27 const std::string& user_name, |
| 23 ServiceEnvironment service_environment) | 28 ServiceEnvironment service_environment) |
| 24 : user_name_(user_name), | 29 : user_name_(user_name), |
| 25 service_environment_(service_environment), | 30 service_environment_(service_environment), |
| 26 test_access_token_fetcher_(nullptr), | 31 test_access_token_fetcher_(nullptr), |
| 27 test_refresh_token_store_(nullptr), | 32 test_refresh_token_store_(nullptr), |
| 28 test_remote_host_info_fetcher_(nullptr) { | 33 test_remote_host_info_fetcher_(nullptr) { |
| 29 DCHECK(!user_name_.empty()); | 34 DCHECK(!user_name_.empty()); |
| 30 DCHECK(service_environment < kUnknownEnvironment); | 35 DCHECK(service_environment < kUnknownEnvironment); |
| 36 |
| 37 PopulateApplicationNames(); |
| 38 PopulateApplicationDetailsMap(); |
| 31 } | 39 } |
| 32 | 40 |
| 33 AppRemotingTestDriverEnvironment::~AppRemotingTestDriverEnvironment() { | 41 AppRemotingTestDriverEnvironment::~AppRemotingTestDriverEnvironment() { |
| 34 } | 42 } |
| 35 | 43 |
| 36 bool AppRemotingTestDriverEnvironment::Initialize( | 44 bool AppRemotingTestDriverEnvironment::Initialize( |
| 37 const std::string& auth_code) { | 45 const std::string& auth_code) { |
| 38 if (!access_token_.empty()) { | 46 if (!access_token_.empty()) { |
| 39 return true; | 47 return true; |
| 40 } | 48 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 100 |
| 93 scoped_ptr<base::MessageLoopForIO> message_loop; | 101 scoped_ptr<base::MessageLoopForIO> message_loop; |
| 94 if (!base::MessageLoop::current()) { | 102 if (!base::MessageLoop::current()) { |
| 95 // Create a temporary message loop if the current thread does not already | 103 // Create a temporary message loop if the current thread does not already |
| 96 // have one so we can use its task runner for our network request. | 104 // have one so we can use its task runner for our network request. |
| 97 message_loop.reset(new base::MessageLoopForIO); | 105 message_loop.reset(new base::MessageLoopForIO); |
| 98 } | 106 } |
| 99 | 107 |
| 100 base::RunLoop run_loop; | 108 base::RunLoop run_loop; |
| 101 | 109 |
| 102 RemoteHostInfoCallback remote_host_info_fetch_callback = | 110 RemoteHostInfoCallback remote_host_info_fetch_callback = base::Bind( |
| 103 base::Bind(&AppRemotingTestDriverEnvironment::OnRemoteHostInfoRetrieved, | 111 &AppRemotingTestDriverEnvironment::OnRemoteHostInfoRetrieved, |
| 104 base::Unretained(this), | 112 base::Unretained(this), run_loop.QuitClosure(), remote_host_info); |
| 105 run_loop.QuitClosure(), | |
| 106 remote_host_info); | |
| 107 | 113 |
| 108 // If a unit test has set |test_remote_host_info_fetcher_| then we should use | 114 // If a unit test has set |test_remote_host_info_fetcher_| then we should use |
| 109 // it below. Note that we do not want to destroy the test object at the end | 115 // it below. Note that we do not want to destroy the test object at the end |
| 110 // of the function which is why we have the dance below. | 116 // of the function which is why we have the dance below. |
| 111 scoped_ptr<RemoteHostInfoFetcher> temporary_remote_host_info_fetcher; | 117 scoped_ptr<RemoteHostInfoFetcher> temporary_remote_host_info_fetcher; |
| 112 RemoteHostInfoFetcher* remote_host_info_fetcher = | 118 RemoteHostInfoFetcher* remote_host_info_fetcher = |
| 113 test_remote_host_info_fetcher_; | 119 test_remote_host_info_fetcher_; |
| 114 if (!remote_host_info_fetcher) { | 120 if (!remote_host_info_fetcher) { |
| 115 temporary_remote_host_info_fetcher.reset(new RemoteHostInfoFetcher()); | 121 temporary_remote_host_info_fetcher.reset(new RemoteHostInfoFetcher()); |
| 116 remote_host_info_fetcher = temporary_remote_host_info_fetcher.get(); | 122 remote_host_info_fetcher = temporary_remote_host_info_fetcher.get(); |
| 117 } | 123 } |
| 118 | 124 |
| 119 remote_host_info_fetcher->RetrieveRemoteHostInfo( | 125 remote_host_info_fetcher->RetrieveRemoteHostInfo( |
| 120 application_id, | 126 application_id, access_token_, service_environment_, |
| 121 access_token_, | |
| 122 service_environment_, | |
| 123 remote_host_info_fetch_callback); | 127 remote_host_info_fetch_callback); |
| 124 | 128 |
| 125 run_loop.Run(); | 129 run_loop.Run(); |
| 126 | 130 |
| 127 return remote_host_info->IsReadyForConnection(); | 131 return remote_host_info->IsReadyForConnection(); |
| 128 } | 132 } |
| 129 | 133 |
| 134 void AppRemotingTestDriverEnvironment::ShowHostAvailability() { |
| 135 const char kHostAvailabilityFormatString[] = "%-25s%-35s%-10s"; |
| 136 std::vector<std::string>::const_iterator it = application_names_.begin(); |
| 137 |
| 138 LOG(INFO) << base::StringPrintf(kHostAvailabilityFormatString, |
| 139 "Application Name", "Application ID", |
| 140 "Status"); |
| 141 |
| 142 while (it != application_names_.end()) { |
| 143 std::string application_name = *it; |
| 144 |
| 145 const RemoteApplicationDetails& application_details = GetDetailsFromAppName( |
| 146 application_name); |
| 147 |
| 148 RemoteHostInfo remote_host_info; |
| 149 GetRemoteHostInfoForApplicationId(application_details.application_id, |
| 150 &remote_host_info); |
| 151 |
| 152 std::string status; |
| 153 RemoteHostStatus remote_host_status = remote_host_info.remote_host_status; |
| 154 if (remote_host_status == kRemoteHostStatusReady) { |
| 155 status = "Ready :)"; |
| 156 } else if (remote_host_status == kRemoteHostStatusPending) { |
| 157 status = "Pending :|"; |
| 158 } else { |
| 159 status = "Unknown :("; |
| 160 } |
| 161 |
| 162 LOG(INFO) << base::StringPrintf( |
| 163 kHostAvailabilityFormatString, application_name.c_str(), |
| 164 application_details.application_id.c_str(), status.c_str()); |
| 165 |
| 166 ++it; |
| 167 } |
| 168 } |
| 169 |
| 170 const RemoteApplicationDetails& |
| 171 AppRemotingTestDriverEnvironment::GetDetailsFromAppName( |
| 172 const std::string& application_name) { |
| 173 DCHECK_GT(application_details_map_.count(application_name), 0UL); |
| 174 |
| 175 return application_details_map_.at(application_name); |
| 176 } |
| 177 |
| 130 void AppRemotingTestDriverEnvironment::SetAccessTokenFetcherForTest( | 178 void AppRemotingTestDriverEnvironment::SetAccessTokenFetcherForTest( |
| 131 AccessTokenFetcher* access_token_fetcher) { | 179 AccessTokenFetcher* access_token_fetcher) { |
| 132 DCHECK(access_token_fetcher); | 180 DCHECK(access_token_fetcher); |
| 133 | 181 |
| 134 test_access_token_fetcher_ = access_token_fetcher; | 182 test_access_token_fetcher_ = access_token_fetcher; |
| 135 } | 183 } |
| 136 | 184 |
| 137 void AppRemotingTestDriverEnvironment::SetRefreshTokenStoreForTest( | 185 void AppRemotingTestDriverEnvironment::SetRefreshTokenStoreForTest( |
| 138 RefreshTokenStore* refresh_token_store) { | 186 RefreshTokenStore* refresh_token_store) { |
| 139 DCHECK(refresh_token_store); | 187 DCHECK(refresh_token_store); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 157 // have one so we can use its task runner for our network request. | 205 // have one so we can use its task runner for our network request. |
| 158 message_loop.reset(new base::MessageLoopForIO); | 206 message_loop.reset(new base::MessageLoopForIO); |
| 159 } | 207 } |
| 160 | 208 |
| 161 base::RunLoop run_loop; | 209 base::RunLoop run_loop; |
| 162 | 210 |
| 163 access_token_.clear(); | 211 access_token_.clear(); |
| 164 | 212 |
| 165 AccessTokenCallback access_token_callback = | 213 AccessTokenCallback access_token_callback = |
| 166 base::Bind(&AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved, | 214 base::Bind(&AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved, |
| 167 base::Unretained(this), | 215 base::Unretained(this), run_loop.QuitClosure()); |
| 168 run_loop.QuitClosure()); | |
| 169 | 216 |
| 170 // If a unit test has set |test_access_token_fetcher_| then we should use it | 217 // If a unit test has set |test_access_token_fetcher_| then we should use it |
| 171 // below. Note that we do not want to destroy the test object at the end of | 218 // below. Note that we do not want to destroy the test object at the end of |
| 172 // the function which is why we have the dance below. | 219 // the function which is why we have the dance below. |
| 173 scoped_ptr<AccessTokenFetcher> temporary_access_token_fetcher; | 220 scoped_ptr<AccessTokenFetcher> temporary_access_token_fetcher; |
| 174 AccessTokenFetcher* access_token_fetcher = test_access_token_fetcher_; | 221 AccessTokenFetcher* access_token_fetcher = test_access_token_fetcher_; |
| 175 if (!access_token_fetcher) { | 222 if (!access_token_fetcher) { |
| 176 temporary_access_token_fetcher.reset(new AccessTokenFetcher()); | 223 temporary_access_token_fetcher.reset(new AccessTokenFetcher()); |
| 177 access_token_fetcher = temporary_access_token_fetcher.get(); | 224 access_token_fetcher = temporary_access_token_fetcher.get(); |
| 178 } | 225 } |
| 179 | 226 |
| 180 if (!auth_code.empty()) { | 227 if (!auth_code.empty()) { |
| 181 // If the user passed in an authcode, then use it to retrieve an | 228 // If the user passed in an authcode, then use it to retrieve an |
| 182 // updated access/refresh token. | 229 // updated access/refresh token. |
| 183 access_token_fetcher->GetAccessTokenFromAuthCode( | 230 access_token_fetcher->GetAccessTokenFromAuthCode(auth_code, |
| 184 auth_code, | 231 access_token_callback); |
| 185 access_token_callback); | |
| 186 } else { | 232 } else { |
| 187 DCHECK(!refresh_token_.empty()); | 233 DCHECK(!refresh_token_.empty()); |
| 188 | 234 |
| 189 access_token_fetcher->GetAccessTokenFromRefreshToken( | 235 access_token_fetcher->GetAccessTokenFromRefreshToken(refresh_token_, |
| 190 refresh_token_, | 236 access_token_callback); |
| 191 access_token_callback); | |
| 192 } | 237 } |
| 193 | 238 |
| 194 run_loop.Run(); | 239 run_loop.Run(); |
| 195 | 240 |
| 196 // If we were using an auth_code and received a valid refresh token, | 241 // If we were using an auth_code and received a valid refresh token, |
| 197 // then we want to store it locally. If we had an auth code and did not | 242 // then we want to store it locally. If we had an auth code and did not |
| 198 // receive a refresh token, then we should let the user know and exit. | 243 // receive a refresh token, then we should let the user know and exit. |
| 199 if (!auth_code.empty()) { | 244 if (!auth_code.empty()) { |
| 200 if (!refresh_token_.empty()) { | 245 if (!refresh_token_.empty()) { |
| 201 // If a unit test has set |test_refresh_token_store_| then we should use | 246 // If a unit test has set |test_refresh_token_store_| then we should use |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 | 281 |
| 237 done_closure.Run(); | 282 done_closure.Run(); |
| 238 } | 283 } |
| 239 | 284 |
| 240 void AppRemotingTestDriverEnvironment::OnRemoteHostInfoRetrieved( | 285 void AppRemotingTestDriverEnvironment::OnRemoteHostInfoRetrieved( |
| 241 base::Closure done_closure, | 286 base::Closure done_closure, |
| 242 RemoteHostInfo* remote_host_info, | 287 RemoteHostInfo* remote_host_info, |
| 243 const RemoteHostInfo& retrieved_remote_host_info) { | 288 const RemoteHostInfo& retrieved_remote_host_info) { |
| 244 DCHECK(remote_host_info); | 289 DCHECK(remote_host_info); |
| 245 | 290 |
| 246 if (retrieved_remote_host_info.IsReadyForConnection()) { | 291 *remote_host_info = retrieved_remote_host_info; |
| 247 *remote_host_info = retrieved_remote_host_info; | |
| 248 } | |
| 249 | 292 |
| 250 done_closure.Run(); | 293 done_closure.Run(); |
| 251 } | 294 } |
| 252 | 295 |
| 253 } // namespace test | 296 } // namespace test |
| 254 } // namespace remoting | 297 } // namespace remoting |
| OLD | NEW |