Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / storage_monitor / test_storage_monitor.cc
1 // Copyright 2014 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 "components/storage_monitor/test_storage_monitor.h"
6
7 #include "base/run_loop.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "components/storage_monitor/storage_info.h"
10
11 #if defined(OS_LINUX)
12 #include "components/storage_monitor/test_media_transfer_protocol_manager_linux.h"
13 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
14 #endif
15
16 TestStorageMonitor::TestStorageMonitor()
17     : StorageMonitor(),
18       init_called_(false) {
19 #if defined(OS_LINUX)
20   media_transfer_protocol_manager_.reset(
21       new TestMediaTransferProtocolManagerLinux());
22 #endif
23 }
24
25 TestStorageMonitor::~TestStorageMonitor() {}
26
27 // static
28 TestStorageMonitor* TestStorageMonitor::CreateAndInstall() {
29   TestStorageMonitor* monitor = new TestStorageMonitor();
30   scoped_ptr<StorageMonitor> pass_monitor(monitor);
31   monitor->Init();
32   monitor->MarkInitialized();
33
34   if (StorageMonitor::GetInstance() == NULL) {
35     StorageMonitor::SetStorageMonitorForTesting(pass_monitor.Pass());
36     return monitor;
37   }
38
39   return NULL;
40 }
41
42 // static
43 TestStorageMonitor* TestStorageMonitor::CreateForBrowserTests() {
44   TestStorageMonitor* monitor = new TestStorageMonitor();
45   monitor->Init();
46   monitor->MarkInitialized();
47
48   scoped_ptr<StorageMonitor> pass_monitor(monitor);
49   StorageMonitor::SetStorageMonitorForTesting(pass_monitor.Pass());
50
51   return monitor;
52 }
53
54 // static
55 void TestStorageMonitor::SyncInitialize() {
56   StorageMonitor* monitor = StorageMonitor::GetInstance();
57   if (monitor->IsInitialized())
58     return;
59
60   base::WaitableEvent event(true, false);
61   monitor->EnsureInitialized(base::Bind(&base::WaitableEvent::Signal,
62                              base::Unretained(&event)));
63   while (!event.IsSignaled()) {
64     base::RunLoop().RunUntilIdle();
65   }
66   DCHECK(monitor->IsInitialized());
67 }
68
69 void TestStorageMonitor::Init() {
70   init_called_ = true;
71 }
72
73 void TestStorageMonitor::MarkInitialized() {
74   StorageMonitor::MarkInitialized();
75 }
76
77 bool TestStorageMonitor::GetStorageInfoForPath(
78     const base::FilePath& path,
79     StorageInfo* device_info) const {
80   DCHECK(device_info);
81
82   if (!path.IsAbsolute())
83     return false;
84
85   std::string device_id = StorageInfo::MakeDeviceId(
86       StorageInfo::FIXED_MASS_STORAGE, path.AsUTF8Unsafe());
87   *device_info =
88       StorageInfo(device_id, path.BaseName().LossyDisplayName(), path.value(),
89                   base::string16(), base::string16(), base::string16(), 0);
90   return true;
91 }
92
93 #if defined(OS_WIN)
94 bool TestStorageMonitor::GetMTPStorageInfoFromDeviceId(
95     const std::string& storage_device_id,
96     base::string16* device_location,
97     base::string16* storage_object_id) const {
98   return false;
99 }
100 #endif
101
102 #if defined(OS_LINUX)
103 device::MediaTransferProtocolManager*
104 TestStorageMonitor::media_transfer_protocol_manager() {
105   return media_transfer_protocol_manager_.get();
106 }
107 #endif
108
109 StorageMonitor::Receiver* TestStorageMonitor::receiver() const {
110   return StorageMonitor::receiver();
111 }
112
113 void TestStorageMonitor::EjectDevice(
114     const std::string& device_id,
115     base::Callback<void(EjectStatus)> callback) {
116   ejected_device_ = device_id;
117   callback.Run(EJECT_OK);
118 }