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
3 // found in the LICENSE file.
5 #include "ppapi/shared_impl/ppapi_globals.h"
7 #include "base/lazy_instance.h" // For testing purposes only.
8 #include "base/logging.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/threading/thread_local.h" // For testing purposes only.
16 // Thread-local globals for testing. See SetPpapiGlobalsOnThreadForTest for more
18 base::LazyInstance
<base::ThreadLocalPointer
<PpapiGlobals
> >::Leaky
19 tls_ppapi_globals_for_test
= LAZY_INSTANCE_INITIALIZER
;
22 PpapiGlobals
* ppapi_globals
= NULL
;
24 PpapiGlobals::PpapiGlobals() {
25 DCHECK(!ppapi_globals
);
27 main_task_runner_
= base::ThreadTaskRunnerHandle::Get();
30 PpapiGlobals::PpapiGlobals(PerThreadForTest
) {
31 DCHECK(!ppapi_globals
);
32 main_task_runner_
= base::ThreadTaskRunnerHandle::Get();
35 PpapiGlobals::~PpapiGlobals() {
36 DCHECK(ppapi_globals
== this || !ppapi_globals
);
40 // Static Getter for the global singleton.
41 PpapiGlobals
* PpapiGlobals::Get() {
44 // In unit tests, the following might be valid (see
45 // SetPpapiGlobalsOnThreadForTest). Normally, this will just return NULL.
46 return GetThreadLocalPointer();
50 void PpapiGlobals::SetPpapiGlobalsOnThreadForTest(PpapiGlobals
* ptr
) {
51 // If we're using a per-thread PpapiGlobals, we should not have a global one.
52 // If we allowed it, it would always over-ride the "test" versions.
53 DCHECK(!ppapi_globals
);
54 tls_ppapi_globals_for_test
.Pointer()->Set(ptr
);
57 base::SingleThreadTaskRunner
* PpapiGlobals::GetMainThreadMessageLoop() {
58 return main_task_runner_
.get();
61 void PpapiGlobals::ResetMainThreadMessageLoopForTesting() {
62 main_task_runner_
= base::ThreadTaskRunnerHandle::Get();
65 bool PpapiGlobals::IsHostGlobals() const { return false; }
67 bool PpapiGlobals::IsPluginGlobals() const { return false; }
69 void PpapiGlobals::MarkPluginIsActive() {}
71 void PpapiGlobals::AddLatencyInfo(const ui::LatencyInfo
& latency_info
,
72 PP_Instance instance
) {
73 latency_info_for_frame_
[instance
].push_back(latency_info
);
76 void PpapiGlobals::TransferLatencyInfoTo(
77 std::vector
<ui::LatencyInfo
>* latency_info
, PP_Instance instance
) {
78 latency_info
->swap(latency_info_for_frame_
[instance
]);
79 latency_info_for_frame_
.erase(instance
);
83 PpapiGlobals
* PpapiGlobals::GetThreadLocalPointer() {
84 return tls_ppapi_globals_for_test
.Pointer()->Get();