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

Side by Side Diff: chrome/browser/sync/profile_sync_auth_provider.cc

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

Powered by Google App Engine
This is Rietveld 408576698