1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/containers/scoped_ptr_hash_map.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/threading/non_thread_safe.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "google_apis/gaia/oauth2_token_service_request.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/url_fetcher_delegate.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/url_request/url_request_context_getter.h"
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "sync/internal_api/public/attachments/attachment_downloader.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "url/gurl.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace syncer {
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// An implementation of AttachmentDownloader.
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass AttachmentDownloaderImpl : public AttachmentDownloader,
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 public OAuth2TokenService::Consumer,
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 public net::URLFetcherDelegate,
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 public base::NonThreadSafe {
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |sync_service_url| is the URL of the sync service.
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |url_request_context_getter| provides a URLRequestContext.
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |account_id| is the account id to use for downloads.
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |scopes| is the set of scopes to use for downloads.
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |token_service_provider| provides an OAuth2 token service.
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  AttachmentDownloaderImpl(
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const GURL& sync_service_url,
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const scoped_refptr<net::URLRequestContextGetter>&
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          url_request_context_getter,
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const std::string& account_id,
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const OAuth2TokenService::ScopeSet& scopes,
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>&
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          token_service_provider);
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~AttachmentDownloaderImpl();
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // AttachmentDownloader implementation.
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void DownloadAttachment(const AttachmentId& attachment_id,
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                  const DownloadCallback& callback) OVERRIDE;
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // OAuth2TokenService::Consumer implementation.
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 const std::string& access_token,
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 const base::Time& expiration_time) OVERRIDE;
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 const GoogleServiceAuthError& error) OVERRIDE;
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // net::URLFetcherDelegate implementation.
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) private:
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  struct DownloadState;
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef std::string AttachmentUrl;
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef base::ScopedPtrHashMap<AttachmentUrl, DownloadState> StateMap;
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef std::vector<DownloadState*> StateList;
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<net::URLFetcher> CreateFetcher(const AttachmentUrl& url,
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                            const std::string& access_token);
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void RequestAccessToken(DownloadState* download_state);
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void ReportResult(
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const DownloadState& download_state,
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const DownloadResult& result,
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const scoped_refptr<base::RefCountedString>& attachment_data);
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  GURL sync_service_url_;
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string account_id_;
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  OAuth2TokenService::ScopeSet oauth2_scopes_;
766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      token_service_provider_;
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<OAuth2TokenService::Request> access_token_request_;
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  StateMap state_map_;
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |requests_waiting_for_access_token_| only keeps references to DownloadState
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // objects while access token request is pending. It doesn't control objects'
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // lifetime.
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  StateList requests_waiting_for_access_token_;
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl);
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace syncer
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif  // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_
92