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

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: Addressing last round of 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 (!refresh_token_store_) {
37 refresh_token_store_ = RefreshTokenStore::OnDisk();
38 }
39
40 // Check to see if we have a refresh token stored for this user.
41 refresh_token_ = refresh_token_store_->FetchRefreshToken(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 bool AppRemotingTestDriverEnvironment::RetrieveAccessToken(
71 const std::string& auth_code) {
72 scoped_ptr<base::MessageLoopForIO> message_loop;
73
74 if (!base::MessageLoop::current()) {
75 // Create a temporary message loop if the current thread does not already
76 // have one so we can use its task runner for our network request.
77 message_loop.reset(new base::MessageLoopForIO);
78 }
79
80 base::RunLoop run_loop;
81
82 access_token_.clear();
83
84 AccessTokenCallback access_token_callback =
85 base::Bind(&AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved,
86 base::Unretained(this),
87 run_loop.QuitClosure());
88
89 // If a unit test has set |test_access_token_fetcher_| then we should use it
90 // below. Note that we do not want to destroy the test object at the end of
91 // the function which is why we have the dance below.
92 scoped_ptr<AccessTokenFetcher> temporary_access_token_fetcher;
93 AccessTokenFetcher* access_token_fetcher = test_access_token_fetcher_.get();
94 if (!access_token_fetcher) {
95 temporary_access_token_fetcher.reset(new AccessTokenFetcher());
96 access_token_fetcher = temporary_access_token_fetcher.get();
97 }
98
99 if (!auth_code.empty()) {
100 // If the user passed in an authcode, then use it to retrieve an
101 // updated access/refresh token.
102 access_token_fetcher->GetAccessTokenFromAuthCode(
103 auth_code,
104 access_token_callback);
105 } else {
106 DCHECK(!refresh_token_.empty());
107
108 access_token_fetcher->GetAccessTokenFromRefreshToken(
109 refresh_token_,
110 access_token_callback);
111 }
112
113 run_loop.Run();
114
115 // If we were using an auth_code and received a valid refresh token,
116 // then we want to store it locally. If we had an auth code and did not
117 // receive a refresh token, then we should let the user know and exit.
118 if (!auth_code.empty()) {
119 if (!refresh_token_.empty()) {
120 if (!refresh_token_store_->StoreRefreshToken(
121 user_name_,
122 refresh_token_)) {
123 // If we failed to persist the refresh token, then we should let the
124 // user sort out the issue before continuing.
125 return false;
126 }
127 } else {
128 LOG(ERROR) << "Failed to use AUTH CODE to retrieve a refresh token.\n"
129 << "Was the one-time use AUTH CODE used more than once?";
130 return false;
131 }
132 }
133
134 if (access_token_.empty()) {
135 LOG(ERROR) << "Failed to retrieve access token.";
136 return false;
137 }
138
139 return true;
140 }
141
142 void AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved(
143 base::Closure done_closure,
144 const std::string& access_token,
145 const std::string& refresh_token) {
146 access_token_ = access_token;
147 refresh_token_ = refresh_token;
148
149 done_closure.Run();
150 }
151
152 } // namespace test
153 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698