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