Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_ | 6 #define CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
|
Andrew T Wilson (Slow)
2016/05/10 22:00:42
#include "base/threading/thread_checker.h" (or wha
Marton Hunyady
2016/05/11 12:55:30
Done.
| |
| 14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
| 15 #include "chrome/browser/chromeos/policy/upload_job.h" | 15 #include "chrome/browser/chromeos/policy/upload_job.h" |
| 16 #include "google_apis/gaia/oauth2_token_service.h" | 16 #include "google_apis/gaia/oauth2_token_service.h" |
| 17 #include "net/url_request/url_fetcher.h" | 17 #include "net/url_request/url_fetcher.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 namespace policy { | 22 namespace policy { |
| 23 | 23 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 42 | 42 |
| 43 // An implemenation of the MimeBoundaryGenerator which uses random | 43 // An implemenation of the MimeBoundaryGenerator which uses random |
| 44 // alpha-numeric characters to construct MIME boundaries. | 44 // alpha-numeric characters to construct MIME boundaries. |
| 45 class RandomMimeBoundaryGenerator : public MimeBoundaryGenerator { | 45 class RandomMimeBoundaryGenerator : public MimeBoundaryGenerator { |
| 46 public: | 46 public: |
| 47 ~RandomMimeBoundaryGenerator() override; | 47 ~RandomMimeBoundaryGenerator() override; |
| 48 | 48 |
| 49 std::string GenerateBoundary() const override; // MimeBoundaryGenerator | 49 std::string GenerateBoundary() const override; // MimeBoundaryGenerator |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // |task_runner| must belong to the same thread from which the consturctor and | |
|
Andrew T Wilson (Slow)
2016/05/10 22:00:42
nit: constructor
Marton Hunyady
2016/05/11 12:55:30
Done.
| |
| 53 // all the public methods are called. | |
| 52 UploadJobImpl(const GURL& upload_url, | 54 UploadJobImpl(const GURL& upload_url, |
| 53 const std::string& account_id, | 55 const std::string& account_id, |
| 54 OAuth2TokenService* token_service, | 56 OAuth2TokenService* token_service, |
| 55 scoped_refptr<net::URLRequestContextGetter> url_context_getter, | 57 scoped_refptr<net::URLRequestContextGetter> url_context_getter, |
| 56 Delegate* delegate, | 58 Delegate* delegate, |
| 57 std::unique_ptr<MimeBoundaryGenerator> boundary_generator); | 59 std::unique_ptr<MimeBoundaryGenerator> boundary_generator, |
| 60 const scoped_refptr<base::SequencedTaskRunner> task_runner); | |
| 58 ~UploadJobImpl() override; | 61 ~UploadJobImpl() override; |
| 59 | 62 |
| 60 // UploadJob: | 63 // UploadJob: |
| 61 void AddDataSegment(const std::string& name, | 64 void AddDataSegment(const std::string& name, |
| 62 const std::string& filename, | 65 const std::string& filename, |
| 63 const std::map<std::string, std::string>& header_entries, | 66 const std::map<std::string, std::string>& header_entries, |
| 64 std::unique_ptr<std::string> data) override; | 67 std::unique_ptr<std::string> data) override; |
| 65 void Start() override; | 68 void Start() override; |
| 66 | 69 |
| 67 private: | 70 private: |
| 68 // Indicates the current state of the UploadJobImpl. | 71 // Indicates the current state of the UploadJobImpl. |
| 72 // State transitions for successful upload: | |
| 73 // IDLE -> ACQUIRING_TOKEN -> PREPARING_CONTENT -> UPLOADING -> SUCCESS | |
| 74 // If error happens, state goes back to ACQUIRING_TOKEN. | |
| 75 // State transitions when error occurs once: | |
| 76 // IDLE -> ACQUIRING_TOKEN -> PREPARING_CONTENT -> UPLOADING -> | |
| 77 // -> ACQUIRING_TOKEN -> PREPARING_CONTENT -> UPLOADING -> SUCCESS | |
| 78 // State transitions when tried unsuccessfully kMaxRetries times: | |
| 79 // ... -> PREPARING_CONTENT -> UPLOADING -> ERROR | |
| 69 enum State { | 80 enum State { |
| 70 IDLE, // Start() has not been called. | 81 IDLE, // Start() has not been called. |
| 71 ACQUIRING_TOKEN, // Trying to acquire the access token. | 82 ACQUIRING_TOKEN, // Trying to acquire the access token. |
| 72 PREPARING_CONTENT, // Currently encoding the content. | 83 PREPARING_CONTENT, // Currently encoding the content. |
| 73 UPLOADING, // Upload started. | 84 UPLOADING, // Upload started. |
| 74 SUCCESS, // Upload successfully completed. | 85 SUCCESS, // Upload successfully completed. |
| 75 ERROR // Upload failed. | 86 ERROR // Upload failed. |
| 76 }; | 87 }; |
| 77 | 88 |
| 78 // OAuth2TokenService::Consumer: | 89 // OAuth2TokenService::Consumer: |
| 79 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 90 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 80 const std::string& access_token, | 91 const std::string& access_token, |
| 81 const base::Time& expiration_time) override; | 92 const base::Time& expiration_time) override; |
| 82 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 93 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 83 const GoogleServiceAuthError& error) override; | 94 const GoogleServiceAuthError& error) override; |
| 84 | 95 |
| 85 // net::URLFetcherDelegate: | 96 // net::URLFetcherDelegate: |
| 86 void OnURLFetchComplete(const net::URLFetcher* source) override; | 97 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 87 | 98 |
| 99 void HandleError(ErrorCode errorCode); | |
| 100 | |
| 88 // Requests an access token for the upload scope. | 101 // Requests an access token for the upload scope. |
| 89 void RequestAccessToken(); | 102 void RequestAccessToken(); |
| 90 | 103 |
| 91 // Dispatches POST request to URLFetcher. | 104 // Dispatches POST request to URLFetcher. |
| 92 void StartUpload(const std::string& access_token); | 105 void StartUpload(); |
| 93 | 106 |
| 94 // Constructs the body of the POST request by concatenating the | 107 // Constructs the body of the POST request by concatenating the |
| 95 // |data_segments_|, separated by appropriate content-disposition headers and | 108 // |data_segments_|, separated by appropriate content-disposition headers and |
| 96 // a MIME boundary. Places the request body in |post_data_| and a copy of the | 109 // a MIME boundary. Places the request body in |post_data_| and a copy of the |
| 97 // MIME boundary in |mime_boundary_|. Returns true on success. If |post_data_| | 110 // MIME boundary in |mime_boundary_|. Returns true on success. If |post_data_| |
| 98 // and |mime_boundary_| were set already, returns true immediately. In case of | 111 // and |mime_boundary_| were set already, returns true immediately. In case of |
| 99 // an error, clears |post_data_| and |mime_boundary_| and returns false. | 112 // an error, clears |post_data_| and |mime_boundary_| and returns false. |
| 100 bool SetUpMultipart(); | 113 bool SetUpMultipart(); |
| 101 | 114 |
| 102 // Assembles the request and starts the URLFetcher. Fails if another upload | 115 // Assembles the request and starts the URLFetcher. Fails if another upload |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 | 153 |
| 141 // The OAuth access token. | 154 // The OAuth access token. |
| 142 std::string access_token_; | 155 std::string access_token_; |
| 143 | 156 |
| 144 // Helper to upload the data. | 157 // Helper to upload the data. |
| 145 std::unique_ptr<net::URLFetcher> upload_fetcher_; | 158 std::unique_ptr<net::URLFetcher> upload_fetcher_; |
| 146 | 159 |
| 147 // The data chunks to be uploaded. | 160 // The data chunks to be uploaded. |
| 148 ScopedVector<DataSegment> data_segments_; | 161 ScopedVector<DataSegment> data_segments_; |
| 149 | 162 |
| 163 // TaskRunner used for scheduling retry attempts. | |
| 164 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 165 | |
| 166 base::ThreadChecker thread_checker_; | |
| 167 | |
| 168 // Should remain the last member so it will be destroyed first and | |
| 169 // invalidate all weak pointers. | |
| 170 base::WeakPtrFactory<UploadJobImpl> weak_factory_; | |
| 171 | |
| 150 DISALLOW_COPY_AND_ASSIGN(UploadJobImpl); | 172 DISALLOW_COPY_AND_ASSIGN(UploadJobImpl); |
| 151 }; | 173 }; |
| 152 | 174 |
| 153 } // namespace policy | 175 } // namespace policy |
| 154 | 176 |
| 155 #endif // CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_ | 177 #endif // CHROME_BROWSER_CHROMEOS_POLICY_UPLOAD_JOB_IMPL_H_ |
| OLD | NEW |