| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ppapi/shared_impl/ppapi_globals.h" | 5 #include "ppapi/shared_impl/ppapi_globals.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" // For testing purposes only. | 7 #include "base/lazy_instance.h" // For testing purposes only. |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 10 #include "base/threading/thread_local.h" // For testing purposes only. | 11 #include "base/threading/thread_local.h" // For testing purposes only. |
| 11 | 12 |
| 12 namespace ppapi { | 13 namespace ppapi { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 // Thread-local globals for testing. See SetPpapiGlobalsOnThreadForTest for more | 16 // Thread-local globals for testing. See SetPpapiGlobalsOnThreadForTest for more |
| 16 // information. | 17 // information. |
| 17 base::LazyInstance<base::ThreadLocalPointer<PpapiGlobals> >::Leaky | 18 base::LazyInstance<base::ThreadLocalPointer<PpapiGlobals> >::Leaky |
| 18 tls_ppapi_globals_for_test = LAZY_INSTANCE_INITIALIZER; | 19 tls_ppapi_globals_for_test = LAZY_INSTANCE_INITIALIZER; |
| 19 } // namespace | 20 } // namespace |
| 20 | 21 |
| 21 PpapiGlobals* ppapi_globals = NULL; | 22 PpapiGlobals* ppapi_globals = NULL; |
| 22 | 23 |
| 23 PpapiGlobals::PpapiGlobals() { | 24 PpapiGlobals::PpapiGlobals() { |
| 24 DCHECK(!ppapi_globals); | 25 DCHECK(!ppapi_globals); |
| 25 ppapi_globals = this; | 26 ppapi_globals = this; |
| 26 main_loop_proxy_ = base::MessageLoopProxy::current(); | 27 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 27 } | 28 } |
| 28 | 29 |
| 29 PpapiGlobals::PpapiGlobals(PerThreadForTest) { | 30 PpapiGlobals::PpapiGlobals(PerThreadForTest) { |
| 30 DCHECK(!ppapi_globals); | 31 DCHECK(!ppapi_globals); |
| 31 main_loop_proxy_ = base::MessageLoopProxy::current(); | 32 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 32 } | 33 } |
| 33 | 34 |
| 34 PpapiGlobals::~PpapiGlobals() { | 35 PpapiGlobals::~PpapiGlobals() { |
| 35 DCHECK(ppapi_globals == this || !ppapi_globals); | 36 DCHECK(ppapi_globals == this || !ppapi_globals); |
| 36 ppapi_globals = NULL; | 37 ppapi_globals = NULL; |
| 37 } | 38 } |
| 38 | 39 |
| 39 // Static Getter for the global singleton. | 40 // Static Getter for the global singleton. |
| 40 PpapiGlobals* PpapiGlobals::Get() { | 41 PpapiGlobals* PpapiGlobals::Get() { |
| 41 if (ppapi_globals) | 42 if (ppapi_globals) |
| 42 return ppapi_globals; | 43 return ppapi_globals; |
| 43 // In unit tests, the following might be valid (see | 44 // In unit tests, the following might be valid (see |
| 44 // SetPpapiGlobalsOnThreadForTest). Normally, this will just return NULL. | 45 // SetPpapiGlobalsOnThreadForTest). Normally, this will just return NULL. |
| 45 return GetThreadLocalPointer(); | 46 return GetThreadLocalPointer(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 // static | 49 // static |
| 49 void PpapiGlobals::SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr) { | 50 void PpapiGlobals::SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr) { |
| 50 // If we're using a per-thread PpapiGlobals, we should not have a global one. | 51 // If we're using a per-thread PpapiGlobals, we should not have a global one. |
| 51 // If we allowed it, it would always over-ride the "test" versions. | 52 // If we allowed it, it would always over-ride the "test" versions. |
| 52 DCHECK(!ppapi_globals); | 53 DCHECK(!ppapi_globals); |
| 53 tls_ppapi_globals_for_test.Pointer()->Set(ptr); | 54 tls_ppapi_globals_for_test.Pointer()->Set(ptr); |
| 54 } | 55 } |
| 55 | 56 |
| 56 base::MessageLoopProxy* PpapiGlobals::GetMainThreadMessageLoop() { | 57 base::SingleThreadTaskRunner* PpapiGlobals::GetMainThreadMessageLoop() { |
| 57 return main_loop_proxy_.get(); | 58 return main_task_runner_.get(); |
| 58 } | 59 } |
| 59 | 60 |
| 60 void PpapiGlobals::ResetMainThreadMessageLoopForTesting() { | 61 void PpapiGlobals::ResetMainThreadMessageLoopForTesting() { |
| 61 main_loop_proxy_ = base::MessageLoopProxy::current(); | 62 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 62 } | 63 } |
| 63 | 64 |
| 64 bool PpapiGlobals::IsHostGlobals() const { return false; } | 65 bool PpapiGlobals::IsHostGlobals() const { return false; } |
| 65 | 66 |
| 66 bool PpapiGlobals::IsPluginGlobals() const { return false; } | 67 bool PpapiGlobals::IsPluginGlobals() const { return false; } |
| 67 | 68 |
| 68 void PpapiGlobals::MarkPluginIsActive() {} | 69 void PpapiGlobals::MarkPluginIsActive() {} |
| 69 | 70 |
| 70 void PpapiGlobals::AddLatencyInfo(const ui::LatencyInfo& latency_info, | 71 void PpapiGlobals::AddLatencyInfo(const ui::LatencyInfo& latency_info, |
| 71 PP_Instance instance) { | 72 PP_Instance instance) { |
| 72 latency_info_for_frame_[instance].push_back(latency_info); | 73 latency_info_for_frame_[instance].push_back(latency_info); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void PpapiGlobals::TransferLatencyInfoTo( | 76 void PpapiGlobals::TransferLatencyInfoTo( |
| 76 std::vector<ui::LatencyInfo>* latency_info, PP_Instance instance) { | 77 std::vector<ui::LatencyInfo>* latency_info, PP_Instance instance) { |
| 77 latency_info->swap(latency_info_for_frame_[instance]); | 78 latency_info->swap(latency_info_for_frame_[instance]); |
| 78 latency_info_for_frame_.erase(instance); | 79 latency_info_for_frame_.erase(instance); |
| 79 } | 80 } |
| 80 | 81 |
| 81 // static | 82 // static |
| 82 PpapiGlobals* PpapiGlobals::GetThreadLocalPointer() { | 83 PpapiGlobals* PpapiGlobals::GetThreadLocalPointer() { |
| 83 return tls_ppapi_globals_for_test.Pointer()->Get(); | 84 return tls_ppapi_globals_for_test.Pointer()->Get(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 } // namespace ppapi | 87 } // namespace ppapi |
| OLD | NEW |