Fix the duplicatedly commited composition text when switching IME.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / upload_job_impl.h
blobea94e58c3e5263ce2c53592fcdf92f27731fc24f
1 // Copyright 2015 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 CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "chrome/browser/chromeos/policy/upload_job.h"
17 #include "google_apis/gaia/oauth2_token_service.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "url/gurl.h"
23 namespace policy {
25 // This implementation of UploadJob uses the OAuth2TokenService to acquire
26 // access tokens for the device management (cloud-based policy) server scope and
27 // uses a URLFetcher to upload data to the specified upload url.
28 class UploadJobImpl : public UploadJob,
29 public OAuth2TokenService::Consumer,
30 public net::URLFetcherDelegate {
31 public:
32 // UploadJobImpl uses a MimeBoundaryGenerator to generate strings which
33 // mark the boundaries between data segments.
34 class MimeBoundaryGenerator {
35 public:
36 virtual ~MimeBoundaryGenerator();
38 virtual std::string GenerateBoundary(size_t length) const = 0;
40 private:
41 DISALLOW_ASSIGN(MimeBoundaryGenerator);
44 // An implemenation of the MimeBoundaryGenerator which uses random
45 // alpha-numeric characters to construct MIME boundaries.
46 class RandomMimeBoundaryGenerator : public MimeBoundaryGenerator {
47 public:
48 ~RandomMimeBoundaryGenerator() override;
50 std::string GenerateBoundary(
51 size_t length) const override; // MimeBoundaryGenerator
54 UploadJobImpl(const GURL& upload_url,
55 const std::string& account_id,
56 OAuth2TokenService* token_service,
57 scoped_refptr<net::URLRequestContextGetter> url_context_getter,
58 Delegate* delegate,
59 scoped_ptr<MimeBoundaryGenerator> boundary_generator);
60 ~UploadJobImpl() override;
62 // UploadJob:
63 void AddDataSegment(const std::string& name,
64 const std::string& filename,
65 const std::map<std::string, std::string>& header_entries,
66 scoped_ptr<std::string> data) override;
67 void Start() override;
69 private:
70 // Indicates the current state of the UploadJobImpl.
71 enum State {
72 IDLE, // Start() has not been called.
73 ACQUIRING_TOKEN, // Trying to acquire the access token.
74 PREPARING_CONTENT, // Currently encoding the content.
75 UPLOADING, // Upload started.
76 SUCCESS, // Upload successfully completed.
77 ERROR // Upload failed.
80 // OAuth2TokenService::Consumer:
81 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
82 const std::string& access_token,
83 const base::Time& expiration_time) override;
84 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
85 const GoogleServiceAuthError& error) override;
87 // net::URLFetcherDelegate:
88 void OnURLFetchComplete(const net::URLFetcher* source) override;
90 // Requests an access token for the upload scope.
91 void RequestAccessToken();
93 // Dispatches POST request to URLFetcher.
94 void StartUpload(const std::string& access_token);
96 // Constructs the body of the POST request by concatenating the
97 // |data_segments_|, separated by appropriate content-disposition headers and
98 // a MIME boundary. Places the request body in |post_data_| and a copy of the
99 // MIME boundary in |mime_boundary_|. Returns true on success. If |post_data_|
100 // and |mime_boundary_| were set already, returns true immediately. In case of
101 // an error, clears |post_data_| and |mime_boundary_| and returns false.
102 bool SetUpMultipart();
104 // Assembles the request and starts the URLFetcher. Fails if another upload
105 // is still in progress or the content was not successfully encoded.
106 void CreateAndStartURLFetcher(const std::string& access_token);
108 // The URL to which the POST request should be directed.
109 const GURL upload_url_;
111 // The account ID that will be used for the access token fetch.
112 const std::string account_id_;
114 // The token service used to retrieve the access token.
115 OAuth2TokenService* const token_service_;
117 // This is used to initialize the net::URLFetcher object.
118 const scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
120 // The delegate to be notified of events.
121 Delegate* const delegate_;
123 // An implementation of MimeBoundaryGenerator. This instance will be used to
124 // generate MIME boundaries when assembling the multipart request in
125 // SetUpMultipart().
126 scoped_ptr<MimeBoundaryGenerator> boundary_generator_;
128 // Current state the uploader is in.
129 State state_;
131 // Contains the cached MIME boundary.
132 scoped_ptr<std::string> mime_boundary_;
134 // Contains the cached, encoded post data.
135 scoped_ptr<std::string> post_data_;
137 // Keeps track of the number of retries.
138 int retry_;
140 // The OAuth request to receive the access token.
141 scoped_ptr<OAuth2TokenService::Request> access_token_request_;
143 // The OAuth access token.
144 std::string access_token_;
146 // Helper to upload the data.
147 scoped_ptr<net::URLFetcher> upload_fetcher_;
149 // The data chunks to be uploaded.
150 ScopedVector<DataSegment> data_segments_;
152 DISALLOW_COPY_AND_ASSIGN(UploadJobImpl);
155 } // namespace policy
157 #endif // CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_