[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / storage_monitor / storage_monitor.cc
1 // Copyright 2014 The Chromium Authors
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/storage_monitor.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/containers/contains.h"
11 #include "base/logging.h"
12 #include "base/memory/raw_ptr.h"
13 #include "components/storage_monitor/removable_storage_observer.h"
14 #include "components/storage_monitor/transient_device_ids.h"
15
16 namespace storage_monitor {
17
18 namespace {
19
20 StorageMonitor* g_storage_monitor = nullptr;
21
22 }  // namespace
23
24 StorageMonitor::Receiver::~Receiver() = default;
25
26 class StorageMonitor::ReceiverImpl : public StorageMonitor::Receiver {
27  public:
28   explicit ReceiverImpl(StorageMonitor* notifications)
29       : notifications_(notifications) {}
30
31   ~ReceiverImpl() override = default;
32
33   void ProcessAttach(const StorageInfo& info) override;
34
35   void ProcessDetach(const std::string& id) override;
36
37   void MarkInitialized() override;
38
39  private:
40   raw_ptr<StorageMonitor> notifications_;
41 };
42
43 void StorageMonitor::ReceiverImpl::ProcessAttach(const StorageInfo& info) {
44   notifications_->ProcessAttach(info);
45 }
46
47 void StorageMonitor::ReceiverImpl::ProcessDetach(const std::string& id) {
48   notifications_->ProcessDetach(id);
49 }
50
51 void StorageMonitor::ReceiverImpl::MarkInitialized() {
52   notifications_->MarkInitialized();
53 }
54
55 // static
56 void StorageMonitor::Create() {
57   delete g_storage_monitor;
58   g_storage_monitor = CreateInternal();
59 }
60
61 // static
62 void StorageMonitor::Destroy() {
63   delete g_storage_monitor;
64   g_storage_monitor = nullptr;
65 }
66
67 StorageMonitor* StorageMonitor::GetInstance() {
68   return g_storage_monitor;
69 }
70
71 void StorageMonitor::SetStorageMonitorForTesting(
72     std::unique_ptr<StorageMonitor> storage_monitor) {
73   delete g_storage_monitor;
74   g_storage_monitor = storage_monitor.release();
75 }
76
77 std::vector<StorageInfo> StorageMonitor::GetAllAvailableStorages() const {
78   std::vector<StorageInfo> results;
79
80   base::AutoLock lock(storage_lock_);
81   for (const auto& item : storage_map_) {
82     results.push_back(item.second);
83   }
84   return results;
85 }
86
87 void StorageMonitor::EnsureInitialized(base::OnceClosure callback) {
88   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
89   if (initialized_) {
90     if (!callback.is_null())
91       std::move(callback).Run();
92     return;
93   }
94
95   if (!callback.is_null()) {
96     on_initialize_callbacks_.push_back(std::move(callback));
97   }
98
99   if (initializing_)
100     return;
101
102   initializing_ = true;
103   Init();
104 }
105
106 bool StorageMonitor::IsInitialized() const {
107   return initialized_;
108 }
109
110 void StorageMonitor::AddObserver(RemovableStorageObserver* obs) {
111   observer_list_->AddObserver(obs);
112 }
113
114 void StorageMonitor::RemoveObserver(
115     RemovableStorageObserver* obs) {
116   observer_list_->RemoveObserver(obs);
117 }
118
119 std::string StorageMonitor::GetTransientIdForDeviceId(
120     const std::string& device_id) {
121   return transient_device_ids_->GetTransientIdForDeviceId(device_id);
122 }
123
124 std::string StorageMonitor::GetDeviceIdForTransientId(
125     const std::string& transient_id) const {
126   return transient_device_ids_->DeviceIdFromTransientId(transient_id);
127 }
128
129 void StorageMonitor::EjectDevice(
130     const std::string& device_id,
131     base::OnceCallback<void(EjectStatus)> callback) {
132   // Platform-specific implementations will override this method to
133   // perform actual device ejection.
134   std::move(callback).Run(EJECT_FAILURE);
135 }
136
137 StorageMonitor::StorageMonitor()
138     : observer_list_(
139           new base::ObserverListThreadSafe<RemovableStorageObserver>()),
140       initializing_(false),
141       initialized_(false),
142       transient_device_ids_(new TransientDeviceIds) {
143   receiver_ = std::make_unique<ReceiverImpl>(this);
144 }
145
146 StorageMonitor::~StorageMonitor() = default;
147
148 StorageMonitor::Receiver* StorageMonitor::receiver() const {
149   return receiver_.get();
150 }
151
152 void StorageMonitor::MarkInitialized() {
153   initialized_ = true;
154   for (auto& callback : on_initialize_callbacks_)
155     std::move(callback).Run();
156   on_initialize_callbacks_.clear();
157 }
158
159 void StorageMonitor::ProcessAttach(const StorageInfo& info) {
160   {
161     base::AutoLock lock(storage_lock_);
162     if (base::Contains(storage_map_, info.device_id())) {
163       // This can happen if our unique id scheme fails. Ignore the incoming
164       // non-unique attachment.
165       return;
166     }
167     storage_map_.insert(std::make_pair(info.device_id(), info));
168   }
169
170   DVLOG(1) << "StorageAttached id " << info.device_id();
171   if (StorageInfo::IsRemovableDevice(info.device_id())) {
172     observer_list_->Notify(
173         FROM_HERE, &RemovableStorageObserver::OnRemovableStorageAttached, info);
174   }
175 }
176
177 void StorageMonitor::ProcessDetach(const std::string& id) {
178   StorageInfo info;
179   {
180     base::AutoLock lock(storage_lock_);
181     auto it = storage_map_.find(id);
182     if (it == storage_map_.end())
183       return;
184     info = it->second;
185     storage_map_.erase(it);
186   }
187
188   DVLOG(1) << "StorageDetached for id " << id;
189   if (StorageInfo::IsRemovableDevice(info.device_id())) {
190     observer_list_->Notify(
191         FROM_HERE, &RemovableStorageObserver::OnRemovableStorageDetached, info);
192   }
193 }
194
195 }  // namespace storage_monitor