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. 4 5 #include "chrome/browser/storage_monitor/test_storage_monitor.h" 6 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" 13 14 #if defined(OS_LINUX) 15 #include "chrome/browser/storage_monitor/test_media_transfer_protocol_manager_linux.h" 16 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h" 17 #endif 18 19 namespace chrome { 20 namespace test { 21 22 TestStorageMonitor::TestStorageMonitor() 23 : StorageMonitor(), 24 init_called_(false) { 25 #if defined(OS_LINUX) 26 media_transfer_protocol_manager_.reset( 27 new TestMediaTransferProtocolManagerLinux()); 28 #endif 29 } 30 31 TestStorageMonitor::~TestStorageMonitor() {} 32 33 // static 34 TestStorageMonitor* TestStorageMonitor::CreateAndInstall() { 35 RemoveSingleton(); 36 TestStorageMonitor* monitor = new TestStorageMonitor(); 37 scoped_ptr<StorageMonitor> pass_monitor(monitor); 38 monitor->Init(); 39 monitor->MarkInitialized(); 40 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); 41 if (browser_process) { 42 browser_process->SetStorageMonitor(pass_monitor.Pass()); 43 return monitor; 44 } 45 return NULL; 46 } 47 48 // static 49 TestStorageMonitor* TestStorageMonitor::CreateForBrowserTests() { 50 TestStorageMonitor* return_monitor = new TestStorageMonitor(); 51 return_monitor->Init(); 52 return_monitor->MarkInitialized(); 53 54 scoped_ptr<StorageMonitor> monitor(return_monitor); 55 BrowserProcessImpl* browser_process = 56 static_cast<BrowserProcessImpl*>(g_browser_process); 57 DCHECK(browser_process); 58 browser_process->set_storage_monitor_for_test(monitor.Pass()); 59 return return_monitor; 60 } 61 62 // static 63 void TestStorageMonitor::RemoveSingleton() { 64 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); 65 if (browser_process) 66 browser_process->SetStorageMonitor(scoped_ptr<StorageMonitor>()); 67 } 68 69 // static 70 void TestStorageMonitor::SyncInitialize() { 71 StorageMonitor* monitor = g_browser_process->storage_monitor(); 72 if (monitor->IsInitialized()) 73 return; 74 75 base::WaitableEvent event(true, false); 76 monitor->EnsureInitialized(base::Bind(&base::WaitableEvent::Signal, 77 base::Unretained(&event))); 78 while (!event.IsSignaled()) { 79 base::RunLoop().RunUntilIdle(); 80 } 81 DCHECK(monitor->IsInitialized()); 82 } 83 84 void TestStorageMonitor::Init() { 85 init_called_ = true; 86 } 87 88 void TestStorageMonitor::MarkInitialized() { 89 StorageMonitor::MarkInitialized(); 90 } 91 92 bool TestStorageMonitor::GetStorageInfoForPath( 93 const base::FilePath& path, 94 StorageInfo* device_info) const { 95 DCHECK(device_info); 96 97 if (!path.IsAbsolute()) 98 return false; 99 100 std::string device_id = StorageInfo::MakeDeviceId( 101 StorageInfo::FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); 102 *device_info = 103 StorageInfo(device_id, path.BaseName().LossyDisplayName(), path.value(), 104 string16(), string16(), string16(), 0); 105 return true; 106 } 107 108 #if defined(OS_WIN) 109 bool TestStorageMonitor::GetMTPStorageInfoFromDeviceId( 110 const std::string& storage_device_id, 111 string16* device_location, 112 string16* storage_object_id) const { 113 return false; 114 } 115 #endif 116 117 #if defined(OS_LINUX) 118 device::MediaTransferProtocolManager* 119 TestStorageMonitor::media_transfer_protocol_manager() { 120 return media_transfer_protocol_manager_.get(); 121 } 122 #endif 123 124 StorageMonitor::Receiver* TestStorageMonitor::receiver() const { 125 return StorageMonitor::receiver(); 126 } 127 128 void TestStorageMonitor::EjectDevice( 129 const std::string& device_id, 130 base::Callback<void(EjectStatus)> callback) { 131 ejected_device_ = device_id; 132 callback.Run(EJECT_OK); 133 } 134 135 } // namespace test 136 } // namespace chrome 137