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 TestStorageMonitor::TestStorageMonitor() 20 : StorageMonitor(), 21 init_called_(false) { 22 #if defined(OS_LINUX) 23 media_transfer_protocol_manager_.reset( 24 new TestMediaTransferProtocolManagerLinux()); 25 #endif 26 } 27 28 TestStorageMonitor::~TestStorageMonitor() {} 29 30 // static 31 TestStorageMonitor* TestStorageMonitor::CreateAndInstall() { 32 TestStorageMonitor* monitor = new TestStorageMonitor(); 33 scoped_ptr<StorageMonitor> pass_monitor(monitor); 34 monitor->Init(); 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()); 40 return monitor; 41 } 42 return NULL; 43 } 44 45 // static 46 TestStorageMonitor* TestStorageMonitor::CreateForBrowserTests() { 47 TestStorageMonitor* return_monitor = new TestStorageMonitor(); 48 return_monitor->Init(); 49 return_monitor->MarkInitialized(); 50 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; 57 } 58 59 // static 60 void TestStorageMonitor::RemoveSingleton() { 61 TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal(); 62 if (browser_process) 63 browser_process->SetStorageMonitor(scoped_ptr<StorageMonitor>()); 64 } 65 66 // static 67 void TestStorageMonitor::SyncInitialize() { 68 StorageMonitor* monitor = g_browser_process->storage_monitor(); 69 if (monitor->IsInitialized()) 70 return; 71 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(); 77 } 78 DCHECK(monitor->IsInitialized()); 79 } 80 81 void TestStorageMonitor::Init() { 82 init_called_ = true; 83 } 84 85 void TestStorageMonitor::MarkInitialized() { 86 StorageMonitor::MarkInitialized(); 87 } 88 89 bool TestStorageMonitor::GetStorageInfoForPath( 90 const base::FilePath& path, 91 StorageInfo* device_info) const { 92 DCHECK(device_info); 93 94 if (!path.IsAbsolute()) 95 return false; 96 97 std::string device_id = StorageInfo::MakeDeviceId( 98 StorageInfo::FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); 99 *device_info = 100 StorageInfo(device_id, path.BaseName().LossyDisplayName(), path.value(), 101 base::string16(), base::string16(), base::string16(), 0); 102 return true; 103 } 104 105 #if defined(OS_WIN) 106 bool TestStorageMonitor::GetMTPStorageInfoFromDeviceId( 107 const std::string& storage_device_id, 108 base::string16* device_location, 109 base::string16* storage_object_id) const { 110 return false; 111 } 112 #endif 113 114 #if defined(OS_LINUX) 115 device::MediaTransferProtocolManager* 116 TestStorageMonitor::media_transfer_protocol_manager() { 117 return media_transfer_protocol_manager_.get(); 118 } 119 #endif 120 121 StorageMonitor::Receiver* TestStorageMonitor::receiver() const { 122 return StorageMonitor::receiver(); 123 } 124 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); 130 } 131