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 #ifndef CHROME_BROWSER_CHROMEOS_CROS_CROS_LIBRARY_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_CROS_CROS_LIBRARY_H_ |
6 #define CHROME_BROWSER_CHROMEOS_CROS_CROS_LIBRARY_H_ | 6 #define CHROME_BROWSER_CHROMEOS_CROS_CROS_LIBRARY_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 template <typename T> struct DefaultLazyInstanceTraits; | 13 template <typename T> struct DefaultLazyInstanceTraits; |
14 } | 14 } |
15 | 15 |
16 namespace chromeos { | 16 namespace chromeos { |
17 | 17 |
18 class CertLibrary; | |
19 class NetworkLibrary; | 18 class NetworkLibrary; |
20 | 19 |
21 // This class handles access to sub-parts of ChromeOS library. it provides | 20 // This class handles access to sub-parts of ChromeOS library. it provides |
22 // a level of indirection so individual libraries that it exposes can | 21 // a level of indirection so individual libraries that it exposes can |
23 // be mocked for testing. | 22 // be mocked for testing. |
24 class CrosLibrary { | 23 class CrosLibrary { |
25 public: | 24 public: |
26 // This class provides access to internal members of CrosLibrary class for | 25 // This class provides access to internal members of CrosLibrary class for |
27 // purpose of testing (i.e. replacement of members' implementation with | 26 // purpose of testing (i.e. replacement of members' implementation with |
28 // mock objects). | 27 // mock objects). |
29 class TestApi { | 28 class TestApi { |
30 public: | 29 public: |
31 // Passing true for own for these setters will cause them to be deleted | 30 // Passing true for own for these setters will cause them to be deleted |
32 // when the CrosLibrary is deleted (or other mocks are set). | 31 // when the CrosLibrary is deleted (or other mocks are set). |
33 void SetCertLibrary(CertLibrary* library, bool own); | |
34 void SetNetworkLibrary(NetworkLibrary* library, bool own); | 32 void SetNetworkLibrary(NetworkLibrary* library, bool own); |
35 | 33 |
36 private: | 34 private: |
37 friend class CrosLibrary; | 35 friend class CrosLibrary; |
38 explicit TestApi(CrosLibrary* library) : library_(library) {} | 36 explicit TestApi(CrosLibrary* library) : library_(library) {} |
39 CrosLibrary* library_; | 37 CrosLibrary* library_; |
40 }; | 38 }; |
41 | 39 |
42 // Sets the global instance. Must be called before any calls to Get(). | 40 // Sets the global instance. Must be called before any calls to Get(). |
43 static void Initialize(bool use_stub); | 41 static void Initialize(bool use_stub); |
44 | 42 |
45 // Destroys the global instance. Must be called before AtExitManager is | 43 // Destroys the global instance. Must be called before AtExitManager is |
46 // destroyed to ensure a clean shutdown. | 44 // destroyed to ensure a clean shutdown. |
47 static void Shutdown(); | 45 static void Shutdown(); |
48 | 46 |
49 // Gets the global instance. Returns NULL if Initialize() has not been | 47 // Gets the global instance. Returns NULL if Initialize() has not been |
50 // called (or Shutdown() has been called). | 48 // called (or Shutdown() has been called). |
51 static CrosLibrary* Get(); | 49 static CrosLibrary* Get(); |
52 | 50 |
53 CertLibrary* GetCertLibrary(); | |
54 NetworkLibrary* GetNetworkLibrary(); | 51 NetworkLibrary* GetNetworkLibrary(); |
55 | 52 |
56 // Getter for Test API that gives access to internal members of this class. | 53 // Getter for Test API that gives access to internal members of this class. |
57 TestApi* GetTestApi(); | 54 TestApi* GetTestApi(); |
58 | 55 |
59 // Note: Since we are no longer loading Libcros, we can return true here | 56 // Note: Since we are no longer loading Libcros, we can return true here |
60 // whenever the used libraries are not stub. | 57 // whenever the used libraries are not stub. |
61 // TODO(hashimoto): Remove this method. | 58 // TODO(hashimoto): Remove this method. |
62 bool libcros_loaded() { return !use_stub_impl_; } | 59 bool libcros_loaded() { return !use_stub_impl_; } |
63 | 60 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 library_ = library; | 92 library_ = library; |
96 own_ = own; | 93 own_ = own; |
97 } | 94 } |
98 } | 95 } |
99 | 96 |
100 private: | 97 private: |
101 L* library_; | 98 L* library_; |
102 bool own_; | 99 bool own_; |
103 }; | 100 }; |
104 | 101 |
105 Library<CertLibrary> cert_lib_; | |
106 Library<NetworkLibrary> network_lib_; | 102 Library<NetworkLibrary> network_lib_; |
107 | 103 |
108 // Stub implementations of the libraries should be used. | 104 // Stub implementations of the libraries should be used. |
109 bool use_stub_impl_; | 105 bool use_stub_impl_; |
110 scoped_ptr<TestApi> test_api_; | 106 scoped_ptr<TestApi> test_api_; |
111 | 107 |
112 DISALLOW_COPY_AND_ASSIGN(CrosLibrary); | 108 DISALLOW_COPY_AND_ASSIGN(CrosLibrary); |
113 }; | 109 }; |
114 | 110 |
115 // The class is used for enabling the stub libcros, and cleaning it up at | 111 // The class is used for enabling the stub libcros, and cleaning it up at |
116 // the end of the object lifetime. Useful for testing. | 112 // the end of the object lifetime. Useful for testing. |
117 class ScopedStubCrosEnabler { | 113 class ScopedStubCrosEnabler { |
118 public: | 114 public: |
119 ScopedStubCrosEnabler() { | 115 ScopedStubCrosEnabler() { |
120 chromeos::CrosLibrary::Initialize(true); | 116 chromeos::CrosLibrary::Initialize(true); |
121 } | 117 } |
122 | 118 |
123 ~ScopedStubCrosEnabler() { | 119 ~ScopedStubCrosEnabler() { |
124 chromeos::CrosLibrary::Shutdown(); | 120 chromeos::CrosLibrary::Shutdown(); |
125 } | 121 } |
126 | 122 |
127 private: | 123 private: |
128 DISALLOW_COPY_AND_ASSIGN(ScopedStubCrosEnabler); | 124 DISALLOW_COPY_AND_ASSIGN(ScopedStubCrosEnabler); |
129 }; | 125 }; |
130 | 126 |
131 } // namespace chromeos | 127 } // namespace chromeos |
132 | 128 |
133 #endif // CHROME_BROWSER_CHROMEOS_CROS_CROS_LIBRARY_H_ | 129 #endif // CHROME_BROWSER_CHROMEOS_CROS_CROS_LIBRARY_H_ |
OLD | NEW |