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_operation_runner.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "chrome/browser/chromeos/gdata/gdata_operations.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 | |
12 using content::BrowserThread; | |
13 | |
14 namespace gdata { | |
15 | |
16 GDataOperationRunner::GDataOperationRunner(Profile* profile) | |
17 : profile_(profile), | |
18 auth_service_(new GDataAuthService()), | |
19 operation_registry_(new GDataOperationRegistry()), | |
20 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
22 auth_service_->AddObserver(this); | |
23 } | |
24 | |
25 GDataOperationRunner::~GDataOperationRunner() { | |
26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
27 auth_service_->RemoveObserver(this); | |
28 } | |
29 | |
30 void GDataOperationRunner::Initialize() { | |
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
32 auth_service_->Initialize(profile_); | |
33 } | |
34 | |
35 void GDataOperationRunner::CancelAll() { | |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
37 operation_registry_->CancelAll(); | |
38 } | |
39 | |
40 void GDataOperationRunner::Authenticate(const AuthStatusCallback& callback) { | |
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
42 auth_service_->StartAuthentication(operation_registry_.get(), callback); | |
43 } | |
44 | |
45 void GDataOperationRunner::StartOperationWithRetry( | |
46 GDataOperationInterface* operation) { | |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
48 | |
49 // The re-authenticatation callback will run on UI thread. | |
50 operation->SetReAuthenticateCallback( | |
51 base::Bind(&GDataOperationRunner::RetryOperation, | |
52 weak_ptr_factory_.GetWeakPtr())); | |
53 StartOperation(operation); | |
54 } | |
55 | |
56 void GDataOperationRunner::StartOperation(GDataOperationInterface* operation) { | |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
58 | |
59 if (!auth_service_->HasAccessToken()) { | |
60 // Fetch OAuth2 authentication token from the refresh token first. | |
61 auth_service_->StartAuthentication( | |
62 operation_registry_.get(), | |
63 base::Bind(&GDataOperationRunner::OnOperationAuthRefresh, | |
64 weak_ptr_factory_.GetWeakPtr(), | |
65 operation)); | |
66 return; | |
67 } | |
68 | |
69 operation->Start(auth_service_->access_token()); | |
70 } | |
71 | |
72 void GDataOperationRunner::OnOperationAuthRefresh( | |
73 GDataOperationInterface* operation, | |
74 GDataErrorCode code, | |
75 const std::string& auth_token) { | |
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
77 | |
78 if (code == HTTP_SUCCESS) { | |
79 DCHECK(auth_service_->HasRefreshToken()); | |
80 StartOperation(operation); | |
81 } else { | |
82 operation->OnAuthFailed(code); | |
83 } | |
84 } | |
85 | |
86 void GDataOperationRunner::RetryOperation(GDataOperationInterface* operation) { | |
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
88 | |
89 auth_service_->ClearAccessToken(); | |
90 // User authentication might have expired - rerun the request to force | |
91 // auth token refresh. | |
92 StartOperation(operation); | |
93 } | |
94 | |
95 void GDataOperationRunner::OnOAuth2RefreshTokenChanged() { | |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
97 } | |
98 | |
99 } // namespace gdata | |
OLD | NEW |