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_MEDIA_FACTORY_FACTORY_SERVICE_H_ | 5 #ifndef MOJO_SERVICES_MEDIA_FACTORY_FACTORY_SERVICE_H_ |
6 #define MOJO_SERVICES_MEDIA_FACTORY_FACTORY_SERVICE_H_ | 6 #define MOJO_SERVICES_MEDIA_FACTORY_FACTORY_SERVICE_H_ |
7 | 7 |
8 #include <unordered_set> | |
9 | |
10 #include "mojo/common/binding_set.h" | 8 #include "mojo/common/binding_set.h" |
11 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
12 #include "mojo/public/cpp/application/application_impl.h" | 10 #include "mojo/public/cpp/application/application_impl.h" |
13 #include "mojo/services/media/control/interfaces/media_factory.mojom.h" | 11 #include "mojo/services/media/control/interfaces/media_factory.mojom.h" |
| 12 #include "services/util/cpp/factory_service_base.h" |
14 | 13 |
15 namespace mojo { | 14 namespace mojo { |
16 namespace media { | 15 namespace media { |
17 | 16 |
18 class MediaFactoryService : public ApplicationDelegate, | 17 class MediaFactoryService : public util::FactoryServiceBase, |
19 public MediaFactory { | 18 public MediaFactory { |
20 public: | 19 public: |
21 // Provides common behavior for all objects created by the factory service. | |
22 class ProductBase : public std::enable_shared_from_this<ProductBase> { | |
23 public: | |
24 virtual ~ProductBase(); | |
25 | |
26 protected: | |
27 ProductBase(MediaFactoryService* owner); | |
28 | |
29 // Returns the ApplicationImpl. | |
30 ApplicationImpl* app() { | |
31 DCHECK(owner_->app_); | |
32 return owner_->app_; | |
33 } | |
34 | |
35 // Tells the factory service to release this product. | |
36 void ReleaseFromOwner() { | |
37 size_t erased = owner_->products_.erase(shared_from_this()); | |
38 DCHECK(erased); | |
39 } | |
40 | |
41 private: | |
42 MediaFactoryService* owner_; | |
43 }; | |
44 | |
45 template <typename Interface> | |
46 class Product : public ProductBase { | |
47 public: | |
48 virtual ~Product() {} | |
49 | |
50 protected: | |
51 Product(Interface* impl, | |
52 InterfaceRequest<Interface> request, | |
53 MediaFactoryService* owner) | |
54 : ProductBase(owner), binding_(impl, request.Pass()) { | |
55 DCHECK(impl); | |
56 binding_.set_connection_error_handler([this]() { ReleaseFromOwner(); }); | |
57 } | |
58 | |
59 // Closes the binding and calls ReleaseFromOwner. | |
60 void UnbindAndReleaseFromOwner() { | |
61 if (binding_.is_bound()) { | |
62 binding_.Close(); | |
63 } | |
64 | |
65 ReleaseFromOwner(); | |
66 } | |
67 | |
68 private: | |
69 Binding<Interface> binding_; | |
70 }; | |
71 | |
72 MediaFactoryService(); | 20 MediaFactoryService(); |
73 | 21 |
74 ~MediaFactoryService() override; | 22 ~MediaFactoryService() override; |
75 | 23 |
76 // ApplicationDelegate implementation. | 24 // ApplicationDelegate implementation. |
77 void Initialize(ApplicationImpl* app) override; | |
78 | |
79 bool ConfigureIncomingConnection( | 25 bool ConfigureIncomingConnection( |
80 ServiceProviderImpl* service_provider_impl) override; | 26 ServiceProviderImpl* service_provider_impl) override; |
81 | 27 |
82 // MediaFactory implementation. | 28 // MediaFactory implementation. |
83 void CreatePlayer(InterfaceHandle<SeekingReader> reader, | 29 void CreatePlayer(InterfaceHandle<SeekingReader> reader, |
84 InterfaceRequest<MediaPlayer> player) override; | 30 InterfaceRequest<MediaPlayer> player) override; |
85 | 31 |
86 void CreateSource(InterfaceHandle<SeekingReader> reader, | 32 void CreateSource(InterfaceHandle<SeekingReader> reader, |
87 Array<MediaTypeSetPtr> allowed_media_types, | 33 Array<MediaTypeSetPtr> allowed_media_types, |
88 InterfaceRequest<MediaSource> source) override; | 34 InterfaceRequest<MediaSource> source) override; |
89 | 35 |
90 void CreateSink(const String& destination_url, | 36 void CreateSink(const String& destination_url, |
91 MediaTypePtr media_type, | 37 MediaTypePtr media_type, |
92 InterfaceRequest<MediaSink> sink) override; | 38 InterfaceRequest<MediaSink> sink) override; |
93 | 39 |
94 void CreateDemux(InterfaceHandle<SeekingReader> reader, | 40 void CreateDemux(InterfaceHandle<SeekingReader> reader, |
95 InterfaceRequest<MediaDemux> demux) override; | 41 InterfaceRequest<MediaDemux> demux) override; |
96 | 42 |
97 void CreateDecoder(MediaTypePtr input_media_type, | 43 void CreateDecoder(MediaTypePtr input_media_type, |
98 InterfaceRequest<MediaTypeConverter> decoder) override; | 44 InterfaceRequest<MediaTypeConverter> decoder) override; |
99 | 45 |
100 void CreateNetworkReader(const String& url, | 46 void CreateNetworkReader(const String& url, |
101 InterfaceRequest<SeekingReader> reader) override; | 47 InterfaceRequest<SeekingReader> reader) override; |
102 | 48 |
103 private: | 49 private: |
104 BindingSet<MediaFactory> bindings_; | 50 BindingSet<MediaFactory> bindings_; |
105 ApplicationImpl* app_; | |
106 std::unordered_set<std::shared_ptr<ProductBase>> products_; | |
107 }; | 51 }; |
108 | 52 |
109 // For use by products when handling mojo requests. | |
110 // Checks the condition, and, if it's false, unbinds, releases from the owner | |
111 // and calls return. Doesn't support stream arguments. | |
112 // TODO(dalesat): Support stream arguments. | |
113 #define RCHECK(condition) \ | |
114 if (!(condition)) { \ | |
115 LOG(ERROR) << "request precondition failed: " #condition "."; \ | |
116 UnbindAndReleaseFromOwner(); \ | |
117 return; \ | |
118 } | |
119 | |
120 } // namespace media | 53 } // namespace media |
121 } // namespace mojo | 54 } // namespace mojo |
122 | 55 |
123 #endif // MOJO_SERVICES_MEDIA_FACTORY_FACTORY_SERVICE_H_ | 56 #endif // MOJO_SERVICES_MEDIA_FACTORY_FACTORY_SERVICE_H_ |
OLD | NEW |