1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync_driver/profile_sync_auth_provider.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "google_apis/gaia/gaia_constants.h"
// This is thin proxy class for forwarding calls between sync and UI threads.
// The main purpose is to hide the fact that ProfileSyncAuthProvider and
// SyncThreadProxy have independent lifetimes. If ProfileSyncAuthProvider is
// destroyed first calls to RequestAccessToken will never complete. This is fine
// since sync thread is not blocked and is in the process of shutdown anyway.
class ProfileSyncAuthProvider::SyncThreadProxy
: public syncer::SyncAuthProvider,
public base::NonThreadSafe {
public:
SyncThreadProxy(
base::WeakPtr<ProfileSyncAuthProvider> provider_impl,
scoped_refptr<base::SingleThreadTaskRunner> provider_task_runner);
// syncer::SyncAuthProvider implementation.
void RequestAccessToken(const RequestTokenCallback& callback) override;
void InvalidateAccessToken(const std::string& token) override;
private:
base::WeakPtr<ProfileSyncAuthProvider> provider_impl_;
scoped_refptr<base::SingleThreadTaskRunner> provider_task_runner_;
DISALLOW_COPY_AND_ASSIGN(SyncThreadProxy);
};
ProfileSyncAuthProvider::SyncThreadProxy::SyncThreadProxy(
base::WeakPtr<ProfileSyncAuthProvider> provider_impl,
scoped_refptr<base::SingleThreadTaskRunner> provider_task_runner)
: provider_impl_(provider_impl),
provider_task_runner_(provider_task_runner) {
// SyncThreadProxy is created on UI thread but used on sync thread.
// Detach NonThreadSafe from UI thread so that it can reattach to sync thread
// on first invocation.
DetachFromThread();
}
void ProfileSyncAuthProvider::SyncThreadProxy::RequestAccessToken(
const RequestTokenCallback& callback) {
DCHECK(CalledOnValidThread());
provider_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ProfileSyncAuthProvider::RequestAccessToken,
provider_impl_,
callback,
base::ThreadTaskRunnerHandle::Get()));
}
void ProfileSyncAuthProvider::SyncThreadProxy::InvalidateAccessToken(
const std::string& token) {
DCHECK(CalledOnValidThread());
provider_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ProfileSyncAuthProvider::InvalidateAccessToken,
provider_impl_,
token));
}
ProfileSyncAuthProvider::ProfileSyncAuthProvider(
ProfileOAuth2TokenService* token_service,
const std::string& account_id,
const std::string& scope)
: OAuth2TokenService::Consumer("sync_auth_provider"),
token_service_(token_service),
account_id_(account_id),
weak_factory_(this) {
oauth2_scope_.insert(scope);
}
ProfileSyncAuthProvider::~ProfileSyncAuthProvider() {
DCHECK(CalledOnValidThread());
}
void ProfileSyncAuthProvider::RequestAccessToken(
const syncer::SyncAuthProvider::RequestTokenCallback& callback,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
DCHECK(CalledOnValidThread());
if (access_token_request_ != NULL) {
// If there is already pending request report it as cancelled.
GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED);
RespondToTokenRequest(error, std::string());
}
request_token_callback_ = callback;
callback_task_runner_ = task_runner;
access_token_request_ =
token_service_->StartRequest(account_id_, oauth2_scope_, this);
}
void ProfileSyncAuthProvider::OnGetTokenSuccess(
const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) {
DCHECK_EQ(access_token_request_, request);
RespondToTokenRequest(GoogleServiceAuthError::AuthErrorNone(), access_token);
}
void ProfileSyncAuthProvider::OnGetTokenFailure(
const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) {
DCHECK_EQ(access_token_request_, request);
RespondToTokenRequest(error, std::string());
}
void ProfileSyncAuthProvider::RespondToTokenRequest(
const GoogleServiceAuthError& error,
const std::string& token) {
DCHECK(CalledOnValidThread());
callback_task_runner_->PostTask(
FROM_HERE, base::Bind(request_token_callback_, error, token));
access_token_request_.reset();
request_token_callback_.Reset();
callback_task_runner_ = NULL;
}
void ProfileSyncAuthProvider::InvalidateAccessToken(const std::string& token) {
DCHECK(CalledOnValidThread());
token_service_->InvalidateAccessToken(account_id_, oauth2_scope_, token);
}
scoped_ptr<syncer::SyncAuthProvider>
ProfileSyncAuthProvider::CreateProviderForSyncThread() {
DCHECK(CalledOnValidThread());
scoped_ptr<syncer::SyncAuthProvider> auth_provider(new SyncThreadProxy(
weak_factory_.GetWeakPtr(), base::ThreadTaskRunnerHandle::Get()));
return auth_provider.Pass();
}
|