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 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(); |
| 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(user_name_); |
| 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(); |
| 145 refresh_token_store = temporary_refresh_token_store.get(); |
| 146 } |
| 147 |
| 148 if (!refresh_token_store->StoreRefreshToken( |
| 149 user_name_, |
| 150 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 |