Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: chrome/browser/media_galleries/linux/mtp_read_file_worker.cc

Issue 2358493002: Remove MTP support on Linux. (Closed)
Patch Set: move files Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/media_galleries/linux/mtp_read_file_worker.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/numerics/safe_conversions.h"
14 #include "chrome/browser/media_galleries/linux/snapshot_file_details.h"
15 #include "components/storage_monitor/storage_monitor.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h"
19
20 using content::BrowserThread;
21 using storage_monitor::StorageMonitor;
22
23 namespace {
24
25 // Appends |data| to the snapshot file specified by the |snapshot_file_path| on
26 // the file thread.
27 // Returns the number of bytes written to the snapshot file. In case of failure,
28 // returns zero.
29 uint32_t WriteDataChunkIntoSnapshotFileOnFileThread(
30 const base::FilePath& snapshot_file_path,
31 const std::string& data) {
32 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
33 return base::AppendToFile(snapshot_file_path, data.c_str(), data.size())
34 ? base::checked_cast<uint32_t>(data.size())
35 : 0;
36 }
37
38 } // namespace
39
40 MTPReadFileWorker::MTPReadFileWorker(const std::string& device_handle)
41 : device_handle_(device_handle),
42 weak_ptr_factory_(this) {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 DCHECK(!device_handle_.empty());
45 }
46
47 MTPReadFileWorker::~MTPReadFileWorker() {
48 }
49
50 void MTPReadFileWorker::WriteDataIntoSnapshotFile(
51 const SnapshotRequestInfo& request_info,
52 const base::File::Info& snapshot_file_info) {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
54 ReadDataChunkFromDeviceFile(
55 base::MakeUnique<SnapshotFileDetails>(request_info, snapshot_file_info));
56 }
57
58 void MTPReadFileWorker::ReadDataChunkFromDeviceFile(
59 std::unique_ptr<SnapshotFileDetails> snapshot_file_details) {
60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
61 DCHECK(snapshot_file_details.get());
62
63 // To avoid calling |snapshot_file_details| methods and passing ownership of
64 // |snapshot_file_details| in the same_line.
65 SnapshotFileDetails* snapshot_file_details_ptr = snapshot_file_details.get();
66
67 device::MediaTransferProtocolManager* mtp_device_manager =
68 StorageMonitor::GetInstance()->media_transfer_protocol_manager();
69 mtp_device_manager->ReadFileChunk(
70 device_handle_,
71 snapshot_file_details_ptr->file_id(),
72 snapshot_file_details_ptr->bytes_written(),
73 snapshot_file_details_ptr->BytesToRead(),
74 base::Bind(&MTPReadFileWorker::OnDidReadDataChunkFromDeviceFile,
75 weak_ptr_factory_.GetWeakPtr(),
76 base::Passed(&snapshot_file_details)));
77 }
78
79 void MTPReadFileWorker::OnDidReadDataChunkFromDeviceFile(
80 std::unique_ptr<SnapshotFileDetails> snapshot_file_details,
81 const std::string& data,
82 bool error) {
83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
84 DCHECK(snapshot_file_details.get());
85 snapshot_file_details->set_error_occurred(
86 error || (data.size() != snapshot_file_details->BytesToRead()));
87 if (snapshot_file_details->error_occurred()) {
88 OnDidWriteIntoSnapshotFile(std::move(snapshot_file_details));
89 return;
90 }
91
92 // To avoid calling |snapshot_file_details| methods and passing ownership of
93 // |snapshot_file_details| in the same_line.
94 SnapshotFileDetails* snapshot_file_details_ptr = snapshot_file_details.get();
95 content::BrowserThread::PostTaskAndReplyWithResult(
96 content::BrowserThread::FILE,
97 FROM_HERE,
98 base::Bind(&WriteDataChunkIntoSnapshotFileOnFileThread,
99 snapshot_file_details_ptr->snapshot_file_path(),
100 data),
101 base::Bind(&MTPReadFileWorker::OnDidWriteDataChunkIntoSnapshotFile,
102 weak_ptr_factory_.GetWeakPtr(),
103 base::Passed(&snapshot_file_details)));
104 }
105
106 void MTPReadFileWorker::OnDidWriteDataChunkIntoSnapshotFile(
107 std::unique_ptr<SnapshotFileDetails> snapshot_file_details,
108 uint32_t bytes_written) {
109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
110 DCHECK(snapshot_file_details.get());
111 if (snapshot_file_details->AddBytesWritten(bytes_written)) {
112 if (!snapshot_file_details->IsSnapshotFileWriteComplete()) {
113 ReadDataChunkFromDeviceFile(std::move(snapshot_file_details));
114 return;
115 }
116 } else {
117 snapshot_file_details->set_error_occurred(true);
118 }
119 OnDidWriteIntoSnapshotFile(std::move(snapshot_file_details));
120 }
121
122 void MTPReadFileWorker::OnDidWriteIntoSnapshotFile(
123 std::unique_ptr<SnapshotFileDetails> snapshot_file_details) {
124 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
125 DCHECK(snapshot_file_details.get());
126
127 if (snapshot_file_details->error_occurred()) {
128 content::BrowserThread::PostTask(
129 content::BrowserThread::IO,
130 FROM_HERE,
131 base::Bind(snapshot_file_details->error_callback(),
132 base::File::FILE_ERROR_FAILED));
133 return;
134 }
135 content::BrowserThread::PostTask(
136 content::BrowserThread::IO,
137 FROM_HERE,
138 base::Bind(snapshot_file_details->success_callback(),
139 snapshot_file_details->file_info(),
140 snapshot_file_details->snapshot_file_path()));
141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698