summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/gdata/gdata_operation_runner.cc
blob: 73d755d690657037e5aa84425e03c6d571253598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/gdata/gdata_operation_runner.h"

#include "base/bind.h"
#include "chrome/browser/chromeos/gdata/gdata_operations.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace gdata {

GDataOperationRunner::GDataOperationRunner(Profile* profile)
    : profile_(profile),
      auth_service_(new GDataAuthService()),
      operation_registry_(new GDataOperationRegistry()),
      weak_ptr_factory_(this),
      weak_ptr_bound_to_ui_thread_(weak_ptr_factory_.GetWeakPtr()) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  auth_service_->AddObserver(this);
}

GDataOperationRunner::~GDataOperationRunner() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  auth_service_->RemoveObserver(this);
}

void GDataOperationRunner::Initialize() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  auth_service_->Initialize(profile_);
}

void GDataOperationRunner::CancelAll() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  operation_registry_->CancelAll();
}

void GDataOperationRunner::Authenticate(const AuthStatusCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  auth_service_->StartAuthentication(operation_registry_.get(), callback);
}

void GDataOperationRunner::StartOperationWithRetry(
    GDataOperationInterface* operation) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // The re-authenticatation callback will run on UI thread.
  operation->SetReAuthenticateCallback(
      base::Bind(&GDataOperationRunner::RetryOperation,
                 weak_ptr_bound_to_ui_thread_));
  StartOperation(operation);
}

void GDataOperationRunner::StartOperation(GDataOperationInterface* operation) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (!auth_service_->IsFullyAuthenticated()) {
    // Fetch OAuth2 authentication token from the refresh token first.
    auth_service_->StartAuthentication(
        operation_registry_.get(),
        base::Bind(&GDataOperationRunner::OnOperationAuthRefresh,
                   weak_ptr_bound_to_ui_thread_,
                   operation));
    return;
  }

  operation->Start(auth_service_->oauth2_auth_token());
}

void GDataOperationRunner::OnOperationAuthRefresh(
    GDataOperationInterface* operation,
    GDataErrorCode code,
    const std::string& auth_token) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (code == HTTP_SUCCESS) {
    DCHECK(auth_service_->IsPartiallyAuthenticated());
    StartOperation(operation);
  } else {
    operation->OnAuthFailed(code);
  }
}

void GDataOperationRunner::RetryOperation(GDataOperationInterface* operation) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  auth_service_->ClearOAuth2Token();
  // User authentication might have expired - rerun the request to force
  // auth token refresh.
  StartOperation(operation);
}

void GDataOperationRunner::OnOAuth2RefreshTokenChanged() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}

}  // namespace gdata