1// Copyright 2014 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#ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_AUTH_PROVIDER_H_
6#define CHROME_BROWSER_SYNC_PROFILE_SYNC_AUTH_PROVIDER_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/memory/weak_ptr.h"
10#include "google_apis/gaia/oauth2_token_service.h"
11#include "sync/internal_api/public/sync_auth_provider.h"
12
13class ProfileOAuth2TokenService;
14
15namespace base {
16class SingleThreadTaskRunner;
17}  // namespace base
18
19// ProfileSyncAuthProvider implements function for SyncAuthProvider. It doesn't
20// inherit from SyncAuthProvider because it lives on UI thread while requests
21// originate from sync thread. SyncThreadProxy implements SyncAuthProvider and
22// forwards all requests.
23class ProfileSyncAuthProvider : public OAuth2TokenService::Consumer,
24                                public base::NonThreadSafe {
25 public:
26  ProfileSyncAuthProvider(ProfileOAuth2TokenService* token_service,
27                          const std::string& account_id,
28                          const std::string& scope);
29  virtual ~ProfileSyncAuthProvider();
30
31  // OAuth2TokenService::Consumer implementation.
32  virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
33                                 const std::string& access_token,
34                                 const base::Time& expiration_time) OVERRIDE;
35  virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
36                                 const GoogleServiceAuthError& error) OVERRIDE;
37
38  // Request access token from OAuth2TokenService. Once access token is received
39  // result should be posted to callback on task_runner thread.
40  void RequestAccessToken(
41      const syncer::SyncAuthProvider::RequestTokenCallback& callback,
42      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
43
44  void InvalidateAccessToken(const std::string& token);
45
46  scoped_ptr<syncer::SyncAuthProvider> CreateProviderForSyncThread();
47
48 private:
49  class SyncThreadProxy;
50
51  void RespondToTokenRequest(const GoogleServiceAuthError& error,
52                             const std::string& token);
53
54  ProfileOAuth2TokenService* token_service_;
55  std::string account_id_;
56  OAuth2TokenService::ScopeSet oauth2_scope_;
57
58  // Only one outstanding request is allowed. Previous request is reported
59  // cancelled if new one arrives.
60  scoped_ptr<OAuth2TokenService::Request> access_token_request_;
61  syncer::SyncAuthProvider::RequestTokenCallback request_token_callback_;
62  scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_;
63
64  base::WeakPtrFactory<ProfileSyncAuthProvider> weak_factory_;
65
66  DISALLOW_COPY_AND_ASSIGN(ProfileSyncAuthProvider);
67};
68
69#endif  // CHROME_BROWSER_SYNC_PROFILE_SYNC_AUTH_PROVIDER_H_
70