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