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