ppapi_globals.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#include "ppapi/shared_impl/ppapi_globals.h"
6
7#include "base/lazy_instance.h"  // For testing purposes only.
8#include "base/logging.h"
9#include "base/message_loop/message_loop_proxy.h"
10#include "base/threading/thread_local.h"  // For testing purposes only.
11
12namespace ppapi {
13
14namespace {
15// Thread-local globals for testing. See SetPpapiGlobalsOnThreadForTest for more
16// information.
17base::LazyInstance<base::ThreadLocalPointer<PpapiGlobals> >::Leaky
18    tls_ppapi_globals_for_test = LAZY_INSTANCE_INITIALIZER;
19}  // namespace
20
21PpapiGlobals* ppapi_globals = NULL;
22
23PpapiGlobals::PpapiGlobals() {
24  DCHECK(!ppapi_globals);
25  ppapi_globals = this;
26  main_loop_proxy_ = base::MessageLoopProxy::current();
27}
28
29PpapiGlobals::PpapiGlobals(PerThreadForTest) {
30  DCHECK(!ppapi_globals);
31  main_loop_proxy_ = base::MessageLoopProxy::current();
32}
33
34PpapiGlobals::~PpapiGlobals() {
35  DCHECK(ppapi_globals == this || !ppapi_globals);
36  ppapi_globals = NULL;
37}
38
39// Static Getter for the global singleton.
40PpapiGlobals* PpapiGlobals::Get() {
41  if (ppapi_globals)
42    return ppapi_globals;
43  // In unit tests, the following might be valid (see
44  // SetPpapiGlobalsOnThreadForTest). Normally, this will just return NULL.
45  return GetThreadLocalPointer();
46}
47
48// static
49void PpapiGlobals::SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr) {
50  // 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  DCHECK(!ppapi_globals);
53  tls_ppapi_globals_for_test.Pointer()->Set(ptr);
54}
55
56base::MessageLoopProxy* PpapiGlobals::GetMainThreadMessageLoop() {
57  return main_loop_proxy_.get();
58}
59
60void PpapiGlobals::ResetMainThreadMessageLoopForTesting() {
61  main_loop_proxy_ = base::MessageLoopProxy::current();
62}
63
64bool PpapiGlobals::IsHostGlobals() const { return false; }
65
66bool PpapiGlobals::IsPluginGlobals() const { return false; }
67
68void PpapiGlobals::MarkPluginIsActive() {}
69
70// static
71PpapiGlobals* PpapiGlobals::GetThreadLocalPointer() {
72  return tls_ppapi_globals_for_test.Pointer()->Get();
73}
74
75}  // namespace ppapi
76