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 NON_EXPORTED_BASE(public MessageReceiver)20 : NON_EXPORTED_BASE(public MessageReceiver) { 21 public: 22 // Doesn't take ownership of |sink|. Therefore |sink| has to stay alive while 23 // this object is alive. 24 explicit FilterChain(MessageReceiver* sink = nullptr); 25 26 FilterChain(FilterChain&& other); 27 FilterChain& operator=(FilterChain&& other); 28 ~FilterChain() override; 29 30 template <typename FilterType, typename... Args> 31 inline void Append(Args&&... args); 32 33 void Append(std::unique_ptr<MessageReceiver> filter); 34 35 // Doesn't take ownership of |sink|. Therefore |sink| has to stay alive while 36 // this object is alive. 37 void SetSink(MessageReceiver* sink); 38 39 // MessageReceiver: 40 bool Accept(Message* message) override; 41 42 private: 43 std::vector<std::unique_ptr<MessageReceiver>> filters_; 44 45 MessageReceiver* sink_; 46 47 DISALLOW_COPY_AND_ASSIGN(FilterChain); 48 }; 49 50 template <typename FilterType, typename... Args> Append(Args &&...args)51inline void FilterChain::Append(Args&&... args) { 52 Append(base::MakeUnique<FilterType>(std::forward<Args>(args)...)); 53 } 54 55 template <> 56 inline void FilterChain::Append<PassThroughFilter>() { 57 } 58 59 } // namespace mojo 60 61 #endif // MOJO_PUBLIC_CPP_BINDINGS_FILTER_CHAIN_H_ 62