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