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

Side by Side Diff: chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.cc

Issue 11297002: [Media Gallery] Added code to support mtp device media file system on Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 8 years 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 | Annotate | Revision Log
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 // MTPDeviceDelegateImplWin implementation.
6
7 #include "chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.h"
8
9 #include <portabledevice.h>
10
Peter Kasting 2012/12/03 20:13:00 Nit: No blank line here
kmadhusu 2012/12/15 02:34:56 Done.
11 #include <vector>
12
13 #include "base/file_path.h"
14 #include "base/file_util.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/string_split.h"
17 #include "base/string_util.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/media_gallery/win/mtp_device_object_entry.h"
21 #include "chrome/browser/media_gallery/win/mtp_device_object_enumerator.h"
22 #include "chrome/browser/media_gallery/win/mtp_device_operations_util.h"
23 #include "chrome/browser/media_gallery/win/recursive_mtp_device_object_enumerato r.h"
24 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi n.h"
25 #include "content/public/browser/browser_thread.h"
26
27 namespace chrome {
28
29 namespace {
30
31 // Gets the details of the MTP storage specified by the |storage_path|. On
32 // success, returns true and fills in |pnp_device_id| and |storage_object_id|.
33 // On failure, returns false and |pnp_device_id| and |storage_object_id| are
34 // not set.
35 bool GetStorageInfoFromStoragePath(const string16& storage_path,
36 string16* pnp_device_id,
37 string16* storage_object_id) {
38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
39 DCHECK(!storage_path.empty());
40 DCHECK(pnp_device_id);
41 DCHECK(storage_object_id);
42 string16 storage_device_id;
43 RemoveChars(storage_path, L"\\\\", &storage_device_id);
44 DCHECK(!storage_device_id.empty());
45 RemovableDeviceNotificationsWindowWin* notifications =
46 RemovableDeviceNotificationsWindowWin::GetInstance();
47 return notifications->GetMTPStorageInfoFromDeviceId(
48 UTF16ToUTF8(storage_device_id), pnp_device_id, storage_object_id);
Peter Kasting 2012/12/03 20:13:00 Nit: Or just return RemovableDeviceNotification
kmadhusu 2012/12/15 02:34:56 I need to do a static_cast before calling GetMTPSt
49 }
50
51 } // namespace
52
53
54 // MTPDeviceDelegateImplWin ---------------------------------------------------
55
56 MTPDeviceDelegateImplWin::MTPDeviceDelegateImplWin(const string16& fs_root_path)
57 : registered_device_path_(fs_root_path),
58 cancel_pending_tasks_(false) {
59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
60 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
61 media_task_runner_ = pool->GetSequencedTaskRunner(
62 pool->GetNamedSequenceToken("media-task-runner"));
63
64 bool success = GetStorageInfoFromStoragePath(
65 registered_device_path_, &pnp_device_id_, &storage_object_id_);
66 DCHECK(success);
67 }
68
69 base::PlatformFileError MTPDeviceDelegateImplWin::GetFileInfo(
70 const FilePath& file_path,
71 base::PlatformFileInfo* file_info) {
72 if (!LazyInit())
73 return base::PLATFORM_FILE_ERROR_FAILED;
74 string16 object_id = GetFileObjectIdFromPath(file_path.value());
75 if (object_id.empty())
76 return base::PLATFORM_FILE_ERROR_FAILED;
77 return MTPDeviceOperationsUtil::GetFileEntryInfo(device_.get(), object_id,
78 file_info);
79 }
80
81 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator>
82 MTPDeviceDelegateImplWin::CreateFileEnumerator(const FilePath& root,
83 bool recursive) {
84 if (root.value().empty() || !LazyInit()) {
Peter Kasting 2012/12/03 20:13:00 Nit: Because the code to create your scoped_ptrs i
kmadhusu 2012/12/15 02:34:56 Done.
85 return make_scoped_ptr(
86 new fileapi::FileSystemFileUtil::EmptyFileEnumerator())
87 .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>();
88 }
89 string16 object_id = GetFileObjectIdFromPath(root.value());
90 if (object_id.empty()) {
91 return make_scoped_ptr(
92 new fileapi::FileSystemFileUtil::EmptyFileEnumerator())
93 .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>();
94 }
95
96 MTPDeviceObjectEntries entries;
97 if (!MTPDeviceOperationsUtil::GetDirectoryEntries(device_.get(), object_id,
98 &entries) ||
99 entries.empty()) {
100 return make_scoped_ptr(
101 new fileapi::FileSystemFileUtil::EmptyFileEnumerator())
102 .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>();
103 }
104
105 if (recursive) {
106 return make_scoped_ptr(
107 new RecursiveMTPDeviceObjectEnumerator(device_.get(), entries))
108 .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>();
109 }
110 return make_scoped_ptr(new MTPDeviceObjectEnumerator(entries))
111 .PassAs<fileapi::FileSystemFileUtil::AbstractFileEnumerator>();
112 }
113
114 base::PlatformFileError MTPDeviceDelegateImplWin::CreateSnapshotFile(
115 const FilePath& device_file_path,
116 const FilePath& local_path,
117 base::PlatformFileInfo* file_info) {
118 if (!LazyInit())
119 return base::PLATFORM_FILE_ERROR_FAILED;
120 string16 file_object_id = GetFileObjectIdFromPath(device_file_path.value());
121 if (file_object_id.empty())
122 return base::PLATFORM_FILE_ERROR_FAILED;
123 if (!MTPDeviceOperationsUtil::WriteFileObjectData(device_.get(),
124 file_object_id,
125 local_path))
126 return base::PLATFORM_FILE_ERROR_FAILED;
127 base::PlatformFileError error = GetFileInfo(device_file_path, file_info);
128 // Modify the last modified time to null. This prevents the time stamp
Peter Kasting 2012/12/03 20:13:00 Nit: Modify -> Set
kmadhusu 2012/12/15 02:34:56 Done.
129 // verification in LocalFileStreamReader.
Peter Kasting 2012/12/03 20:13:00 Nit: Can you be more detailed in describing what v
kmadhusu 2012/12/15 02:34:56 Done.
130 file_info->last_modified = base::Time();
131 return error;
132 }
133
134 base::SequencedTaskRunner* MTPDeviceDelegateImplWin::GetMediaTaskRunner() {
135 return media_task_runner_.get();
136 }
137
138 void MTPDeviceDelegateImplWin::CancelPendingTasksAndDeleteDelegate() {
139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
140 // Caution: This function is called on the IO thread. Access only the thread
141 // safe member variables in this function. Do all the clean up operations in
142 // DeleteDelegateOnTaskRunner().
143 {
144 base::AutoLock locked(cancel_tasks_lock_);
145 cancel_pending_tasks_ = true;
146 }
147 media_task_runner_->PostTask(
148 FROM_HERE,
149 base::Bind(&MTPDeviceDelegateImplWin::DeleteDelegateOnTaskRunner,
150 base::Unretained(this)));
151 }
152
153 base::WeakPtr<fileapi::MTPDeviceDelegate> MTPDeviceDelegateImplWin::
154 GetAsWeakPtrOnIOThread() {
155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
156 base::WeakPtr<fileapi::MTPDeviceDelegate> delegate = AsWeakPtr();
157 // The weak pointer is instantiated on IO thread, but only dereferenced on
158 // task runner thread. Therefore, detach from the current thread.
159 DetachFromThread();
160 return delegate;
161 }
162
163 MTPDeviceDelegateImplWin::~MTPDeviceDelegateImplWin() {
164 DCHECK(media_task_runner_->RunsTasksOnCurrentThread());
165 // Do all the clean up operations on DeleteDelegateOnTaskRunner().
166 }
167
168 bool MTPDeviceDelegateImplWin::LazyInit() {
169 DCHECK(media_task_runner_);
170 DCHECK(media_task_runner_->RunsTasksOnCurrentThread());
171 {
172 // Device delegate will be destructed soon.
173 base::AutoLock locked(cancel_tasks_lock_);
174 if (cancel_pending_tasks_)
175 return false;
176 }
177 if (device_.get())
178 return true; // Already successfully initialized.
179 DCHECK(!pnp_device_id_.empty());
180 return MTPDeviceOperationsUtil::OpenDevice(pnp_device_id_, &device_);
181 }
182
183 string16 MTPDeviceDelegateImplWin::GetFileObjectIdFromPath(
184 const string16& file_path) {
185 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
Peter Kasting 2012/12/03 20:13:00 Nit: Why not use |media_task_runner_| here like yo
kmadhusu 2012/12/15 02:34:56 Done.
186 DCHECK(!file_path.empty());
187 if (registered_device_path_ == file_path)
188 return storage_object_id_;
189
190 string16 actual_file_path(file_path);
191 ReplaceFirstSubstringAfterOffset(&actual_file_path, 0,
192 registered_device_path_, string16());
193 DCHECK(!actual_file_path.empty());
194 typedef std::vector<string16> PathComponents;
195 PathComponents path_components;
196 base::SplitString(actual_file_path, L'\\', &path_components);
197 DCHECK(!path_components.empty());
198 string16 parent_id(storage_object_id_);
199 string16 file_object_id;
200 for (PathComponents::const_iterator path_iter = path_components.begin() + 1;
201 path_iter != path_components.end(); ++path_iter) {
202 file_object_id = MTPDeviceOperationsUtil::GetObjectIdFromName(device_,
203 parent_id,
204 *path_iter);
205 if (file_object_id.empty())
206 break;
207 parent_id = file_object_id;
208 }
209 return file_object_id;
210 }
211
212 void MTPDeviceDelegateImplWin::DeleteDelegateOnTaskRunner() {
213 DCHECK(media_task_runner_->RunsTasksOnCurrentThread());
214 delete this;
215 }
216
217 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698