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

Side by Side Diff: chrome/browser/chromeos/gdata/auth_service.cc

Issue 10920091: Move Drive API files to google_apis directory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/chromeos/gdata/auth_service.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/bind.h"
11 #include "base/message_loop_proxy.h"
12 #include "chrome/browser/chromeos/gdata/operations_base.h"
13 #include "chrome/browser/chromeos/gdata/task_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/signin/token_service.h"
16 #include "chrome/browser/signin/token_service_factory.h"
17 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/net/gaia/gaia_constants.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/notification_types.h"
23
24 using content::BrowserThread;
25
26 namespace gdata {
27
28 void AuthService::Initialize(Profile* profile) {
29 profile_ = profile;
30 // Get OAuth2 refresh token (if we have any) and register for its updates.
31 TokenService* service = TokenServiceFactory::GetForProfile(profile_);
32 refresh_token_ = service->GetOAuth2LoginRefreshToken();
33 registrar_.Add(this,
34 chrome::NOTIFICATION_TOKEN_AVAILABLE,
35 content::Source<TokenService>(service));
36 registrar_.Add(this,
37 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED,
38 content::Source<TokenService>(service));
39
40 if (!refresh_token_.empty())
41 FOR_EACH_OBSERVER(Observer, observers_, OnOAuth2RefreshTokenChanged());
42 }
43
44 AuthService::AuthService(const std::vector<std::string>& scopes)
45 : profile_(NULL),
46 scopes_(scopes),
47 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 }
50
51 AuthService::~AuthService() {
52 }
53
54 void AuthService::StartAuthentication(OperationRegistry* registry,
55 const AuthStatusCallback& callback) {
56 scoped_refptr<base::MessageLoopProxy> relay_proxy(
57 base::MessageLoopProxy::current());
58
59 if (HasAccessToken()) {
60 relay_proxy->PostTask(FROM_HERE,
61 base::Bind(callback, gdata::HTTP_SUCCESS, access_token_));
62 } else if (HasRefreshToken()) {
63 BrowserThread::PostTask(
64 BrowserThread::UI,
65 FROM_HERE,
66 base::Bind(&AuthService::StartAuthenticationOnUIThread,
67 weak_ptr_factory_.GetWeakPtr(),
68 registry,
69 CreateRelayCallback(
70 base::Bind(&AuthService::OnAuthCompleted,
71 weak_ptr_factory_.GetWeakPtr(),
72 callback))));
73 } else {
74 relay_proxy->PostTask(FROM_HERE,
75 base::Bind(callback, gdata::GDATA_NOT_READY, std::string()));
76 }
77 }
78
79 void AuthService::StartAuthenticationOnUIThread(
80 OperationRegistry* registry,
81 const AuthStatusCallback& callback) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 // We have refresh token, let's gets authenticated.
84 (new AuthOperation(registry, callback, scopes_, refresh_token_))->Start();
85 }
86
87 void AuthService::OnAuthCompleted(const AuthStatusCallback& callback,
88 GDataErrorCode error,
89 const std::string& access_token) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
91 DCHECK(!callback.is_null());
92
93 if (error == HTTP_SUCCESS)
94 access_token_ = access_token;
95
96 // TODO(zelidrag): Add retry, back-off logic when things go wrong here.
97 callback.Run(error, access_token);
98 }
99
100 void AuthService::AddObserver(Observer* observer) {
101 observers_.AddObserver(observer);
102 }
103
104 void AuthService::RemoveObserver(Observer* observer) {
105 observers_.RemoveObserver(observer);
106 }
107
108 void AuthService::Observe(int type,
109 const content::NotificationSource& source,
110 const content::NotificationDetails& details) {
111 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE ||
112 type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED);
113
114 TokenService::TokenAvailableDetails* token_details =
115 content::Details<TokenService::TokenAvailableDetails>(details).ptr();
116 if (token_details->service() != GaiaConstants::kGaiaOAuth2LoginRefreshToken)
117 return;
118
119 access_token_.clear();
120 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) {
121 TokenService* service = TokenServiceFactory::GetForProfile(profile_);
122 refresh_token_ = service->GetOAuth2LoginRefreshToken();
123 } else {
124 refresh_token_.clear();
125 }
126 FOR_EACH_OBSERVER(Observer, observers_, OnOAuth2RefreshTokenChanged());
127 }
128
129 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698