OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chrome_to_mobile_service.h" | 5 #include "chrome/browser/chrome_to_mobile_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // Call this as a BlockingPoolTask to avoid the FILE thread. | 128 // Call this as a BlockingPoolTask to avoid the FILE thread. |
129 void CreateSnapshotFile(CreateSnapshotFileCallback callback) { | 129 void CreateSnapshotFile(CreateSnapshotFileCallback callback) { |
130 FilePath snapshot; | 130 FilePath snapshot; |
131 bool success = file_util::CreateTemporaryFile(&snapshot); | 131 bool success = file_util::CreateTemporaryFile(&snapshot); |
132 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 132 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
133 base::Bind(callback, snapshot, success)); | 133 base::Bind(callback, snapshot, success)); |
134 } | 134 } |
135 | 135 |
136 // Send snapshot file contents as POST data in a job submit request. | 136 // Send snapshot file contents as POST data in a job submit request. |
137 // Call this as a BlockingPoolSequencedTask (before posting DeleteSnapshotFile). | 137 // Call this as a BlockingPoolSequencedTask (before posting DeleteSnapshotFile). |
138 void SubmitSnapshotFile(content::URLFetcher* request, | 138 void SubmitSnapshotFile(net::URLFetcher* request, |
139 const ChromeToMobileService::RequestData& data) { | 139 const ChromeToMobileService::RequestData& data) { |
140 std::string file; | 140 std::string file; |
141 if (file_util::ReadFileToString(data.snapshot_path, &file) && !file.empty()) { | 141 if (file_util::ReadFileToString(data.snapshot_path, &file) && !file.empty()) { |
142 std::string post_data, mime_boundary; | 142 std::string post_data, mime_boundary; |
143 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); | 143 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); |
144 cloud_print::AddMultipartValueForUpload("printerid", | 144 cloud_print::AddMultipartValueForUpload("printerid", |
145 UTF16ToUTF8(data.mobile_id), mime_boundary, std::string(), &post_data); | 145 UTF16ToUTF8(data.mobile_id), mime_boundary, std::string(), &post_data); |
146 cloud_print::AddMultipartValueForUpload("tag", GetJobString(data), | 146 cloud_print::AddMultipartValueForUpload("tag", GetJobString(data), |
147 mime_boundary, std::string(), &post_data); | 147 mime_boundary, std::string(), &post_data); |
148 cloud_print::AddMultipartValueForUpload("title", UTF16ToUTF8(data.title), | 148 cloud_print::AddMultipartValueForUpload("title", UTF16ToUTF8(data.title), |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 data.mobile_id = mobile_id; | 246 data.mobile_id = mobile_id; |
247 content::WebContents* web_contents = | 247 content::WebContents* web_contents = |
248 browser::FindLastActiveWithProfile(profile_)->GetSelectedWebContents(); | 248 browser::FindLastActiveWithProfile(profile_)->GetSelectedWebContents(); |
249 data.url = web_contents->GetURL(); | 249 data.url = web_contents->GetURL(); |
250 data.title = web_contents->GetTitle(); | 250 data.title = web_contents->GetTitle(); |
251 data.snapshot_path = snapshot; | 251 data.snapshot_path = snapshot; |
252 bool send_snapshot = !snapshot.empty(); | 252 bool send_snapshot = !snapshot.empty(); |
253 data.snapshot_id = send_snapshot ? guid::GenerateGUID() : std::string(); | 253 data.snapshot_id = send_snapshot ? guid::GenerateGUID() : std::string(); |
254 data.type = send_snapshot ? DELAYED_SNAPSHOT : URL; | 254 data.type = send_snapshot ? DELAYED_SNAPSHOT : URL; |
255 | 255 |
256 content::URLFetcher* submit_url = CreateRequest(data); | 256 net::URLFetcher* submit_url = CreateRequest(data); |
257 request_observer_map_[submit_url] = observer; | 257 request_observer_map_[submit_url] = observer; |
258 submit_url->Start(); | 258 submit_url->Start(); |
259 | 259 |
260 if (send_snapshot) { | 260 if (send_snapshot) { |
261 LogMetric(SENDING_SNAPSHOT); | 261 LogMetric(SENDING_SNAPSHOT); |
262 | 262 |
263 data.type = SNAPSHOT; | 263 data.type = SNAPSHOT; |
264 content::URLFetcher* submit_snapshot = CreateRequest(data); | 264 net::URLFetcher* submit_snapshot = CreateRequest(data); |
265 request_observer_map_[submit_snapshot] = observer; | 265 request_observer_map_[submit_snapshot] = observer; |
266 content::BrowserThread::PostBlockingPoolSequencedTask( | 266 content::BrowserThread::PostBlockingPoolSequencedTask( |
267 data.snapshot_path.AsUTF8Unsafe(), FROM_HERE, | 267 data.snapshot_path.AsUTF8Unsafe(), FROM_HERE, |
268 base::Bind(&SubmitSnapshotFile, submit_snapshot, data)); | 268 base::Bind(&SubmitSnapshotFile, submit_snapshot, data)); |
269 } | 269 } |
270 } | 270 } |
271 | 271 |
272 void ChromeToMobileService::DeleteSnapshot(const FilePath& snapshot) { | 272 void ChromeToMobileService::DeleteSnapshot(const FilePath& snapshot) { |
273 DCHECK(snapshot.empty() || snapshots_.find(snapshot) != snapshots_.end()); | 273 DCHECK(snapshot.empty() || snapshots_.find(snapshot) != snapshots_.end()); |
274 if (snapshots_.find(snapshot) != snapshots_.end()) { | 274 if (snapshots_.find(snapshot) != snapshots_.end()) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 if (success && browser && browser->GetSelectedWebContents()) { | 335 if (success && browser && browser->GetSelectedWebContents()) { |
336 // Generate the snapshot and have the observer be called back on completion. | 336 // Generate the snapshot and have the observer be called back on completion. |
337 browser->GetSelectedWebContents()->GenerateMHTML(path, | 337 browser->GetSelectedWebContents()->GenerateMHTML(path, |
338 base::Bind(&Observer::SnapshotGenerated, observer)); | 338 base::Bind(&Observer::SnapshotGenerated, observer)); |
339 } else if (observer.get()) { | 339 } else if (observer.get()) { |
340 // Signal snapshot generation failure. | 340 // Signal snapshot generation failure. |
341 observer->SnapshotGenerated(FilePath(), 0); | 341 observer->SnapshotGenerated(FilePath(), 0); |
342 } | 342 } |
343 } | 343 } |
344 | 344 |
345 content::URLFetcher* ChromeToMobileService::CreateRequest( | 345 net::URLFetcher* ChromeToMobileService::CreateRequest( |
346 const RequestData& data) { | 346 const RequestData& data) { |
347 bool get = data.type != SNAPSHOT; | 347 bool get = data.type != SNAPSHOT; |
348 GURL service_url(cloud_print_url_->GetCloudPrintServiceURL()); | 348 GURL service_url(cloud_print_url_->GetCloudPrintServiceURL()); |
349 content::URLFetcher* request = content::URLFetcher::Create( | 349 net::URLFetcher* request = content::URLFetcher::Create( |
350 data.type == SEARCH ? GetSearchURL(service_url) : | 350 data.type == SEARCH ? GetSearchURL(service_url) : |
351 GetSubmitURL(service_url, data), | 351 GetSubmitURL(service_url, data), |
352 get ? content::URLFetcher::GET : content::URLFetcher::POST, this); | 352 get ? content::URLFetcher::GET : content::URLFetcher::POST, this); |
353 request->SetRequestContext(profile_->GetRequestContext()); | 353 request->SetRequestContext(profile_->GetRequestContext()); |
354 request->SetMaxRetries(kMaxRetries); | 354 request->SetMaxRetries(kMaxRetries); |
355 request->SetExtraRequestHeaders("Authorization: OAuth " + | 355 request->SetExtraRequestHeaders("Authorization: OAuth " + |
356 access_token_ + "\r\n" + cloud_print::kChromeCloudPrintProxyHeader); | 356 access_token_ + "\r\n" + cloud_print::kChromeCloudPrintProxyHeader); |
357 request->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 357 request->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
358 net::LOAD_DO_NOT_SAVE_COOKIES); | 358 net::LOAD_DO_NOT_SAVE_COOKIES); |
359 return request; | 359 return request; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 | 510 |
511 // Ensure a second response is not sent after reporting failure below. | 511 // Ensure a second response is not sent after reporting failure below. |
512 request_observer_map_.erase(other); | 512 request_observer_map_.erase(other); |
513 break; | 513 break; |
514 } | 514 } |
515 } | 515 } |
516 | 516 |
517 LogMetric(success ? SEND_SUCCESS : SEND_ERROR); | 517 LogMetric(success ? SEND_SUCCESS : SEND_ERROR); |
518 observer->OnSendComplete(success); | 518 observer->OnSendComplete(success); |
519 } | 519 } |
OLD | NEW |