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