| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gin/public/v8_platform.h" | 5 #include "gin/public/v8_platform.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
| 10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 120 |
| 121 void V8Platform::UpdateTraceEventDuration(const uint8_t* category_enabled_flag, | 121 void V8Platform::UpdateTraceEventDuration(const uint8_t* category_enabled_flag, |
| 122 const char* name, | 122 const char* name, |
| 123 uint64_t handle) { | 123 uint64_t handle) { |
| 124 base::trace_event::TraceEventHandle traceEventHandle; | 124 base::trace_event::TraceEventHandle traceEventHandle; |
| 125 memcpy(&traceEventHandle, &handle, sizeof(handle)); | 125 memcpy(&traceEventHandle, &handle, sizeof(handle)); |
| 126 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_enabled_flag, name, | 126 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_enabled_flag, name, |
| 127 traceEventHandle); | 127 traceEventHandle); |
| 128 } | 128 } |
| 129 | 129 |
| 130 namespace { |
| 131 |
| 132 class EnabledStateObserverImpl final |
| 133 : public base::trace_event::TraceLog::EnabledStateObserver { |
| 134 public: |
| 135 EnabledStateObserverImpl() = default; |
| 136 |
| 137 void OnTraceLogEnabled() final { |
| 138 base::AutoLock lock(mutex_); |
| 139 for (auto o : observers_) { |
| 140 o->OnTraceEnabled(); |
| 141 } |
| 142 } |
| 143 |
| 144 void OnTraceLogDisabled() final { |
| 145 base::AutoLock lock(mutex_); |
| 146 for (auto o : observers_) { |
| 147 o->OnTraceDisabled(); |
| 148 } |
| 149 } |
| 150 |
| 151 void AddObserver(v8::Platform::TraceStateObserver* observer) { |
| 152 base::AutoLock lock(mutex_); |
| 153 DCHECK(!observers_.count(observer)); |
| 154 observers_.insert(observer); |
| 155 if (observers_.size() == 1) { |
| 156 base::trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| 157 } |
| 158 } |
| 159 |
| 160 void RemoveObserver(v8::Platform::TraceStateObserver* observer) { |
| 161 base::AutoLock lock(mutex_); |
| 162 DCHECK(observers_.count(observer) == 1); |
| 163 observers_.erase(observer); |
| 164 if (observers_.empty()) { |
| 165 base::trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver( |
| 166 this); |
| 167 } |
| 168 } |
| 169 |
| 170 private: |
| 171 base::Lock mutex_; |
| 172 std::unordered_set<v8::Platform::TraceStateObserver*> observers_; |
| 173 |
| 174 DISALLOW_COPY_AND_ASSIGN(EnabledStateObserverImpl); |
| 175 }; |
| 176 |
| 177 base::LazyInstance<EnabledStateObserverImpl>::Leaky g_trace_state_dispatcher = |
| 178 LAZY_INSTANCE_INITIALIZER; |
| 179 |
| 180 } // namespace |
| 181 |
| 182 void V8Platform::AddTraceStateObserver( |
| 183 v8::Platform::TraceStateObserver* observer) { |
| 184 g_trace_state_dispatcher.Get().AddObserver(observer); |
| 185 } |
| 186 |
| 187 void V8Platform::RemoveTraceStateObserver( |
| 188 v8::Platform::TraceStateObserver* observer) { |
| 189 g_trace_state_dispatcher.Get().RemoveObserver(observer); |
| 190 } |
| 191 |
| 130 } // namespace gin | 192 } // namespace gin |
| OLD | NEW |