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_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/threading/thread_local.h" // For testing purposes only. | 10 #include "base/threading/thread_local.h" // For testing purposes only. |
11 | 11 |
12 namespace ppapi { | 12 namespace ppapi { |
13 | 13 |
14 namespace { | 14 namespace { |
15 // Thread-local globals for testing. See SetPpapiGlobalsOnThreadForTest for more | 15 // Thread-local globals for testing. See SetPpapiGlobalsOnThreadForTest for more |
16 // information. | 16 // information. |
17 base::LazyInstance<base::ThreadLocalPointer<PpapiGlobals> >::Leaky | 17 base::LazyInstance<base::ThreadLocalPointer<PpapiGlobals> >::Leaky |
18 tls_ppapi_globals_for_test = LAZY_INSTANCE_INITIALIZER; | 18 tls_ppapi_globals_for_test = LAZY_INSTANCE_INITIALIZER; |
19 } // namespace | 19 } // namespace |
20 | 20 |
21 PpapiGlobals* PpapiGlobals::ppapi_globals_ = NULL; | 21 PpapiGlobals* ppapi_globals = NULL; |
22 | 22 |
23 PpapiGlobals::PpapiGlobals() { | 23 PpapiGlobals::PpapiGlobals() { |
24 DCHECK(!ppapi_globals_); | 24 DCHECK(!ppapi_globals); |
25 ppapi_globals_ = this; | 25 ppapi_globals = this; |
26 main_loop_proxy_ = base::MessageLoopProxy::current(); | 26 main_loop_proxy_ = base::MessageLoopProxy::current(); |
27 } | 27 } |
28 | 28 |
29 PpapiGlobals::PpapiGlobals(PerThreadForTest) { | 29 PpapiGlobals::PpapiGlobals(PerThreadForTest) { |
30 DCHECK(!ppapi_globals_); | 30 DCHECK(!ppapi_globals); |
31 main_loop_proxy_ = base::MessageLoopProxy::current(); | 31 main_loop_proxy_ = base::MessageLoopProxy::current(); |
32 } | 32 } |
33 | 33 |
34 PpapiGlobals::~PpapiGlobals() { | 34 PpapiGlobals::~PpapiGlobals() { |
35 DCHECK(ppapi_globals_ == this || !ppapi_globals_); | 35 DCHECK(ppapi_globals == this || !ppapi_globals); |
36 ppapi_globals_ = NULL; | 36 ppapi_globals = NULL; |
| 37 } |
| 38 |
| 39 //Static Getter for the global singleton. |
| 40 PpapiGlobals* 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(); |
37 } | 46 } |
38 | 47 |
39 // static | 48 // static |
40 void PpapiGlobals::SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr) { | 49 void PpapiGlobals::SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr) { |
41 // If we're using a per-thread PpapiGlobals, we should not have a global one. | 50 // If we're using a per-thread PpapiGlobals, we should not have a global one. |
42 // If we allowed it, it would always over-ride the "test" versions. | 51 // If we allowed it, it would always over-ride the "test" versions. |
43 DCHECK(!ppapi_globals_); | 52 DCHECK(!ppapi_globals); |
44 tls_ppapi_globals_for_test.Pointer()->Set(ptr); | 53 tls_ppapi_globals_for_test.Pointer()->Set(ptr); |
45 } | 54 } |
46 | 55 |
47 base::MessageLoopProxy* PpapiGlobals::GetMainThreadMessageLoop() { | 56 base::MessageLoopProxy* PpapiGlobals::GetMainThreadMessageLoop() { |
48 return main_loop_proxy_.get(); | 57 return main_loop_proxy_.get(); |
49 } | 58 } |
50 | 59 |
51 void PpapiGlobals::ResetMainThreadMessageLoopForTesting() { | 60 void PpapiGlobals::ResetMainThreadMessageLoopForTesting() { |
52 main_loop_proxy_ = base::MessageLoopProxy::current(); | 61 main_loop_proxy_ = base::MessageLoopProxy::current(); |
53 } | 62 } |
54 | 63 |
55 bool PpapiGlobals::IsHostGlobals() const { | 64 bool PpapiGlobals::IsHostGlobals() const { |
56 return false; | 65 return false; |
57 } | 66 } |
58 | 67 |
59 bool PpapiGlobals::IsPluginGlobals() const { | 68 bool PpapiGlobals::IsPluginGlobals() const { |
60 return false; | 69 return false; |
61 } | 70 } |
62 | 71 |
63 // static | 72 // static |
64 PpapiGlobals* PpapiGlobals::GetThreadLocalPointer() { | 73 PpapiGlobals* PpapiGlobals::GetThreadLocalPointer() { |
65 return tls_ppapi_globals_for_test.Pointer()->Get(); | 74 return tls_ppapi_globals_for_test.Pointer()->Get(); |
66 } | 75 } |
67 | 76 |
68 } // namespace ppapi | 77 } // namespace ppapi |
OLD | NEW |