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
|
// Copyright (c) 2012 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 "chrome/browser/google_apis/auth_service.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/message_loop_proxy.h"
#include "chrome/browser/google_apis/auth_service_observer.h"
#include "chrome/browser/google_apis/operations_base.h"
#include "chrome/browser/google_apis/task_util.h"
#include "chrome/browser/signin/token_service.h"
#include "chrome/browser/signin/token_service_factory.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "google_apis/gaia/gaia_constants.h"
using content::BrowserThread;
namespace gdata {
void AuthService::Initialize(Profile* profile) {
profile_ = profile;
// Get OAuth2 refresh token (if we have any) and register for its updates.
TokenService* service = TokenServiceFactory::GetForProfile(profile_);
refresh_token_ = service->GetOAuth2LoginRefreshToken();
registrar_.Add(this,
chrome::NOTIFICATION_TOKEN_AVAILABLE,
content::Source<TokenService>(service));
registrar_.Add(this,
chrome::NOTIFICATION_TOKEN_REQUEST_FAILED,
content::Source<TokenService>(service));
if (!refresh_token_.empty())
FOR_EACH_OBSERVER(AuthServiceObserver,
observers_,
OnOAuth2RefreshTokenChanged());
}
AuthService::AuthService(const std::vector<std::string>& scopes)
: profile_(NULL),
scopes_(scopes),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
AuthService::~AuthService() {
}
void AuthService::StartAuthentication(OperationRegistry* registry,
const AuthStatusCallback& callback) {
scoped_refptr<base::MessageLoopProxy> relay_proxy(
base::MessageLoopProxy::current());
if (HasAccessToken()) {
relay_proxy->PostTask(FROM_HERE,
base::Bind(callback, gdata::HTTP_SUCCESS, access_token_));
} else if (HasRefreshToken()) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&AuthService::StartAuthenticationOnUIThread,
weak_ptr_factory_.GetWeakPtr(),
registry,
CreateRelayCallback(
base::Bind(&AuthService::OnAuthCompleted,
weak_ptr_factory_.GetWeakPtr(),
callback))));
} else {
relay_proxy->PostTask(FROM_HERE,
base::Bind(callback, gdata::GDATA_NOT_READY, std::string()));
}
}
void AuthService::StartAuthenticationOnUIThread(
OperationRegistry* registry,
const AuthStatusCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// We have refresh token, let's gets authenticated.
(new AuthOperation(registry, callback, scopes_, refresh_token_))->Start();
}
void AuthService::OnAuthCompleted(const AuthStatusCallback& callback,
GDataErrorCode error,
const std::string& access_token) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
if (error == HTTP_SUCCESS)
access_token_ = access_token;
// TODO(zelidrag): Add retry, back-off logic when things go wrong here.
callback.Run(error, access_token);
}
void AuthService::AddObserver(AuthServiceObserver* observer) {
observers_.AddObserver(observer);
}
void AuthService::RemoveObserver(AuthServiceObserver* observer) {
observers_.RemoveObserver(observer);
}
void AuthService::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE ||
type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED);
TokenService::TokenAvailableDetails* token_details =
content::Details<TokenService::TokenAvailableDetails>(details).ptr();
if (token_details->service() != GaiaConstants::kGaiaOAuth2LoginRefreshToken)
return;
access_token_.clear();
if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) {
TokenService* service = TokenServiceFactory::GetForProfile(profile_);
refresh_token_ = service->GetOAuth2LoginRefreshToken();
} else {
refresh_token_.clear();
}
FOR_EACH_OBSERVER(AuthServiceObserver,
observers_,
OnOAuth2RefreshTokenChanged());
}
} // namespace gdata
|