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 @@
#include "mojo/public/cpp/bindings/lib/filter_chain.h"
#include <assert.h>
#include <algorithm>
namespace mojo {
......@@ -31,27 +33,16 @@ FilterChain::~FilterChain() {
}
}
FilterChain& FilterChain::Append(MessageFilter* filter) {
void FilterChain::SetSink(MessageReceiver* sink) {
assert(!sink_);
sink_ = sink;
if (!filters_.empty())
filters_.back()->set_sink(filter);
filters_.push_back(filter);
return *this;
}
FilterChain& FilterChain::Append(PassThroughFilter* filter) {
delete filter;
return *this;
filters_.back()->set_sink(sink);
}
MessageReceiver* FilterChain::GetHead() {
assert(sink_);
if (filters_.empty())
return sink_;
filters_.back()->set_sink(sink_);
return filters_.front();
return filters_.empty() ? sink_ : filters_.front();
}
} // namespace internal
......
......@@ -5,8 +5,6 @@
#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_FILTER_CHAIN_H_
#define MOJO_PUBLIC_CPP_BINDINGS_LIB_FILTER_CHAIN_H_
#include <assert.h>
#include <vector>
#include "mojo/public/cpp/bindings/message.h"
......@@ -30,20 +28,17 @@ class FilterChain {
~FilterChain();
// Takes ownership of |filter|.
FilterChain& Append(MessageFilter* filter);
FilterChain& Append(PassThroughFilter* filter);
template <typename FilterType>
inline void Append();
// Doesn't take ownership of |sink|. Therefore |sink| has to stay alive while
// this object is alive.
void set_sink(MessageReceiver* sink) {
assert(!sink_);
sink_ = sink;
}
void SetSink(MessageReceiver* sink);
// 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
// message to pass, it will be forwarded to |sink_|.
// The returned value is invalidated when this object goes away.
MessageReceiver* GetHead();
private:
......@@ -53,6 +48,18 @@ class FilterChain {
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 mojo
......
......@@ -56,9 +56,9 @@ class InterfaceImplState : public ErrorHandler {
assert(!router_);
FilterChain filters;
filters.Append(new MessageHeaderValidator)
.Append(new typename Interface::RequestValidator_)
.Append(new typename Interface::Client::ResponseValidator_);
filters.Append<MessageHeaderValidator>();
filters.Append<typename Interface::RequestValidator_>();
filters.Append<typename Interface::Client::ResponseValidator_>();
router_ = new Router(handle.Pass(), filters.Pass(), waiter);
router_->set_incoming_receiver(&stub_);
......
......@@ -42,9 +42,9 @@ class InterfacePtrState {
assert(!router_);
FilterChain filters;
filters.Append(new MessageHeaderValidator)
.Append(new typename Interface::Client::RequestValidator_)
.Append(new typename Interface::ResponseValidator_);
filters.Append<MessageHeaderValidator>();
filters.Append<typename Interface::Client::RequestValidator_>();
filters.Append<typename Interface::ResponseValidator_>();
router_ = new Router(handle.Pass(), filters.Pass(), waiter);
ProxyWithStub* proxy = new ProxyWithStub(router_);
......
......@@ -71,7 +71,7 @@ Router::Router(ScopedMessagePipeHandle message_pipe,
weak_self_(this),
incoming_receiver_(NULL),
next_request_id_(0) {
filters_.set_sink(&thunk_);
filters_.SetSink(&thunk_);
connector_.set_incoming_receiver(filters_.GetHead());
}
......
......@@ -26,9 +26,9 @@ template<typename Interface> class SyncDispatcher {
: message_pipe_(message_pipe.Pass()) {
stub_.set_sink(sink);
filters_.Append(new internal::MessageHeaderValidator)
.Append(new typename Interface::RequestValidator_);
filters_.set_sink(&stub_);
filters_.Append<internal::MessageHeaderValidator>();
filters_.Append<typename Interface::RequestValidator_>();
filters_.SetSink(&stub_);
}
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