Chromium Code Reviews| Index: chrome/browser/media_gallery/win/mtp_get_storage_info_worker.cc |
| diff --git a/chrome/browser/media_gallery/win/mtp_get_storage_info_worker.cc b/chrome/browser/media_gallery/win/mtp_get_storage_info_worker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4c91a55326872c200d703d28e3b901be47fe46e5 |
| --- /dev/null |
| +++ b/chrome/browser/media_gallery/win/mtp_get_storage_info_worker.cc |
| @@ -0,0 +1,88 @@ |
| +// 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. |
| +// |
| +// MTPGetStorageInfoWorker implementation. |
| + |
| +#include "chrome/browser/media_gallery/win/mtp_get_storage_info_worker.h" |
| + |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/string_util.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/media_gallery/win/mtp_device_delegate_impl_win.h" |
| +#include "chrome/browser/system_monitor/removable_storage_notifications.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace chrome { |
| + |
| +MTPGetStorageInfoWorker::MTPGetStorageInfoWorker( |
| + const string16& storage_path, |
| + base::SequencedTaskRunner* task_runner, |
| + MTPDeviceDelegateImplWin* device_delegate, |
| + base::WaitableEvent* task_completed_event) |
| + : storage_path_(storage_path), |
| + media_task_runner_(task_runner), |
| + device_delegate_(device_delegate), |
| + on_task_completed_event_(task_completed_event), |
| + result_(false) { |
| + DCHECK(!storage_path_.empty()); |
| + DCHECK(media_task_runner_.get()); |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + DCHECK(device_delegate_); |
| +} |
| + |
| +void MTPGetStorageInfoWorker::Run() { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&MTPGetStorageInfoWorker::DoWorkOnUIThread, this)); |
| + on_task_completed_event_->Wait(); |
| + if (device_delegate_->IsCancelTasksFlagSet()) |
| + cancel_tasks_flag_.Set(); |
| +} |
| + |
| +bool MTPGetStorageInfoWorker::GetDeviceStorageInfo( |
| + string16* pnp_device_id, |
| + string16* storage_object_id) const { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| + if (cancel_tasks_flag_.IsSet()) |
| + return false; |
| + DCHECK(pnp_device_id); |
| + DCHECK(storage_object_id); |
| + *pnp_device_id = pnp_device_id_; |
| + *storage_object_id = storage_object_id_; |
| + return result_; |
| +} |
| + |
| +MTPGetStorageInfoWorker::~MTPGetStorageInfoWorker() { |
| + DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| +} |
| + |
| +void MTPGetStorageInfoWorker::DoWorkOnUIThread() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + if (cancel_tasks_flag_.IsSet()) |
| + return; |
| + string16 storage_device_id; |
| + RemoveChars(storage_path_, L"\\\\", &storage_device_id); |
| + DCHECK(!storage_device_id.empty()); |
| + RemovableStorageNotifications* notifications = |
| + RemovableStorageNotifications::GetInstance(); |
| + DCHECK(notifications); |
| + result_ = notifications->GetMTPStorageInfoFromDeviceId( |
| + UTF16ToUTF8(storage_device_id), &pnp_device_id_, &storage_object_id_); |
| + if (!cancel_tasks_flag_.IsSet()) |
| + on_task_completed_event_->Signal(); |
| +} |
| + |
| +// static |
| +void MTPGetStorageInfoWorkerDeleter::Destruct( |
| + const MTPGetStorageInfoWorker* worker) { |
| + if (!worker->media_task_runner()->RunsTasksOnCurrentThread()) { |
| + worker->media_task_runner()->DeleteSoon(FROM_HERE, worker); |
|
Ryan Sleevi
2013/01/05 03:00:58
LEAK: If DeleteSoon fails to post a task, you'll e
Peter Kasting
2013/01/05 03:17:19
But if it fails to post a task, how do we know? A
Ryan Sleevi
2013/01/05 03:44:37
blergh, DeleteSoon seems to be swallowing the resu
|
| + return; |
| + } |
| + delete worker; |
| +} |
| + |
| +} // namespace chrome |