OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 MOJO_SERVICES_UTIL_CPP_FACTORY_SERVICE_BASE_H_ | 5 #ifndef MOJO_SERVICES_UTIL_CPP_FACTORY_SERVICE_BASE_H_ |
6 #define MOJO_SERVICES_UTIL_CPP_FACTORY_SERVICE_BASE_H_ | 6 #define MOJO_SERVICES_UTIL_CPP_FACTORY_SERVICE_BASE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <unordered_set> | 9 #include <unordered_set> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "mojo/public/cpp/application/application_delegate.h" | 12 #include "mojo/public/cpp/application/application_delegate.h" |
13 #include "mojo/public/cpp/application/application_impl.h" | 13 #include "mojo/public/cpp/application/application_impl.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 namespace util { | 16 namespace util { |
17 | 17 |
18 class FactoryServiceBase : public ApplicationDelegate { | 18 class FactoryServiceBase : public ApplicationDelegate { |
19 public: | 19 public: |
20 // Provides common behavior for all objects created by the factory service. | 20 // Provides common behavior for all objects created by the factory service. |
21 class ProductBase : public std::enable_shared_from_this<ProductBase> { | 21 class ProductBase : public std::enable_shared_from_this<ProductBase> { |
22 public: | 22 public: |
23 virtual ~ProductBase(); | 23 virtual ~ProductBase(); |
24 | 24 |
25 protected: | 25 protected: |
26 ProductBase(FactoryServiceBase* owner); | 26 ProductBase(FactoryServiceBase* owner); |
27 | 27 |
28 // Returns the ApplicationImpl. | 28 // Returns the ApplicationImpl. |
29 ApplicationImpl* app() { | 29 ApplicationImpl* app() { |
30 DCHECK(owner_->app_); | 30 DCHECK(owner_->app()); |
31 return owner_->app_; | 31 return owner_->app(); |
32 } | 32 } |
33 | 33 |
34 // Tells the factory service to release this product. | 34 // Tells the factory service to release this product. |
35 void ReleaseFromOwner() { | 35 void ReleaseFromOwner() { |
36 size_t erased = owner_->products_.erase(shared_from_this()); | 36 size_t erased = owner_->products_.erase(shared_from_this()); |
37 DCHECK(erased); | 37 DCHECK(erased); |
38 } | 38 } |
39 | 39 |
40 private: | 40 private: |
41 FactoryServiceBase* owner_; | 41 FactoryServiceBase* owner_; |
(...skipping 23 matching lines...) Expand all Loading... |
65 } | 65 } |
66 | 66 |
67 private: | 67 private: |
68 Binding<Interface> binding_; | 68 Binding<Interface> binding_; |
69 }; | 69 }; |
70 | 70 |
71 FactoryServiceBase(); | 71 FactoryServiceBase(); |
72 | 72 |
73 ~FactoryServiceBase() override; | 73 ~FactoryServiceBase() override; |
74 | 74 |
| 75 ApplicationImpl* app() { return app_; } |
| 76 |
75 // ApplicationDelegate implementation. | 77 // ApplicationDelegate implementation. |
76 void Initialize(ApplicationImpl* app) override; | 78 void Initialize(ApplicationImpl* app) override; |
77 | 79 |
78 protected: | 80 protected: |
79 template <typename ProductImpl> | 81 template <typename ProductImpl> |
80 void AddProduct(std::shared_ptr<ProductImpl> product) { | 82 void AddProduct(std::shared_ptr<ProductImpl> product) { |
81 products_.insert(std::static_pointer_cast<ProductBase>(product)); | 83 products_.insert(std::static_pointer_cast<ProductBase>(product)); |
82 } | 84 } |
83 | 85 |
84 private: | 86 private: |
85 ApplicationImpl* app_; | 87 ApplicationImpl* app_; |
86 std::unordered_set<std::shared_ptr<ProductBase>> products_; | 88 std::unordered_set<std::shared_ptr<ProductBase>> products_; |
87 }; | 89 }; |
88 | 90 |
89 // For use by products when handling mojo requests. | 91 // For use by products when handling mojo requests. |
90 // Checks the condition, and, if it's false, unbinds, releases from the owner | 92 // Checks the condition, and, if it's false, unbinds, releases from the owner |
91 // and calls return. Doesn't support stream arguments. | 93 // and calls return. Doesn't support stream arguments. |
92 // TODO(dalesat): Support stream arguments. | 94 // TODO(dalesat): Support stream arguments. |
93 #define RCHECK(condition) \ | 95 #define RCHECK(condition) \ |
94 if (!(condition)) { \ | 96 if (!(condition)) { \ |
95 LOG(ERROR) << "request precondition failed: " #condition "."; \ | 97 LOG(ERROR) << "request precondition failed: " #condition "."; \ |
96 UnbindAndReleaseFromOwner(); \ | 98 UnbindAndReleaseFromOwner(); \ |
97 return; \ | 99 return; \ |
98 } | 100 } |
99 | 101 |
100 } // namespace util | 102 } // namespace util |
101 } // namespace mojo | 103 } // namespace mojo |
102 | 104 |
103 #endif // MOJO_SERVICES_UTIL_CPP_FACTORY_SERVICE_BASE_H_ | 105 #endif // MOJO_SERVICES_UTIL_CPP_FACTORY_SERVICE_BASE_H_ |
OLD | NEW |