Popular sites on the NTP: Try to keep the ordering constant
[chromium-blink-merge.git] / ppapi / shared_impl / ppapi_globals.cc
blob08cdbca34d93dabf602b7f37a868d18e1f3885da
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.
13 namespace ppapi {
15 namespace {
16 // Thread-local globals for testing. See SetPpapiGlobalsOnThreadForTest for more
17 // information.
18 base::LazyInstance<base::ThreadLocalPointer<PpapiGlobals> >::Leaky
19 tls_ppapi_globals_for_test = LAZY_INSTANCE_INITIALIZER;
20 } // namespace
22 PpapiGlobals* ppapi_globals = NULL;
24 PpapiGlobals::PpapiGlobals() {
25 DCHECK(!ppapi_globals);
26 ppapi_globals = this;
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);
37 ppapi_globals = NULL;
40 // Static Getter for the global singleton.
41 PpapiGlobals* PpapiGlobals::Get() {
42 if (ppapi_globals)
43 return ppapi_globals;
44 // In unit tests, the following might be valid (see
45 // SetPpapiGlobalsOnThreadForTest). Normally, this will just return NULL.
46 return GetThreadLocalPointer();
49 // static
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 // static
72 PpapiGlobals* PpapiGlobals::GetThreadLocalPointer() {
73 return tls_ppapi_globals_for_test.Pointer()->Get();
76 } // namespace ppapi