Commit f01ad852 authored by yzshen@chromium.org's avatar yzshen@chromium.org

Change FilterChain::Append() to a template method.

So that we can avoid creating PassThroughFilter.
The CL also makes FilterChain::GetHead() a little more efficient (SyncDispatcher calls it once for receiving each message).

BUG=None
TEST=None
R=darin@chromium.org

Review URL: https://codereview.chromium.org/291583005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271107 0039d316-1c4b-4281-b951-d872f2087c98
parent 164da428
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "mojo/public/cpp/bindings/lib/filter_chain.h" #include "mojo/public/cpp/bindings/lib/filter_chain.h"
#include <assert.h>
#include <algorithm> #include <algorithm>
namespace mojo { namespace mojo {
...@@ -31,27 +33,16 @@ FilterChain::~FilterChain() { ...@@ -31,27 +33,16 @@ FilterChain::~FilterChain() {
} }
} }
FilterChain& FilterChain::Append(MessageFilter* filter) { void FilterChain::SetSink(MessageReceiver* sink) {
assert(!sink_);
sink_ = sink;
if (!filters_.empty()) if (!filters_.empty())
filters_.back()->set_sink(filter); filters_.back()->set_sink(sink);
filters_.push_back(filter);
return *this;
}
FilterChain& FilterChain::Append(PassThroughFilter* filter) {
delete filter;
return *this;
} }
MessageReceiver* FilterChain::GetHead() { MessageReceiver* FilterChain::GetHead() {
assert(sink_); assert(sink_);
return filters_.empty() ? sink_ : filters_.front();
if (filters_.empty())
return sink_;
filters_.back()->set_sink(sink_);
return filters_.front();
} }
} // namespace internal } // namespace internal
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_FILTER_CHAIN_H_ #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_FILTER_CHAIN_H_
#define MOJO_PUBLIC_CPP_BINDINGS_LIB_FILTER_CHAIN_H_ #define MOJO_PUBLIC_CPP_BINDINGS_LIB_FILTER_CHAIN_H_
#include <assert.h>
#include <vector> #include <vector>
#include "mojo/public/cpp/bindings/message.h" #include "mojo/public/cpp/bindings/message.h"
...@@ -30,20 +28,17 @@ class FilterChain { ...@@ -30,20 +28,17 @@ class FilterChain {
~FilterChain(); ~FilterChain();
// Takes ownership of |filter|. template <typename FilterType>
FilterChain& Append(MessageFilter* filter); inline void Append();
FilterChain& Append(PassThroughFilter* filter);
// Doesn't take ownership of |sink|. Therefore |sink| has to stay alive while // Doesn't take ownership of |sink|. Therefore |sink| has to stay alive while
// this object is alive. // this object is alive.
void set_sink(MessageReceiver* sink) { void SetSink(MessageReceiver* sink);
assert(!sink_);
sink_ = sink;
}
// Returns a receiver to accept messages. Messages flow through all filters in // Returns a receiver to accept messages. Messages flow through all filters in
// the same order as they were appended to the chain. If all filters allow a // the same order as they were appended to the chain. If all filters allow a
// message to pass, it will be forwarded to |sink_|. // message to pass, it will be forwarded to |sink_|.
// The returned value is invalidated when this object goes away.
MessageReceiver* GetHead(); MessageReceiver* GetHead();
private: private:
...@@ -53,6 +48,18 @@ class FilterChain { ...@@ -53,6 +48,18 @@ class FilterChain {
MessageReceiver* sink_; MessageReceiver* sink_;
}; };
template <typename FilterType>
inline void FilterChain::Append() {
FilterType* filter = new FilterType(sink_);
if (!filters_.empty())
filters_.back()->set_sink(filter);
filters_.push_back(filter);
}
template <>
inline void FilterChain::Append<PassThroughFilter>() {
}
} // namespace internal } // namespace internal
} // namespace mojo } // namespace mojo
......
...@@ -56,9 +56,9 @@ class InterfaceImplState : public ErrorHandler { ...@@ -56,9 +56,9 @@ class InterfaceImplState : public ErrorHandler {
assert(!router_); assert(!router_);
FilterChain filters; FilterChain filters;
filters.Append(new MessageHeaderValidator) filters.Append<MessageHeaderValidator>();
.Append(new typename Interface::RequestValidator_) filters.Append<typename Interface::RequestValidator_>();
.Append(new typename Interface::Client::ResponseValidator_); filters.Append<typename Interface::Client::ResponseValidator_>();
router_ = new Router(handle.Pass(), filters.Pass(), waiter); router_ = new Router(handle.Pass(), filters.Pass(), waiter);
router_->set_incoming_receiver(&stub_); router_->set_incoming_receiver(&stub_);
......
...@@ -42,9 +42,9 @@ class InterfacePtrState { ...@@ -42,9 +42,9 @@ class InterfacePtrState {
assert(!router_); assert(!router_);
FilterChain filters; FilterChain filters;
filters.Append(new MessageHeaderValidator) filters.Append<MessageHeaderValidator>();
.Append(new typename Interface::Client::RequestValidator_) filters.Append<typename Interface::Client::RequestValidator_>();
.Append(new typename Interface::ResponseValidator_); filters.Append<typename Interface::ResponseValidator_>();
router_ = new Router(handle.Pass(), filters.Pass(), waiter); router_ = new Router(handle.Pass(), filters.Pass(), waiter);
ProxyWithStub* proxy = new ProxyWithStub(router_); ProxyWithStub* proxy = new ProxyWithStub(router_);
......
...@@ -71,7 +71,7 @@ Router::Router(ScopedMessagePipeHandle message_pipe, ...@@ -71,7 +71,7 @@ Router::Router(ScopedMessagePipeHandle message_pipe,
weak_self_(this), weak_self_(this),
incoming_receiver_(NULL), incoming_receiver_(NULL),
next_request_id_(0) { next_request_id_(0) {
filters_.set_sink(&thunk_); filters_.SetSink(&thunk_);
connector_.set_incoming_receiver(filters_.GetHead()); connector_.set_incoming_receiver(filters_.GetHead());
} }
......
...@@ -26,9 +26,9 @@ template<typename Interface> class SyncDispatcher { ...@@ -26,9 +26,9 @@ template<typename Interface> class SyncDispatcher {
: message_pipe_(message_pipe.Pass()) { : message_pipe_(message_pipe.Pass()) {
stub_.set_sink(sink); stub_.set_sink(sink);
filters_.Append(new internal::MessageHeaderValidator) filters_.Append<internal::MessageHeaderValidator>();
.Append(new typename Interface::RequestValidator_); filters_.Append<typename Interface::RequestValidator_>();
filters_.set_sink(&stub_); filters_.SetSink(&stub_);
} }
bool WaitAndDispatchOneMessage() { bool WaitAndDispatchOneMessage() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment