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

Side by Side Diff: chrome/browser/media_gallery/linux/mtp_recursive_device_object_enumerator.cc

Issue 11855005: [Linux, Media Gallery] Fix MTPRecursiveDeviceObjectEnumerator to recursively enumerate all the medi… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 7 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/media_gallery/linux/mtp_recursive_device_object_enumerator.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/media_gallery/linux/mtp_recursive_device_object_enumera tor.h" 5 #include "chrome/browser/media_gallery/linux/mtp_recursive_device_object_enumera tor.h"
6 6
7 #include "base/sequenced_task_runner.h" 7 #include "base/sequenced_task_runner.h"
8 #include "base/synchronization/waitable_event.h" 8 #include "base/synchronization/waitable_event.h"
9 #include "chrome/browser/media_gallery/linux/mtp_device_object_enumerator.h" 9 #include "chrome/browser/media_gallery/linux/mtp_device_object_enumerator.h"
10 #include "chrome/browser/media_gallery/linux/mtp_read_directory_worker.h" 10 #include "chrome/browser/media_gallery/linux/mtp_read_directory_worker.h"
11 11
12 namespace chrome { 12 namespace chrome {
13 13
14 MTPRecursiveDeviceObjectEnumerator::MTPRecursiveDeviceObjectEnumerator( 14 MTPRecursiveDeviceObjectEnumerator::MTPRecursiveDeviceObjectEnumerator(
15 const std::string& handle, 15 const std::string& handle,
16 base::SequencedTaskRunner* task_runner, 16 base::SequencedTaskRunner* task_runner,
17 const std::vector<MtpFileEntry>& entries, 17 const std::vector<MtpFileEntry>& entries,
18 base::WaitableEvent* task_completed_event, 18 base::WaitableEvent* task_completed_event,
19 base::WaitableEvent* shutdown_event) 19 base::WaitableEvent* shutdown_event)
20 : device_handle_(handle), 20 : device_handle_(handle),
21 media_task_runner_(task_runner), 21 media_task_runner_(task_runner),
22 file_entries_(entries),
23 file_entry_iter_(file_entries_.begin()),
24 on_task_completed_event_(task_completed_event), 22 on_task_completed_event_(task_completed_event),
25 on_shutdown_event_(shutdown_event) { 23 on_shutdown_event_(shutdown_event) {
26 DCHECK(on_task_completed_event_); 24 DCHECK(on_task_completed_event_);
27 DCHECK(on_shutdown_event_); 25 DCHECK(on_shutdown_event_);
26 DCHECK(!entries.empty());
28 current_enumerator_.reset(new MTPDeviceObjectEnumerator(entries)); 27 current_enumerator_.reset(new MTPDeviceObjectEnumerator(entries));
29 } 28 }
30 29
31 MTPRecursiveDeviceObjectEnumerator::~MTPRecursiveDeviceObjectEnumerator() { 30 MTPRecursiveDeviceObjectEnumerator::~MTPRecursiveDeviceObjectEnumerator() {
32 } 31 }
33 32
34 FilePath MTPRecursiveDeviceObjectEnumerator::Next() { 33 FilePath MTPRecursiveDeviceObjectEnumerator::Next() {
35 if (on_shutdown_event_->IsSignaled()) { 34 if (on_shutdown_event_->IsSignaled()) {
36 // Process is in shut down mode. 35 // Process is in shut down mode.
37 return FilePath(); 36 return FilePath();
38 } 37 }
39 38
40 FilePath path = current_enumerator_->Next(); 39 MTPDeviceObjectEnumerator* enumerator =
41 if (!path.empty()) 40 static_cast<MTPDeviceObjectEnumerator*>(current_enumerator_.get());
42 return path; 41 if (!enumerator) {
Lei Zhang 2013/01/12 00:08:36 I fail to see how this helps. How does |enumerator
kmadhusu 2013/01/12 01:03:53 Removed this check and modified code to handle the
42 // This should not ever be null. If it happens this function is not used
43 // correctly.
44 // Correct usage:
45 // while (!enumerator->Next().empty()) {
46 // // doSomething();
47 // }
48 return FilePath();
49 }
43 50
44 // We reached the end. 51 if (!enumerator->HasMoreEntries())
45 if (file_entry_iter_ == file_entries_.end()) 52 UpdateEnumeratorToTraverseSubdirectory();
46 return FilePath();
47 53
48 // Enumerate subdirectories of the next media file entry. 54 if (IsDirectory()) {
49 MtpFileEntry next_file_entry = *file_entry_iter_; 55 // If the current entry is a directory, add it to
50 ++file_entry_iter_; 56 // |untraversed_directory_entry_ids_| to scan after scanning this directory.
51 57 DirectoryEntryId dir_entry_id;
52 // Create a MTPReadDirectoryWorker object to enumerate sub directories. 58 if (static_cast<MTPDeviceObjectEnumerator*>(
53 scoped_refptr<MTPReadDirectoryWorker> worker(new MTPReadDirectoryWorker( 59 current_enumerator_.get())->GetEntryId(&dir_entry_id))
54 device_handle_, next_file_entry.item_id(), media_task_runner_, 60 untraversed_directory_entry_ids_.push(dir_entry_id);
55 on_task_completed_event_, on_shutdown_event_));
56 worker->Run();
57 if (!worker->get_file_entries().empty()) {
58 current_enumerator_.reset(
59 new MTPDeviceObjectEnumerator(worker->get_file_entries()));
60 } else {
61 current_enumerator_.reset(
62 new fileapi::FileSystemFileUtil::EmptyFileEnumerator());
63 } 61 }
64 return current_enumerator_->Next(); 62 return current_enumerator_->Next();
65 } 63 }
66 64
67 int64 MTPRecursiveDeviceObjectEnumerator::Size() { 65 int64 MTPRecursiveDeviceObjectEnumerator::Size() {
68 return current_enumerator_->Size(); 66 return current_enumerator_->Size();
69 } 67 }
70 68
71 bool MTPRecursiveDeviceObjectEnumerator::IsDirectory() { 69 bool MTPRecursiveDeviceObjectEnumerator::IsDirectory() {
72 return current_enumerator_->IsDirectory(); 70 return current_enumerator_->IsDirectory();
73 } 71 }
74 72
75 base::Time MTPRecursiveDeviceObjectEnumerator::LastModifiedTime() { 73 base::Time MTPRecursiveDeviceObjectEnumerator::LastModifiedTime() {
76 return current_enumerator_->LastModifiedTime(); 74 return current_enumerator_->LastModifiedTime();
77 } 75 }
78 76
77 void MTPRecursiveDeviceObjectEnumerator::
78 UpdateEnumeratorToTraverseSubdirectory() {
79 while (!untraversed_directory_entry_ids_.empty()) {
80 // Create a MTPReadDirectoryWorker object to enumerate sub directories.
81 DirectoryEntryId dir_entry_id = untraversed_directory_entry_ids_.front();
82 untraversed_directory_entry_ids_.pop();
83 scoped_refptr<MTPReadDirectoryWorker> worker(new MTPReadDirectoryWorker(
84 device_handle_, dir_entry_id, media_task_runner_,
85 on_task_completed_event_, on_shutdown_event_));
86 worker->Run();
87 if (!worker->get_file_entries().empty()) {
88 current_enumerator_.reset(
89 new MTPDeviceObjectEnumerator(worker->get_file_entries()));
90 return;
91 }
92 }
93
94 // Reached the end. Traversed all the sub directories.
95 current_enumerator_.reset(
96 new fileapi::FileSystemFileUtil::EmptyFileEnumerator());
97 }
98
79 } // namespace chrome 99 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/media_gallery/linux/mtp_recursive_device_object_enumerator.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698