summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_gallery/mtp_device_delegate_impl_chromeos.cc
blob: 0f3b34f23fc2e5538a572e1a8e07113d75fc2eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media_gallery/mtp_device_delegate_impl_chromeos.h"

#include "base/bind.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/sequenced_task_runner.h"
#include "base/sequenced_task_runner_helpers.h"
#include "base/string_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/mtp/media_transfer_protocol_manager.h"
#include "chromeos/dbus/media_transfer_protocol_daemon_client.h"
#include "chromeos/dbus/mtp_file_entry.pb.h"
#include "chromeos/dbus/mtp_storage_info.pb.h"
#include "content/public/browser/browser_thread.h"

using base::Bind;
using base::PlatformFileError;
using base::PlatformFileInfo;
using base::SequencedTaskRunner;
using base::Time;
using content::BrowserThread;
using fileapi::FileSystemFileUtil;

namespace {

using base::DeleteHelper;
using base::RefCountedThreadSafe;
using base::WaitableEvent;
using chromeos::mtp::MediaTransferProtocolManager;

// Helper struct to delete worker objects on |media_task_runner_| thread.
template <typename WORKER> struct WorkerDeleter {
  static void Destruct(const WORKER* worker) {
    if (!worker->media_task_runner()->RunsTasksOnCurrentThread()) {
      worker->media_task_runner()->DeleteSoon(FROM_HERE, worker);
      return;
    }
    delete worker;
  }
};

typedef struct WorkerDeleter<class GetFileInfoWorker> GetFileInfoWorkerDeleter;
typedef struct WorkerDeleter<class OpenStorageWorker> OpenStorageWorkerDeleter;
typedef struct WorkerDeleter<class ReadDirectoryWorker>
    ReadDirectoryWorkerDeleter;
typedef struct WorkerDeleter<class ReadFileWorker> ReadFileWorkerDeleter;

// File path separator constant.
const char kRootPath[] = "/";

// Returns MediaTransferProtocolManager instance on success or NULL on failure.
MediaTransferProtocolManager* GetMediaTransferProtocolManager() {
  MediaTransferProtocolManager* mtp_dev_mgr =
      MediaTransferProtocolManager::GetInstance();
  DCHECK(mtp_dev_mgr);
  return mtp_dev_mgr;
}

// Does nothing.
// This method is used to handle the results of
// MediaTransferProtocolManager::CloseStorage method call.
void DoNothing(bool error) {
}

// Returns the device relative file path given |file_path|.
// E.g.: If the |file_path| is "/usb:2,2:12345/DCIM" and |registered_dev_path|
// is "/usb:2,2:12345", this function returns the device relative path which is
// "/DCIM".
std::string GetDeviceRelativePath(const std::string& registered_dev_path,
                                  const std::string& file_path) {
  DCHECK(!registered_dev_path.empty());
  DCHECK(!file_path.empty());

  std::string actual_file_path;
  if (registered_dev_path == file_path) {
    actual_file_path = kRootPath;
  } else {
    actual_file_path = file_path;
    ReplaceFirstSubstringAfterOffset(&actual_file_path, 0,
                                     registered_dev_path.c_str(), "");
  }
  DCHECK(!actual_file_path.empty());
  return actual_file_path;
}

// Worker class to open a mtp device for communication. This class is
// instantiated and destructed on |media_task_runner_|. In order to post a
// request on Dbus thread, the caller should run on UI thread. Therefore, this
// class posts the open device request on UI thread and receives the response
// on UI thread.
class OpenStorageWorker
    : public RefCountedThreadSafe<OpenStorageWorker, OpenStorageWorkerDeleter> {
 public:
  // Constructed on |media_task_runner_| thread.
  OpenStorageWorker(const std::string& name, SequencedTaskRunner* task_runner)
      : storage_name_(name),
        media_task_runner_(task_runner),
        event_(false, false) {
  }

  // This function is invoked on |media_task_runner_| to post the task on UI
  // thread. This blocks the |media_task_runner_| until the task is complete.
  void Run() {
    DCHECK(media_task_runner_->RunsTasksOnCurrentThread());
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                            Bind(&OpenStorageWorker::DoWorkOnUIThread, this));
    event_.Wait();
  }

  // Returns a device handle string if the OpenStorage() request was
  // successfully completed or an empty string otherwise.
  const std::string& device_handle() const { return device_handle_; }

  // Returns the |media_task_runner_| associated with this worker object.
  // This function is exposed for WorkerDeleter struct to access the
  // |media_task_runner_|.
  SequencedTaskRunner* media_task_runner() const {
    return media_task_runner_.get();
  }

 private:
  friend struct WorkerDeleter<OpenStorageWorker>;
  friend class DeleteHelper<OpenStorageWorker>;
  friend class RefCountedThreadSafe<OpenStorageWorker,
                                    OpenStorageWorkerDeleter>;

  // Destructed via OpenStorageWorkerDeleter struct.
  virtual ~OpenStorageWorker() {
    // This object must be destructed on |media_task_runner_|.
  }

  // Dispatches a request to MediaTransferProtocolManager to open the mtp
  // storage for communication. This is called on UI thread.
  void DoWorkOnUIThread() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

    GetMediaTransferProtocolManager()->OpenStorage(
        storage_name_, chromeos::OPEN_STORAGE_MODE_READ_ONLY,
        Bind(&OpenStorageWorker::OnDidWorkOnUIThread, this));
  }

  // Query callback for DoWorkOnUIThread(). |error| is set to true if the device
  // did not open successfully. This function signals to unblock
  // |media_task_runner_|.
  void OnDidWorkOnUIThread(const std::string& device_handle, bool error) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    if (!error)
      device_handle_ = device_handle;
    event_.Signal();
  }

  // Stores the storage name to open the device.
  const std::string storage_name_;

  // Stores a reference to |media_task_runner_| to destruct this object on the
  // correct thread.
  scoped_refptr<SequencedTaskRunner> media_task_runner_;

  // |media_task_runner_| can wait on this event until the required operation
  // is complete.
  // TODO(kmadhusu): Remove this WaitableEvent after modifying the
  // DeviceMediaFileUtil functions as asynchronous functions.
  WaitableEvent event_;

  // Stores the result of OpenStorage() request.
  std::string device_handle_;

  DISALLOW_COPY_AND_ASSIGN(OpenStorageWorker);
};

// Worker class to get media device file information given a |path|.
class GetFileInfoWorker
    : public RefCountedThreadSafe<GetFileInfoWorker, GetFileInfoWorkerDeleter> {
 public:
  // Constructed on |media_task_runner_| thread.
  GetFileInfoWorker(const std::string& handle,
                    const std::string& path,
                    SequencedTaskRunner* task_runner)
      : device_handle_(handle),
        path_(path),
        media_task_runner_(task_runner),
        error_(base::PLATFORM_FILE_OK),
        event_(false, false) {
  }

  // This function is invoked on |media_task_runner_| to post the task on UI
  // thread. This blocks the |media_task_runner_| until the task is complete.
  void Run() {
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                            Bind(&GetFileInfoWorker::DoWorkOnUIThread, this));
    event_.Wait();
  }

  // Returns GetFileInfo() result and fills in |file_info| with requested file
  // entry details.
  PlatformFileError get_file_info(PlatformFileInfo* file_info) const {
    if (file_info)
      *file_info = file_entry_info_;
    return error_;
  }

  // Returns the |media_task_runner_| associated with this worker object.
  // This function is exposed for WorkerDeleter struct to access the
  // |media_task_runner_|.
  SequencedTaskRunner* media_task_runner() const {
    return media_task_runner_.get();
  }

 private:
  friend struct WorkerDeleter<GetFileInfoWorker>;
  friend class DeleteHelper<GetFileInfoWorker>;
  friend class RefCountedThreadSafe<GetFileInfoWorker,
                                    GetFileInfoWorkerDeleter>;

  // Destructed via GetFileInfoWorkerDeleter.
  virtual ~GetFileInfoWorker() {
    // This object must be destructed on |media_task_runner_|.
  }

  // Dispatches a request to MediaTransferProtocolManager to get file
  // information.
  void DoWorkOnUIThread() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

    GetMediaTransferProtocolManager()->GetFileInfoByPath(
        device_handle_, path_,
        Bind(&GetFileInfoWorker::OnDidWorkOnUIThread, this));
  }

  // Query callback for DoWorkOnUIThread(). On success, |file_entry| has media
  // file information. On failure, |error| is set to true. This function signals
  // to unblock |media_task_runner_|.
  void OnDidWorkOnUIThread(const MtpFileEntry& file_entry, bool error) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    if (error) {
      error_ = base::PLATFORM_FILE_ERROR_NOT_FOUND;
    } else {
      file_entry_info_.size = file_entry.file_size();
      file_entry_info_.is_directory =
          file_entry.file_type() == MtpFileEntry::FILE_TYPE_FOLDER;
      file_entry_info_.is_symbolic_link = false;
      file_entry_info_.last_modified =
          base::Time::FromTimeT(file_entry.modification_time());
      file_entry_info_.last_accessed =
          base::Time::FromTimeT(file_entry.modification_time());
      file_entry_info_.creation_time = base::Time();
    }
    event_.Signal();
  }

  // Stores the device handle to query the device.
  const std::string device_handle_;

  // Stores the requested media device file path.
  const std::string path_;

  // Stores a reference to |media_task_runner_| to destruct this object on the
  // correct thread.
  scoped_refptr<SequencedTaskRunner> media_task_runner_;

  // Stores the result of GetFileInfo().
  PlatformFileError error_;

  // Stores the media file entry information.
  PlatformFileInfo file_entry_info_;

  // |media_task_runner_| can wait on this event until the required operation
  // is complete.
  // TODO(kmadhusu): Remove this WaitableEvent after modifying the
  // DeviceMediaFileUtil functions as asynchronous functions.
  WaitableEvent event_;

  DISALLOW_COPY_AND_ASSIGN(GetFileInfoWorker);
};

// Worker class to read media device file data given a file |path|.
class ReadFileWorker
    : public RefCountedThreadSafe<ReadFileWorker, ReadFileWorkerDeleter> {
 public:
  // Constructed on |media_task_runner_| thread.
  ReadFileWorker(const std::string& handle,
                 const std::string& path,
                 SequencedTaskRunner* task_runner)
      : device_handle_(handle),
        path_(path),
        media_task_runner_(task_runner),
        event_(false, false) {
  }

  // This function is invoked on |media_task_runner_| to post the task on UI
  // thread. This blocks the |media_task_runner_| until the task is complete.
  void Run() {
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                            Bind(&ReadFileWorker::DoWorkOnUIThread, this));
    event_.Wait();
  }

  // Returns the media file contents received by ReadFileByPath() callback
  // function.
  const std::string& data() const { return data_; }

  // Returns the |media_task_runner_| associated with this worker object.
  // This function is exposed for WorkerDeleter struct to access the
  // |media_task_runner_|.
  SequencedTaskRunner* media_task_runner() const {
    return media_task_runner_.get();
  }

 private:
  friend struct WorkerDeleter<ReadFileWorker>;
  friend class DeleteHelper<ReadFileWorker>;
  friend class RefCountedThreadSafe<ReadFileWorker, ReadFileWorkerDeleter>;

  // Destructed via ReadFileWorkerDeleter.
  virtual ~ReadFileWorker() {
    // This object must be destructed on |media_task_runner_|.
  }

  // Dispatches a request to MediaTransferProtocolManager to get the media file
  // contents.
  void DoWorkOnUIThread() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    GetMediaTransferProtocolManager()->ReadFileByPath(
      device_handle_, path_, Bind(&ReadFileWorker::OnDidWorkOnUIThread, this));
  }

  // Query callback for DoWorkOnUIThread(). On success, |data| has the media
  // file contents. On failure, |error| is set to true. This function signals
  // to unblock |media_task_runner_|.
  void OnDidWorkOnUIThread(const std::string& data, bool error) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    if (!error) {
      // TODO(kmadhusu): Data could be really huge. Consider passing data by
      // pointer/ref rather than by value here to avoid an extra data copy.
      data_ = data;
    }
    event_.Signal();
  }

  // Stores the device unique identifier to query the device.
  const std::string device_handle_;

  // Stores the media device file path.
  const std::string path_;

  // Stores a reference to |media_task_runner_| to destruct this object on the
  // correct thread.
  scoped_refptr<SequencedTaskRunner> media_task_runner_;

  // Stores the result of ReadFileByPath() callback.
  std::string data_;

  // |media_task_runner_| can wait on this event until the required operation
  // is complete.
  // TODO(kmadhusu): Remove this WaitableEvent after modifying the
  // DeviceMediaFileUtil functions as asynchronous functions.
  WaitableEvent event_;

  DISALLOW_COPY_AND_ASSIGN(ReadFileWorker);
};

// Worker class to read directory contents. Device is already opened for
// communication.
class ReadDirectoryWorker
    : public RefCountedThreadSafe<ReadDirectoryWorker,
                                  ReadDirectoryWorkerDeleter> {
 public:
  // Construct a worker object given the directory |path|. This object is
  // constructed on |media_task_runner_| thread.
  ReadDirectoryWorker(const std::string& handle,
                      const std::string& path,
                      SequencedTaskRunner* task_runner)
      : device_handle_(handle),
        dir_path_(path),
        dir_entry_id_(0),
        media_task_runner_(task_runner),
        event_(false, false) {
    DCHECK(!dir_path_.empty());
  }

  // Construct a worker object given the directory |entry_id|. This object is
  // constructed on |media_task_runner_| thread.
  ReadDirectoryWorker(const std::string& storage_name,
                      const uint32_t entry_id,
                      SequencedTaskRunner* task_runner)
      : device_handle_(storage_name),
        dir_entry_id_(entry_id),
        media_task_runner_(task_runner),
        event_(false, false) {
  }

  // This function is invoked on |media_task_runner_| to post the task on UI
  // thread. This blocks the |media_task_runner_| until the task is complete.
  void Run() {
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                            Bind(&ReadDirectoryWorker::DoWorkOnUIThread, this));
    event_.Wait();
  }

  // Returns the directory entries for the given directory path.
  const std::vector<MtpFileEntry>& get_file_entries() const {
    return file_entries_;
  }

  // Returns the |media_task_runner_| associated with this worker object.
  // This function is exposed for WorkerDeleter struct to access the
  // |media_task_runner_|.
  SequencedTaskRunner* media_task_runner() const {
    return media_task_runner_.get();
  }

 private:
  friend struct WorkerDeleter<ReadDirectoryWorker>;
  friend class DeleteHelper<ReadDirectoryWorker>;
  friend class RefCountedThreadSafe<ReadDirectoryWorker,
                                    ReadDirectoryWorkerDeleter>;

  // Destructed via ReadDirectoryWorkerDeleter.
  virtual ~ReadDirectoryWorker() {
    // This object must be destructed on |media_task_runner_|.
  }

  // Dispatches a request to MediaTransferProtocolManager to read the directory
  // entries. This is called on UI thread.
  void DoWorkOnUIThread() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

    if (!dir_path_.empty()) {
      GetMediaTransferProtocolManager()->ReadDirectoryByPath(
          device_handle_, dir_path_,
          Bind(&ReadDirectoryWorker::OnDidWorkOnUIThread, this));
    } else {
      GetMediaTransferProtocolManager()->ReadDirectoryById(
          device_handle_, dir_entry_id_,
          Bind(&ReadDirectoryWorker::OnDidWorkOnUIThread, this));
    }
  }

  // Query callback for DoWorkOnUIThread(). On success, |file_entries| has the
  // directory file entries. |error| is true if there was an error. This
  // function signals to unblock |media_task_runner_|.
  void OnDidWorkOnUIThread(const std::vector<MtpFileEntry>& file_entries,
                           bool error) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    if (!error)
      file_entries_ = file_entries;
    event_.Signal();
  }

  // Stores the device handle to communicate with storage device.
  const std::string device_handle_;

  // Stores the directory path whose contents needs to be listed.
  const std::string dir_path_;

  // Stores the directory entry id whose contents needs to be listed.
  const uint32_t dir_entry_id_;

  // Stores a reference to |media_task_runner_| to destruct this object on the
  // correct thread.
  scoped_refptr<SequencedTaskRunner> media_task_runner_;

  // |media_task_runner_| can wait on this event until the required operation
  // is complete.
  // TODO(kmadhusu): Remove this WaitableEvent after modifying the
  // DeviceMediaFileUtil functions as asynchronous functions.
  WaitableEvent event_;

  // Stores the result of read directory request.
  std::vector<MtpFileEntry> file_entries_;

  DISALLOW_COPY_AND_ASSIGN(ReadDirectoryWorker);
};

// Simply enumerate each files from a given file entry list.
// Used to enumerate top-level files of an media file system.
class MediaFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator {
 public:
  explicit MediaFileEnumerator(const std::vector<MtpFileEntry>& entries)
      : file_entries_(entries),
        file_entry_iter_(file_entries_.begin()) {
  }

  virtual ~MediaFileEnumerator() {}

  // AbstractFileEnumerator override.
  // Returns the next file entry path on success and empty file path on
  // failure.
  virtual FilePath Next() OVERRIDE {
    if (file_entry_iter_ == file_entries_.end())
      return FilePath();

    current_file_info_ = *file_entry_iter_;
    ++file_entry_iter_;
    return FilePath(current_file_info_.file_name());
  }

  // AbstractFileEnumerator override.
  // Returns the size of the current file entry.
  virtual int64 Size() OVERRIDE {
    return current_file_info_.file_size();
  }

  // AbstractFileEnumerator override.
  // Returns true if the current file entry is a directory else false.
  virtual bool IsDirectory() OVERRIDE {
    return current_file_info_.file_type() == MtpFileEntry::FILE_TYPE_FOLDER;
  }

  // AbstractFileEnumerator override.
  // Returns the last modified time of the current file entry.
  virtual base::Time LastModifiedTime() OVERRIDE {
    return base::Time::FromTimeT(current_file_info_.modification_time());
  }

 private:
  // List of directory file entries information.
  const std::vector<MtpFileEntry> file_entries_;

  // Iterator to access the individual file entries.
  std::vector<MtpFileEntry>::const_iterator file_entry_iter_;

  // Stores the current file information.
  MtpFileEntry current_file_info_;

  DISALLOW_COPY_AND_ASSIGN(MediaFileEnumerator);
};

// Recursively enumerate each file entry from a given media file entry set.
class RecursiveMediaFileEnumerator
    : public FileSystemFileUtil::AbstractFileEnumerator {
 public:
  RecursiveMediaFileEnumerator(const std::string& handle,
                               SequencedTaskRunner* task_runner,
                               const std::vector<MtpFileEntry>& entries)
      : device_handle_(handle),
        media_task_runner_(task_runner),
        file_entries_(entries),
        file_entry_iter_(file_entries_.begin()) {
    current_enumerator_.reset(new MediaFileEnumerator(entries));
  }

  virtual ~RecursiveMediaFileEnumerator() {}

  // AbstractFileEnumerator override.
  // Returns the next file entry path on success and empty file path on
  // failure or when it reaches the end.
  virtual FilePath Next() OVERRIDE {
    FilePath path = current_enumerator_->Next();
    if (!path.empty())
      return path;

    // We reached the end.
    if (file_entry_iter_ == file_entries_.end())
      return FilePath();

    // Enumerate subdirectories of the next media file entry.
    MtpFileEntry next_file_entry = *file_entry_iter_;
    ++file_entry_iter_;

    // Create a ReadDirectoryWorker object to enumerate sub directories.
    scoped_refptr<ReadDirectoryWorker> worker(new ReadDirectoryWorker(
        device_handle_, next_file_entry.item_id(), media_task_runner_));
    worker->Run();
    if (!worker->get_file_entries().empty()) {
      current_enumerator_.reset(
          new MediaFileEnumerator(worker->get_file_entries()));
    } else {
      current_enumerator_.reset(new FileSystemFileUtil::EmptyFileEnumerator());
    }
    return current_enumerator_->Next();
  }

  // AbstractFileEnumerator override.
  // Returns the size of the current file entry.
  virtual int64 Size() OVERRIDE {
    return current_enumerator_->Size();
  }

  // AbstractFileEnumerator override.
  // Returns true if the current media file entry is a folder type else false.
  virtual bool IsDirectory() OVERRIDE {
    return current_enumerator_->IsDirectory();
  }

  // AbstractFileEnumerator override.
  // Returns the last modified time of the current file entry.
  virtual base::Time LastModifiedTime() OVERRIDE {
    return current_enumerator_->LastModifiedTime();
  }

 private:
  // Stores the device handle that was used to open the device.
  const std::string device_handle_;

  // Stores a reference to |media_task_runner_| to construct and destruct
  // ReadDirectoryWorker object on the correct thread.
  scoped_refptr<SequencedTaskRunner> media_task_runner_;

  // List of top-level directory file entries.
  const std::vector<MtpFileEntry> file_entries_;

  // Iterator to access the individual file entries.
  std::vector<MtpFileEntry>::const_iterator file_entry_iter_;

  // Enumerator to access current directory Id/path entries.
  scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> current_enumerator_;

  DISALLOW_COPY_AND_ASSIGN(RecursiveMediaFileEnumerator);
};

}  // namespace

namespace chromeos {

MtpDeviceDelegateImplCros::MtpDeviceDelegateImplCros(
    const std::string& device_location)
    : device_path_(device_location) {
  CHECK(!device_path_.empty());
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
  base::SequencedWorkerPool::SequenceToken media_sequence_token =
      pool->GetNamedSequenceToken("media-task-runner");
  media_task_runner_ = pool->GetSequencedTaskRunner(media_sequence_token);
  DCHECK(media_task_runner_);
}

MtpDeviceDelegateImplCros::~MtpDeviceDelegateImplCros() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  GetMediaTransferProtocolManager()->CloseStorage(device_handle_,
                                                  Bind(&DoNothing));
}

PlatformFileError MtpDeviceDelegateImplCros::GetFileInfo(
    const FilePath& file_path,
    PlatformFileInfo* file_info) {
  if (!LazyInit())
    return base::PLATFORM_FILE_ERROR_FAILED;

  scoped_refptr<GetFileInfoWorker> worker(new GetFileInfoWorker(
      device_handle_, GetDeviceRelativePath(device_path_, file_path.value()),
      media_task_runner_));
  worker->Run();
  return worker->get_file_info(file_info);
}

FileSystemFileUtil::AbstractFileEnumerator*
MtpDeviceDelegateImplCros::CreateFileEnumerator(
        const FilePath& root,
        bool recursive) {
  if (root.value().empty() || !LazyInit())
    return new FileSystemFileUtil::EmptyFileEnumerator();

  scoped_refptr<ReadDirectoryWorker> worker(new ReadDirectoryWorker(
      device_handle_, GetDeviceRelativePath(device_path_, root.value()),
      media_task_runner_));
  worker->Run();

  if (worker->get_file_entries().empty())
    return new FileSystemFileUtil::EmptyFileEnumerator();

  if (recursive) {
    return new RecursiveMediaFileEnumerator(
        device_handle_, media_task_runner_, worker->get_file_entries());
  }
  return new MediaFileEnumerator(worker->get_file_entries());
}

PlatformFileError MtpDeviceDelegateImplCros::CreateSnapshotFile(
    const FilePath& device_file_path,
    const FilePath& local_path,
    PlatformFileInfo* file_info) {
  if (!LazyInit())
    return base::PLATFORM_FILE_ERROR_FAILED;

  scoped_refptr<ReadFileWorker> worker(new ReadFileWorker(
      device_handle_,
      GetDeviceRelativePath(device_path_, device_file_path.value()),
      media_task_runner_));
  worker->Run();

  const std::string& file_data = worker->data();
  int data_size = static_cast<int>(file_data.length());
  if (file_data.empty() ||
      file_util::WriteFile(local_path, file_data.c_str(),
                           data_size) != data_size) {
    return base::PLATFORM_FILE_ERROR_FAILED;
  }

  PlatformFileError error = GetFileInfo(device_file_path, file_info);

  // Modify the last modified time to null. This prevents the time stamp
  // verfication in LocalFileStreamReader.
  file_info->last_modified = base::Time();
  return error;
}

SequencedTaskRunner* MtpDeviceDelegateImplCros::media_task_runner() {
  return media_task_runner_.get();
}

void MtpDeviceDelegateImplCros::DeleteOnCorrectThread() const {
  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this);
    return;
  }
  delete this;
}

bool MtpDeviceDelegateImplCros::LazyInit() {
  DCHECK(media_task_runner_);
  DCHECK(media_task_runner_->RunsTasksOnCurrentThread());

  if (!device_handle_.empty())
    return true;  // Already successfully initialized.

  std::string storage_name;
  RemoveChars(device_path_, kRootPath, &storage_name);
  DCHECK(!storage_name.empty());
  scoped_refptr<OpenStorageWorker> worker(new OpenStorageWorker(
      storage_name, media_task_runner_));
  worker->Run();
  device_handle_ = worker->device_handle();
  return !device_handle_.empty();
}

}  // namespace chromeos