1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/kasko/dll_lifetime.h"
16 :
17 : #include "base/at_exit.h"
18 : #include "base/logging_win.h"
19 :
20 m : namespace kasko {
21 :
22 m : class DllLifetime::Core : public base::RefCounted<DllLifetime::Core> {
23 m : public:
24 : // Returns a reference to the single Core instance, creating it if necessary.
25 m : static scoped_refptr<Core> Get();
26 :
27 m : private:
28 m : friend class base::RefCounted<Core>;
29 :
30 m : Core();
31 m : ~Core();
32 :
33 : // The exit manager is in charge of calling the dtors of singletons.
34 m : base::AtExitManager exit_manager_;
35 :
36 : // The single Core instance.
37 m : static Core* instance_;
38 :
39 m : DISALLOW_COPY_AND_ASSIGN(Core);
40 m : };
41 :
42 m : DllLifetime::Core* DllLifetime::Core::instance_ = nullptr;
43 :
44 m : namespace {
45 : // Use the same log facility as Chrome for convenience.
46 : // {3A8A3990-64BC-4143-AEAF-0AA1A0123BCB}
47 m : static const GUID kKaskoTraceProviderName = {
48 m : 0x3a8a3990,
49 m : 0x64bc,
50 m : 0x4143,
51 m : {0xae, 0xaf, 0xa, 0xa1, 0xa0, 0x12, 0x3b, 0xcb}};
52 m : } // namespace
53 :
54 m : DllLifetime::DllLifetime() : core_(DllLifetime::Core::Get()) {
55 m : }
56 :
57 m : DllLifetime::~DllLifetime() {
58 m : }
59 :
60 : // static
61 m : scoped_refptr<DllLifetime::Core> DllLifetime::Core::Get() {
62 m : if (!instance_)
63 m : instance_ = new Core();
64 m : return scoped_refptr<Core>(instance_);
65 m : }
66 :
67 m : DllLifetime::Core::Core() {
68 m : logging::LogEventProvider::Initialize(kKaskoTraceProviderName);
69 m : }
70 :
71 m : DllLifetime::Core::~Core() {
72 m : logging::LogEventProvider::Uninitialize();
73 m : DCHECK_EQ(this, instance_);
74 m : instance_ = nullptr;
75 m : }
76 :
77 m : } // namespace kasko
|