1 // Copyright (c) 2013 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.
5 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
7 #include "base/run_loop.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/browser_process_impl.h"
11 #include "chrome/browser/storage_monitor/storage_info.h"
12 #include "chrome/test/base/testing_browser_process.h"
15 #include "chrome/browser/storage_monitor/test_media_transfer_protocol_manager_linux.h"
16 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
19 TestStorageMonitor::TestStorageMonitor()
23 media_transfer_protocol_manager_
.reset(
24 new TestMediaTransferProtocolManagerLinux());
28 TestStorageMonitor::~TestStorageMonitor() {}
31 TestStorageMonitor
* TestStorageMonitor::CreateAndInstall() {
32 TestStorageMonitor
* monitor
= new TestStorageMonitor();
33 scoped_ptr
<StorageMonitor
> pass_monitor(monitor
);
35 monitor
->MarkInitialized();
36 TestingBrowserProcess
* browser_process
= TestingBrowserProcess::GetGlobal();
37 if (browser_process
) {
38 DCHECK(browser_process
->storage_monitor() == NULL
);
39 browser_process
->SetStorageMonitor(pass_monitor
.Pass());
46 TestStorageMonitor
* TestStorageMonitor::CreateForBrowserTests() {
47 TestStorageMonitor
* return_monitor
= new TestStorageMonitor();
48 return_monitor
->Init();
49 return_monitor
->MarkInitialized();
51 scoped_ptr
<StorageMonitor
> monitor(return_monitor
);
52 BrowserProcessImpl
* browser_process
=
53 static_cast<BrowserProcessImpl
*>(g_browser_process
);
54 DCHECK(browser_process
);
55 browser_process
->set_storage_monitor_for_test(monitor
.Pass());
56 return return_monitor
;
60 void TestStorageMonitor::RemoveSingleton() {
61 TestingBrowserProcess
* browser_process
= TestingBrowserProcess::GetGlobal();
63 browser_process
->SetStorageMonitor(scoped_ptr
<StorageMonitor
>());
67 void TestStorageMonitor::SyncInitialize() {
68 StorageMonitor
* monitor
= g_browser_process
->storage_monitor();
69 if (monitor
->IsInitialized())
72 base::WaitableEvent
event(true, false);
73 monitor
->EnsureInitialized(base::Bind(&base::WaitableEvent::Signal
,
74 base::Unretained(&event
)));
75 while (!event
.IsSignaled()) {
76 base::RunLoop().RunUntilIdle();
78 DCHECK(monitor
->IsInitialized());
81 void TestStorageMonitor::Init() {
85 void TestStorageMonitor::MarkInitialized() {
86 StorageMonitor::MarkInitialized();
89 bool TestStorageMonitor::GetStorageInfoForPath(
90 const base::FilePath
& path
,
91 StorageInfo
* device_info
) const {
94 if (!path
.IsAbsolute())
97 std::string device_id
= StorageInfo::MakeDeviceId(
98 StorageInfo::FIXED_MASS_STORAGE
, path
.AsUTF8Unsafe());
100 StorageInfo(device_id
, path
.BaseName().LossyDisplayName(), path
.value(),
101 base::string16(), base::string16(), base::string16(), 0);
106 bool TestStorageMonitor::GetMTPStorageInfoFromDeviceId(
107 const std::string
& storage_device_id
,
108 base::string16
* device_location
,
109 base::string16
* storage_object_id
) const {
114 #if defined(OS_LINUX)
115 device::MediaTransferProtocolManager
*
116 TestStorageMonitor::media_transfer_protocol_manager() {
117 return media_transfer_protocol_manager_
.get();
121 StorageMonitor::Receiver
* TestStorageMonitor::receiver() const {
122 return StorageMonitor::receiver();
125 void TestStorageMonitor::EjectDevice(
126 const std::string
& device_id
,
127 base::Callback
<void(EjectStatus
)> callback
) {
128 ejected_device_
= device_id
;
129 callback
.Run(EJECT_OK
);