Commit 7c009a74 authored by Yuwei Huang's avatar Yuwei Huang Committed by Commit Bot

[CRD iOS] Feed logger immediately with access token

Previously we don't feed the logger with access token until the logger
fails to send a log due to denied permissions, which will pollute our
error rate statistics.

This CL makes the app set the logger's access token immediately after the
user logs in or switches account.

Bug: 750366
Change-Id: I6898efd73339b2207d954422bcf2410eb0efa4f5
Reviewed-on: https://chromium-review.googlesource.com/591510Reviewed-by: default avatarScott Nichols <nicholss@chromium.org>
Commit-Queue: Yuwei Huang <yuweih@chromium.org>
Cr-Commit-Position: refs/heads/master@{#490788}
parent f9491126
......@@ -21,6 +21,9 @@ class IosClientRuntimeDelegate : public ChromotingClientRuntime::Delegate {
void RuntimeDidShutdown() override;
void RequestAuthTokenForLogger() override;
// Sets the access token. Should be called when the user switches accounts.
void SetAuthToken(const std::string& access_token);
base::WeakPtr<IosClientRuntimeDelegate> GetWeakPtr();
private:
......
......@@ -39,6 +39,8 @@ void IosClientRuntimeDelegate::RuntimeDidShutdown() {
void IosClientRuntimeDelegate::RequestAuthTokenForLogger() {
if (!runtime_->ui_task_runner()->BelongsToCurrentThread()) {
// TODO(yuweih): base::Unretained(this) looks suspicious here. Maybe we can
// use GetWeakPtr()?
runtime_->ui_task_runner()->PostTask(
FROM_HERE,
base::Bind(&IosClientRuntimeDelegate::RequestAuthTokenForLogger,
......@@ -51,11 +53,7 @@ void IosClientRuntimeDelegate::RequestAuthTokenForLogger() {
NSString* userEmail, NSString* accessToken) {
if (status == RemotingAuthenticationStatusSuccess) {
// Set the new auth token for the log writer on the network thread.
std::string access_token = base::SysNSStringToUTF8(accessToken);
runtime_->network_task_runner()->PostTask(
FROM_HERE, base::BindBlockArc(^{
runtime_->log_writer()->SetAuthToken(access_token);
}));
SetAuthToken(base::SysNSStringToUTF8(accessToken));
} else {
LOG(ERROR) << "Failed to fetch access token for log writer. ("
<< status << ")";
......@@ -64,6 +62,28 @@ void IosClientRuntimeDelegate::RequestAuthTokenForLogger() {
}
}
void IosClientRuntimeDelegate::SetAuthToken(const std::string& access_token) {
if (runtime_->network_task_runner()->BelongsToCurrentThread()) {
runtime_->log_writer()->SetAuthToken(access_token);
return;
}
runtime_->network_task_runner()->PostTask(
FROM_HERE,
base::Bind(
base::BindBlockArc(^(const std::string& token) {
// |token| need to be bound separately since the Objective-C block
// captures the reference without copying.
// Using |runtime_| here will be unsafe since that will implicitly
// capture |this|, of which the lifetime is not managed by the
// block.
ChromotingClientRuntime::GetInstance()->log_writer()->SetAuthToken(
token);
}),
access_token));
}
base::WeakPtr<IosClientRuntimeDelegate> IosClientRuntimeDelegate::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
......
......@@ -151,6 +151,12 @@ NSString* const kUserInfo = @"kUserInfo";
if (user) {
userInfo = [NSDictionary dictionaryWithObject:user forKey:kUserInfo];
[self requestHostListFetch];
[_authentication
callbackWithAccessToken:^(RemotingAuthenticationStatus status,
NSString* userEmail, NSString* accessToken) {
_clientRuntimeDelegate->SetAuthToken(
base::SysNSStringToUTF8(accessToken));
}];
} else {
_hosts = nil;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment