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