Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / public / attachments / attachment_uploader_impl.h
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 SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_UPLOADER_IMPL_H_
6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_UPLOADER_IMPL_H_
7
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
15 class GURL;
16
17 namespace net {
18 class URLRequestContextGetter;
19 }  // namespace net
20
21 namespace syncer {
22
23 // An implementation of AttachmentUploader.
24 class SYNC_EXPORT AttachmentUploaderImpl : public AttachmentUploader,
25                                            public base::NonThreadSafe {
26  public:
27   // |sync_service_url| is the URL of the sync service.
28   //
29   // |url_request_context_getter| provides a URLRequestContext.
30   //
31   // |account_id| is the account id to use for uploads.
32   //
33   // |scopes| is the set of scopes to use for uploads.
34   //
35   // |token_service_provider| provides an OAuth2 token service.
36   AttachmentUploaderImpl(
37       const GURL& sync_service_url,
38       const scoped_refptr<net::URLRequestContextGetter>&
39           url_request_context_getter,
40       const std::string& account_id,
41       const OAuth2TokenService::ScopeSet& scopes,
42       const scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>&
43           token_service_provider);
44   ~AttachmentUploaderImpl() override;
45
46   // AttachmentUploader implementation.
47   void UploadAttachment(const Attachment& attachment,
48                         const UploadCallback& callback) override;
49
50   // Return the URL for the given |sync_service_url| and |attachment_id|.
51   static GURL GetURLForAttachmentId(const GURL& sync_service_url,
52                                     const AttachmentId& attachment_id);
53
54   // Return the crc32c of the memory described by |data| and |size|.
55   //
56   // The value is base64 encoded, big-endian format.  Suitable for use in the
57   // X-Goog-Hash header
58   // (https://cloud.google.com/storage/docs/reference-headers#xgooghash).
59   //
60   // Potentially expensive.
61   static std::string ComputeCrc32cHash(const char* data, size_t size);
62
63  private:
64   class UploadState;
65   typedef std::string UniqueId;
66   typedef base::ScopedPtrHashMap<UniqueId, UploadState> StateMap;
67
68   void OnUploadStateStopped(const UniqueId& unique_id);
69
70   GURL sync_service_url_;
71   scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
72   std::string account_id_;
73   OAuth2TokenService::ScopeSet scopes_;
74   scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>
75       token_service_provider_;
76   StateMap state_map_;
77
78   // Must be last data member.
79   base::WeakPtrFactory<AttachmentUploaderImpl> weak_ptr_factory_;
80
81   DISALLOW_COPY_AND_ASSIGN(AttachmentUploaderImpl);
82 };
83
84 }  // namespace syncer
85
86 #endif  // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_UPLOADER_IMPL_H_