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.
5 #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_UPLOADER_IMPL_H_
6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_UPLOADER_IMPL_H_
8 #include "base/containers/scoped_ptr_hash_map.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/threading/non_thread_safe.h"
11 #include "google_apis/gaia/oauth2_token_service_request.h"
12 #include "net/url_request/url_request_context_getter.h"
13 #include "sync/internal_api/public/attachments/attachment_uploader.h"
14 #include "sync/internal_api/public/base/model_type.h"
19 class URLRequestContextGetter
;
24 // An implementation of AttachmentUploader.
25 class SYNC_EXPORT AttachmentUploaderImpl
: public AttachmentUploader
,
26 public base::NonThreadSafe
{
28 // |sync_service_url| is the URL of the sync service.
30 // |url_request_context_getter| provides a URLRequestContext.
32 // |account_id| is the account id to use for uploads.
34 // |scopes| is the set of scopes to use for uploads.
36 // |token_service_provider| provides an OAuth2 token service.
38 // |store_birthday| is the raw, sync store birthday.
40 // |model_type| is the model type this uploader is used with.
41 AttachmentUploaderImpl(
42 const GURL
& sync_service_url
,
43 const scoped_refptr
<net::URLRequestContextGetter
>&
44 url_request_context_getter
,
45 const std::string
& account_id
,
46 const OAuth2TokenService::ScopeSet
& scopes
,
47 const scoped_refptr
<OAuth2TokenServiceRequest::TokenServiceProvider
>&
48 token_service_provider
,
49 const std::string
& store_birthday
,
50 ModelType model_type
);
51 ~AttachmentUploaderImpl() override
;
53 // AttachmentUploader implementation.
54 void UploadAttachment(const Attachment
& attachment
,
55 const UploadCallback
& callback
) override
;
57 // Return the URL for the given |sync_service_url| and |attachment_id|.
58 static GURL
GetURLForAttachmentId(const GURL
& sync_service_url
,
59 const AttachmentId
& attachment_id
);
61 // Format crc32c to pass into X-Goog-Hash header.
63 // The value is base64 encoded, big-endian format. Suitable for use in the
65 // (https://cloud.google.com/storage/docs/reference-headers#xgooghash).
66 static std::string
FormatCrc32cHash(uint32_t crc32c
);
68 // Apply common settings to |fetcher|, suitable for both uploads and
70 static void ConfigureURLFetcherCommon(
71 net::URLFetcher
* fetcher
,
72 const std::string
& auth_token
,
73 const std::string
& raw_store_birthday
,
75 net::URLRequestContextGetter
* request_context_getter
);
79 typedef std::string UniqueId
;
80 typedef base::ScopedPtrHashMap
<UniqueId
, scoped_ptr
<UploadState
>> StateMap
;
82 void OnUploadStateStopped(const UniqueId
& unique_id
);
84 // Encodes |input| into |output| using URL safe base64, no padding.
85 // NOTE: Safe to use the same variable for both |input| and |output|.
86 static void Base64URLSafeEncode(const std::string
& input
,
89 GURL sync_service_url_
;
90 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
91 std::string account_id_
;
92 OAuth2TokenService::ScopeSet scopes_
;
93 scoped_refptr
<OAuth2TokenServiceRequest::TokenServiceProvider
>
94 token_service_provider_
;
95 std::string raw_store_birthday_
;
97 ModelType model_type_
;
99 // Must be last data member.
100 base::WeakPtrFactory
<AttachmentUploaderImpl
> weak_ptr_factory_
;
102 DISALLOW_COPY_AND_ASSIGN(AttachmentUploaderImpl
);
105 } // namespace syncer
107 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_UPLOADER_IMPL_H_