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

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: Fixing some lint/readability errors 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 initialized_(false) {
24 DCHECK(!user_name_.empty());
25 DCHECK(!service_environment.empty());
26 }
27
28 AppRemotingTestDriverEnvironment::~AppRemotingTestDriverEnvironment() {
29 }
30
31 bool AppRemotingTestDriverEnvironment::Initialize(
32 const std::string& auth_code) {
33 if (initialized_) {
Wez 2015/02/13 03:01:53 Do you want to let things try to initialize this s
joedow 2015/02/14 02:31:27 The Environment class is only initialized once (be
34 return true;
35 }
36
37 if (!refresh_token_storage_.get()) {
Wez 2015/02/13 03:01:53 Drop .get()
joedow 2015/02/14 02:31:27 Done.
38 refresh_token_storage_.reset(new remoting::test::RefreshTokenStorage());
39 }
40
41 // Check to see if we have a refresh token stored for this user.
42 refresh_token_ = refresh_token_storage_->ReadRefreshTokenFromDisk(user_name_);
43 if (refresh_token_.empty()) {
44 // This isn't necessarily an error as this might be a first run scenario.
45 DVLOG(1) << "No refresh token stored for " << user_name_;
46
47 if (auth_code.empty()) {
48 // No token and no Auth code means no service connectivity, bail!
49 LOG(ERROR) << "Cannot retrieve an access token without a stored refresh"
50 << " token on disk or an auth_code passed into the tool";
51 return false;
52 }
53 }
54
55 if (!RetrieveAccessToken(auth_code)) {
56 // If we cannot retrieve an access token, then nothing is going to work and
57 // we should let the caller know that our object is not ready to be used.
58 return false;
59 }
60
61 initialized_ = true;
62
63 return true;
64 }
65
66 bool AppRemotingTestDriverEnvironment::RefreshAccessToken() {
67 DCHECK(!refresh_token_.empty());
68
69 std::string auth_code; // Empty auth code is used when refreshing.
Wez 2015/02/13 03:01:52 ?
joedow 2015/02/14 02:31:27 Done.
70 return RetrieveAccessToken(auth_code);
Wez 2015/02/13 03:01:53 RetrieveAccessToken might be better being RefreshA
joedow 2015/02/14 02:31:27 Done. I had thought about splitting the RetrieveA
Wez 2015/02/19 22:00:22 Acknowledged.
71 }
72
73 void AppRemotingTestDriverEnvironment::SetAccessTokenFetcherForTesting(
74 remoting::test::AccessTokenFetcher* mock_fetcher) {
Wez 2015/02/13 03:01:52 This parameter should be a scoped_ptr<>
joedow 2015/02/14 02:31:27 Done.
75 access_token_fetcher_.reset(mock_fetcher);
76 }
77
78 void AppRemotingTestDriverEnvironment::SetRefreshTokenStorageForTesting(
79 remoting::test::RefreshTokenStorageInterface* mock_refresh_token_storage) {
Wez 2015/02/13 03:01:53 As above
joedow 2015/02/14 02:31:27 Done.
80 refresh_token_storage_.reset(mock_refresh_token_storage);
81 }
82
83 void AppRemotingTestDriverEnvironment::SetUp() {
84 // The object should already be initialized by the time SetUp is called.
Wez 2015/02/13 03:01:53 I thought SetUp and TearDown were called once per-
joedow 2015/02/14 02:31:27 The environment class is setup and torn down once
Wez 2015/02/19 22:00:22 Acknowledged.
85 DCHECK(initialized_);
86 }
87
88 void AppRemotingTestDriverEnvironment::TearDown() {
89 if (!initialized_) {
Wez 2015/02/13 03:01:52 Why would TearDown be called if we're not initiali
joedow 2015/02/14 02:31:27 Done.
90 return;
91 }
92
93 // Tear down the members that we created in the SetUp method here.
94
95 initialized_ = false;
96 }
97
98 bool AppRemotingTestDriverEnvironment::RetrieveAccessToken(
99 const std::string& auth_code) {
100 scoped_ptr<base::MessageLoopForIO> message_loop;
101
102 if (!base::MessageLoop::current()) {
103 // Create a temporary message loop if the current thread does not already
104 // have one so we can use its task runner for our network request.
105 message_loop.reset(new base::MessageLoopForIO);
106 }
107 DCHECK(!network_request_run_loop_.get());
108 network_request_run_loop_.reset(new base::RunLoop());
Wez 2015/02/13 03:01:53 You can just create this on the stack, can't you,
joedow 2015/02/14 02:31:27 Done.
109
110 access_token_.clear();
111
112 AccessTokenCallback access_token_callback = base::Bind(
113 &AppRemotingTestDriverEnvironment::OnAccessTokenRetrieved,
114 base::Unretained(this));
115
116 if (!access_token_fetcher_.get()) {
117 access_token_fetcher_.reset(new remoting::test::AccessTokenFetcher());
Wez 2015/02/13 03:01:53 Are you sure that the fetcher won't bind to the po
joedow 2015/02/14 02:31:27 I updated the code so the real access token fetche
Wez 2015/02/19 22:00:22 Sorry, I don't understand what you mean.
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(auth_code,
124 access_token_callback);
125 } else {
126 DCHECK(!refresh_token_.empty());
127
128 access_token_fetcher_->GetAccessTokenFromRefreshToken(refresh_token_,
129 access_token_callback);
130 }
131
132 network_request_run_loop_->Run();
133 network_request_run_loop_.reset();
134
135 // If we were using an auth_code and received a valid refresh token,
136 // then we want to store it locally. If we had an auth code and did not
137 // receive a refresh token, then we should let the user know and exit.
138 if (!auth_code.empty()) {
139 if (!refresh_token_.empty()) {
140 if (!refresh_token_storage_->
141 WriteRefreshTokenToDisk(user_name_, 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 const std::string& access_token,
163 const std::string& refresh_token) {
164 access_token_ = access_token;
165 refresh_token_ = refresh_token;
166
167 network_request_run_loop_->Quit();
168 }
169
170 } // namespace test
171 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698