OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ |
6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ |
7 | 7 |
8 #include "base/containers/scoped_ptr_hash_map.h" | 8 #include "base/containers/scoped_ptr_hash_map.h" |
9 #include "base/threading/non_thread_safe.h" | 9 #include "base/threading/non_thread_safe.h" |
10 #include "google_apis/gaia/oauth2_token_service_request.h" | 10 #include "google_apis/gaia/oauth2_token_service_request.h" |
11 #include "net/url_request/url_fetcher_delegate.h" | 11 #include "net/url_request/url_fetcher_delegate.h" |
12 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
13 #include "sync/api/attachments/attachment_downloader.h" | 13 #include "sync/api/attachments/attachment_downloader.h" |
| 14 #include "url/gurl.h" |
14 | 15 |
15 namespace syncer { | 16 namespace syncer { |
16 | 17 |
17 // An implementation of AttachmentDownloader. | 18 // An implementation of AttachmentDownloader. |
18 class AttachmentDownloaderImpl : public AttachmentDownloader, | 19 class AttachmentDownloaderImpl : public AttachmentDownloader, |
19 public OAuth2TokenService::Consumer, | 20 public OAuth2TokenService::Consumer, |
20 public net::URLFetcherDelegate, | 21 public net::URLFetcherDelegate, |
21 public base::NonThreadSafe { | 22 public base::NonThreadSafe { |
22 public: | 23 public: |
23 // |url_prefix| is the URL prefix (including trailing slash) to be used when | 24 // |sync_service_url| is the URL of the sync service. |
24 // downloading attachments. | |
25 // | 25 // |
26 // |url_request_context_getter| provides a URLRequestContext. | 26 // |url_request_context_getter| provides a URLRequestContext. |
27 // | 27 // |
28 // |account_id| is the account id to use for downloads. | 28 // |account_id| is the account id to use for downloads. |
29 // | 29 // |
30 // |scopes| is the set of scopes to use for downloads. | 30 // |scopes| is the set of scopes to use for downloads. |
31 // | 31 // |
32 // |token_service_provider| provides an OAuth2 token service. | 32 // |token_service_provider| provides an OAuth2 token service. |
33 AttachmentDownloaderImpl( | 33 AttachmentDownloaderImpl( |
34 const std::string& url_prefix, | 34 const GURL& sync_service_url, |
35 const scoped_refptr<net::URLRequestContextGetter>& | 35 const scoped_refptr<net::URLRequestContextGetter>& |
36 url_request_context_getter, | 36 url_request_context_getter, |
37 const std::string& account_id, | 37 const std::string& account_id, |
38 const OAuth2TokenService::ScopeSet& scopes, | 38 const OAuth2TokenService::ScopeSet& scopes, |
39 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> | 39 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> |
40 token_service_provider); | 40 token_service_provider); |
41 virtual ~AttachmentDownloaderImpl(); | 41 virtual ~AttachmentDownloaderImpl(); |
42 | 42 |
43 // AttachmentDownloader implementation. | 43 // AttachmentDownloader implementation. |
44 virtual void DownloadAttachment(const AttachmentId& attachment_id, | 44 virtual void DownloadAttachment(const AttachmentId& attachment_id, |
45 const DownloadCallback& callback) OVERRIDE; | 45 const DownloadCallback& callback) OVERRIDE; |
46 | 46 |
47 // OAuth2TokenService::Consumer implementation. | 47 // OAuth2TokenService::Consumer implementation. |
48 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 48 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
49 const std::string& access_token, | 49 const std::string& access_token, |
50 const base::Time& expiration_time) OVERRIDE; | 50 const base::Time& expiration_time) OVERRIDE; |
51 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 51 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
52 const GoogleServiceAuthError& error) OVERRIDE; | 52 const GoogleServiceAuthError& error) OVERRIDE; |
53 | 53 |
54 // net::URLFetcherDelegate implementation. | 54 // net::URLFetcherDelegate implementation. |
55 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 55 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
56 | 56 |
57 private: | 57 private: |
58 struct DownloadState; | 58 struct DownloadState; |
59 typedef std::string AttachmentUrl; | 59 typedef std::string AttachmentUrl; |
60 typedef base::ScopedPtrHashMap<AttachmentUrl, DownloadState> StateMap; | 60 typedef base::ScopedPtrHashMap<AttachmentUrl, DownloadState> StateMap; |
61 typedef std::vector<DownloadState*> StateList; | 61 typedef std::vector<DownloadState*> StateList; |
62 | 62 |
63 AttachmentUrl GetAttachmentUrl(const AttachmentId& attachment_id); | |
64 scoped_ptr<net::URLFetcher> CreateFetcher(const AttachmentUrl& url, | 63 scoped_ptr<net::URLFetcher> CreateFetcher(const AttachmentUrl& url, |
65 const std::string& access_token); | 64 const std::string& access_token); |
66 void RequestAccessToken(DownloadState* download_state); | 65 void RequestAccessToken(DownloadState* download_state); |
67 void ReportResult( | 66 void ReportResult( |
68 const DownloadState& download_state, | 67 const DownloadState& download_state, |
69 const DownloadResult& result, | 68 const DownloadResult& result, |
70 const scoped_refptr<base::RefCountedString>& attachment_data); | 69 const scoped_refptr<base::RefCountedString>& attachment_data); |
71 | 70 |
72 std::string url_prefix_; | 71 GURL sync_service_url_; |
73 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 72 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
74 | 73 |
75 std::string account_id_; | 74 std::string account_id_; |
76 OAuth2TokenService::ScopeSet oauth2_scopes_; | 75 OAuth2TokenService::ScopeSet oauth2_scopes_; |
77 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> | 76 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> |
78 token_service_provider_; | 77 token_service_provider_; |
79 scoped_ptr<OAuth2TokenService::Request> access_token_request_; | 78 scoped_ptr<OAuth2TokenService::Request> access_token_request_; |
80 | 79 |
81 StateMap state_map_; | 80 StateMap state_map_; |
82 // |requests_waiting_for_access_token_| only keeps references to DownloadState | 81 // |requests_waiting_for_access_token_| only keeps references to DownloadState |
83 // objects while access token request is pending. It doesn't control objects' | 82 // objects while access token request is pending. It doesn't control objects' |
84 // lifetime. | 83 // lifetime. |
85 StateList requests_waiting_for_access_token_; | 84 StateList requests_waiting_for_access_token_; |
86 | 85 |
87 DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl); | 86 DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl); |
88 }; | 87 }; |
89 | 88 |
90 } // namespace syncer | 89 } // namespace syncer |
91 | 90 |
92 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | 91 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ |
OLD | NEW |