1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_FILTER_CHAIN_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_FILTER_CHAIN_H_ 7 8 #include <utility> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/macros.h" 13 #include "base/memory/ptr_util.h" 14 #include "mojo/public/cpp/bindings/bindings_export.h" 15 #include "mojo/public/cpp/bindings/message.h" 16 17 namespace mojo { 18 19 class MOJO_CPP_BINDINGS_EXPORT FilterChain : public MessageReceiver { 20 public: 21 // Doesn't take ownership of |sink|. Therefore |sink| has to stay alive while 22 // this object is alive. 23 explicit FilterChain(MessageReceiver* sink = nullptr); 24 25 FilterChain(FilterChain&& other); 26 FilterChain& operator=(FilterChain&& other); 27 ~FilterChain() override; 28 29 template <typename FilterType, typename... Args> 30 inline void Append(Args&&... args); 31 32 void Append(std::unique_ptr<MessageReceiver> filter); 33 34 // Doesn't take ownership of |sink|. Therefore |sink| has to stay alive while 35 // this object is alive. 36 void SetSink(MessageReceiver* sink); 37 38 // MessageReceiver: 39 bool Accept(Message* message) override; 40 41 private: 42 std::vector<std::unique_ptr<MessageReceiver>> filters_; 43 44 MessageReceiver* sink_; 45 46 DISALLOW_COPY_AND_ASSIGN(FilterChain); 47 }; 48 49 template <typename FilterType, typename... Args> Append(Args &&...args)50inline void FilterChain::Append(Args&&... args) { 51 Append(std::make_unique<FilterType>(std::forward<Args>(args)...)); 52 } 53 54 template <> 55 inline void FilterChain::Append<PassThroughFilter>() { 56 } 57 58 } // namespace mojo 59 60 #endif // MOJO_PUBLIC_CPP_BINDINGS_FILTER_CHAIN_H_ 61