OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "chrome/browser/chromeos/policy/upload_job_impl.h" | 5 #include "chrome/browser/chromeos/policy/upload_job_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 | 44 |
45 UploadJobImpl::RandomMimeBoundaryGenerator::~RandomMimeBoundaryGenerator() { | 45 UploadJobImpl::RandomMimeBoundaryGenerator::~RandomMimeBoundaryGenerator() { |
46 } | 46 } |
47 | 47 |
48 // multipart/form-data POST request to upload the data. A DataSegment | 48 // multipart/form-data POST request to upload the data. A DataSegment |
49 // corresponds to one "Content-Disposition" in the "multipart" request. | 49 // corresponds to one "Content-Disposition" in the "multipart" request. |
50 class DataSegment { | 50 class DataSegment { |
51 public: | 51 public: |
52 DataSegment(const std::string& name, | 52 DataSegment(const std::string& name, |
53 const std::string& filename, | 53 const std::string& filename, |
54 scoped_ptr<std::string> data, | 54 std::unique_ptr<std::string> data, |
55 const std::map<std::string, std::string>& header_entries); | 55 const std::map<std::string, std::string>& header_entries); |
56 | 56 |
57 // Returns the header entries for this DataSegment. | 57 // Returns the header entries for this DataSegment. |
58 const std::map<std::string, std::string>& GetHeaderEntries() const; | 58 const std::map<std::string, std::string>& GetHeaderEntries() const; |
59 | 59 |
60 // Returns the string that will be assigned to the |name| field in the header. | 60 // Returns the string that will be assigned to the |name| field in the header. |
61 // |name| must be unique throughout the multipart message. This is enforced in | 61 // |name| must be unique throughout the multipart message. This is enforced in |
62 // SetUpMultipart(). | 62 // SetUpMultipart(). |
63 const std::string& GetName() const; | 63 const std::string& GetName() const; |
64 | 64 |
65 // Returns the string that will be assigned to the |filename| field in the | 65 // Returns the string that will be assigned to the |filename| field in the |
66 // header. If the |filename| is the empty string, the header field will be | 66 // header. If the |filename| is the empty string, the header field will be |
67 // omitted. | 67 // omitted. |
68 const std::string& GetFilename() const; | 68 const std::string& GetFilename() const; |
69 | 69 |
70 // Returns the data contained in this DataSegment. Ownership is passed. | 70 // Returns the data contained in this DataSegment. Ownership is passed. |
71 scoped_ptr<std::string> GetData(); | 71 std::unique_ptr<std::string> GetData(); |
72 | 72 |
73 // Returns the size in bytes of the blob in |data_|. | 73 // Returns the size in bytes of the blob in |data_|. |
74 size_t GetDataSize() const; | 74 size_t GetDataSize() const; |
75 | 75 |
76 private: | 76 private: |
77 const std::string name_; | 77 const std::string name_; |
78 const std::string filename_; | 78 const std::string filename_; |
79 scoped_ptr<std::string> data_; | 79 std::unique_ptr<std::string> data_; |
80 std::map<std::string, std::string> header_entries_; | 80 std::map<std::string, std::string> header_entries_; |
81 | 81 |
82 DISALLOW_COPY_AND_ASSIGN(DataSegment); | 82 DISALLOW_COPY_AND_ASSIGN(DataSegment); |
83 }; | 83 }; |
84 | 84 |
85 DataSegment::DataSegment( | 85 DataSegment::DataSegment( |
86 const std::string& name, | 86 const std::string& name, |
87 const std::string& filename, | 87 const std::string& filename, |
88 scoped_ptr<std::string> data, | 88 std::unique_ptr<std::string> data, |
89 const std::map<std::string, std::string>& header_entries) | 89 const std::map<std::string, std::string>& header_entries) |
90 : name_(name), | 90 : name_(name), |
91 filename_(filename), | 91 filename_(filename), |
92 data_(std::move(data)), | 92 data_(std::move(data)), |
93 header_entries_(header_entries) { | 93 header_entries_(header_entries) { |
94 DCHECK(data_); | 94 DCHECK(data_); |
95 } | 95 } |
96 | 96 |
97 const std::map<std::string, std::string>& DataSegment::GetHeaderEntries() | 97 const std::map<std::string, std::string>& DataSegment::GetHeaderEntries() |
98 const { | 98 const { |
99 return header_entries_; | 99 return header_entries_; |
100 } | 100 } |
101 | 101 |
102 const std::string& DataSegment::GetName() const { | 102 const std::string& DataSegment::GetName() const { |
103 return name_; | 103 return name_; |
104 } | 104 } |
105 | 105 |
106 const std::string& DataSegment::GetFilename() const { | 106 const std::string& DataSegment::GetFilename() const { |
107 return filename_; | 107 return filename_; |
108 } | 108 } |
109 | 109 |
110 scoped_ptr<std::string> DataSegment::GetData() { | 110 std::unique_ptr<std::string> DataSegment::GetData() { |
111 return std::move(data_); | 111 return std::move(data_); |
112 } | 112 } |
113 | 113 |
114 size_t DataSegment::GetDataSize() const { | 114 size_t DataSegment::GetDataSize() const { |
115 DCHECK(data_); | 115 DCHECK(data_); |
116 return data_->size(); | 116 return data_->size(); |
117 } | 117 } |
118 | 118 |
119 std::string UploadJobImpl::RandomMimeBoundaryGenerator::GenerateBoundary() | 119 std::string UploadJobImpl::RandomMimeBoundaryGenerator::GenerateBoundary() |
120 const { | 120 const { |
121 return net::GenerateMimeMultipartBoundary(); | 121 return net::GenerateMimeMultipartBoundary(); |
122 } | 122 } |
123 | 123 |
124 UploadJobImpl::UploadJobImpl( | 124 UploadJobImpl::UploadJobImpl( |
125 const GURL& upload_url, | 125 const GURL& upload_url, |
126 const std::string& account_id, | 126 const std::string& account_id, |
127 OAuth2TokenService* token_service, | 127 OAuth2TokenService* token_service, |
128 scoped_refptr<net::URLRequestContextGetter> url_context_getter, | 128 scoped_refptr<net::URLRequestContextGetter> url_context_getter, |
129 Delegate* delegate, | 129 Delegate* delegate, |
130 scoped_ptr<MimeBoundaryGenerator> boundary_generator) | 130 std::unique_ptr<MimeBoundaryGenerator> boundary_generator) |
131 : OAuth2TokenService::Consumer("cros_upload_job"), | 131 : OAuth2TokenService::Consumer("cros_upload_job"), |
132 upload_url_(upload_url), | 132 upload_url_(upload_url), |
133 account_id_(account_id), | 133 account_id_(account_id), |
134 token_service_(token_service), | 134 token_service_(token_service), |
135 url_context_getter_(url_context_getter), | 135 url_context_getter_(url_context_getter), |
136 delegate_(delegate), | 136 delegate_(delegate), |
137 boundary_generator_(std::move(boundary_generator)), | 137 boundary_generator_(std::move(boundary_generator)), |
138 state_(IDLE), | 138 state_(IDLE), |
139 retry_(0) { | 139 retry_(0) { |
140 DCHECK(token_service_); | 140 DCHECK(token_service_); |
141 DCHECK(url_context_getter_); | 141 DCHECK(url_context_getter_); |
142 DCHECK(delegate_); | 142 DCHECK(delegate_); |
143 if (!upload_url_.is_valid()) { | 143 if (!upload_url_.is_valid()) { |
144 state_ = ERROR; | 144 state_ = ERROR; |
145 NOTREACHED() << upload_url_ << " is not a valid URL."; | 145 NOTREACHED() << upload_url_ << " is not a valid URL."; |
146 } | 146 } |
147 } | 147 } |
148 | 148 |
149 UploadJobImpl::~UploadJobImpl() { | 149 UploadJobImpl::~UploadJobImpl() { |
150 } | 150 } |
151 | 151 |
152 void UploadJobImpl::AddDataSegment( | 152 void UploadJobImpl::AddDataSegment( |
153 const std::string& name, | 153 const std::string& name, |
154 const std::string& filename, | 154 const std::string& filename, |
155 const std::map<std::string, std::string>& header_entries, | 155 const std::map<std::string, std::string>& header_entries, |
156 scoped_ptr<std::string> data) { | 156 std::unique_ptr<std::string> data) { |
157 // Cannot add data to busy or failed instance. | 157 // Cannot add data to busy or failed instance. |
158 DCHECK_EQ(IDLE, state_); | 158 DCHECK_EQ(IDLE, state_); |
159 if (state_ != IDLE) | 159 if (state_ != IDLE) |
160 return; | 160 return; |
161 | 161 |
162 scoped_ptr<DataSegment> data_segment( | 162 std::unique_ptr<DataSegment> data_segment( |
163 new DataSegment(name, filename, std::move(data), header_entries)); | 163 new DataSegment(name, filename, std::move(data), header_entries)); |
164 data_segments_.push_back(std::move(data_segment)); | 164 data_segments_.push_back(std::move(data_segment)); |
165 } | 165 } |
166 | 166 |
167 void UploadJobImpl::Start() { | 167 void UploadJobImpl::Start() { |
168 // Cannot start an upload on a busy or failed instance. | 168 // Cannot start an upload on a busy or failed instance. |
169 DCHECK_EQ(IDLE, state_); | 169 DCHECK_EQ(IDLE, state_); |
170 if (state_ != IDLE) | 170 if (state_ != IDLE) |
171 return; | 171 return; |
172 RequestAccessToken(); | 172 RequestAccessToken(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 data_segment->GetName() + "\""); | 222 data_segment->GetName() + "\""); |
223 if (!data_segment->GetFilename().empty()) { | 223 if (!data_segment->GetFilename().empty()) { |
224 post_data_->append("; filename=\"" + data_segment->GetFilename() + "\""); | 224 post_data_->append("; filename=\"" + data_segment->GetFilename() + "\""); |
225 } | 225 } |
226 post_data_->append("\r\n"); | 226 post_data_->append("\r\n"); |
227 | 227 |
228 // Add custom header fields. | 228 // Add custom header fields. |
229 for (const auto& entry : data_segment->GetHeaderEntries()) { | 229 for (const auto& entry : data_segment->GetHeaderEntries()) { |
230 post_data_->append(entry.first + ": " + entry.second + "\r\n"); | 230 post_data_->append(entry.first + ": " + entry.second + "\r\n"); |
231 } | 231 } |
232 scoped_ptr<std::string> data = data_segment->GetData(); | 232 std::unique_ptr<std::string> data = data_segment->GetData(); |
233 post_data_->append("\r\n" + *data + "\r\n"); | 233 post_data_->append("\r\n" + *data + "\r\n"); |
234 } | 234 } |
235 post_data_->append("--" + *mime_boundary_.get() + "--\r\n"); | 235 post_data_->append("--" + *mime_boundary_.get() + "--\r\n"); |
236 | 236 |
237 // Issues a warning if our buffer size estimate was too small. | 237 // Issues a warning if our buffer size estimate was too small. |
238 if (post_data_->size() > size) { | 238 if (post_data_->size() > size) { |
239 LOG(WARNING) | 239 LOG(WARNING) |
240 << "Reallocation needed in POST data buffer. Expected maximum size " | 240 << "Reallocation needed in POST data buffer. Expected maximum size " |
241 << size << " bytes, actual size " << post_data_->size() << " bytes."; | 241 << size << " bytes, actual size " << post_data_->size() << " bytes."; |
242 } | 242 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 if (success) { | 342 if (success) { |
343 state_ = SUCCESS; | 343 state_ = SUCCESS; |
344 delegate_->OnSuccess(); | 344 delegate_->OnSuccess(); |
345 } else { | 345 } else { |
346 state_ = ERROR; | 346 state_ = ERROR; |
347 delegate_->OnFailure(SERVER_ERROR); | 347 delegate_->OnFailure(SERVER_ERROR); |
348 } | 348 } |
349 } | 349 } |
350 | 350 |
351 } // namespace policy | 351 } // namespace policy |
OLD | NEW |