Skip to content

Commit

Permalink
Reland chromium#1: Clean up interface of attachment broker.
Browse files Browse the repository at this point in the history
I forgot to initialize a member variable, which is why the original CL flakily
succeeded (and managed to pass the CQ).

Original issue's description:
> ipc: Clean up interface of attachment broker.
>
> AttachmentBrokerUnprivileged now has a method
> DesignateBrokerCommunicationChannel which is used by non-broker processes to
> designate an IPC::Channel as the communication medium for brokerable attachment
> messages. IPC::Channel has a new member attachment_broker_endpoint_ which causes
> it to pass all messages through the attachment broker before forwarding them to
> the listener.
>
> BUG=493414
>
> Committed: https://crrev.com/9a06836982214f2edced21bbd1615b49e7f231bf
> Cr-Commit-Position: refs/heads/master@{#341143}

BUG=493414
TBR=tsepez@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#341207}
  • Loading branch information
erikchen authored and Commit bot committed Jul 30, 2015
1 parent fc59cb4 commit 8c73f83
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions ipc/attachment_broker_privileged.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ AttachmentBrokerPrivileged::~AttachmentBrokerPrivileged() {}

void AttachmentBrokerPrivileged::RegisterCommunicationChannel(
Channel* channel) {
channel->set_attachment_broker_endpoint(true);
auto it = std::find(channels_.begin(), channels_.end(), channel);
DCHECK(channels_.end() == it);
channels_.push_back(channel);
Expand Down
5 changes: 4 additions & 1 deletion ipc/attachment_broker_privileged_win_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class IPCAttachmentBrokerPrivilegedWinTest : public IPCTestBase {
broker_->AddObserver(&observer_);
set_attachment_broker(broker_.get());
CreateChannel(&proxy_listener_);
broker_->set_sender(channel());
broker_->DesignateBrokerCommunicationChannel(channel());
ASSERT_TRUE(ConnectChannel());
ASSERT_TRUE(StartClient());
}
Expand Down Expand Up @@ -278,6 +278,9 @@ TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleToSelf) {

set_broker(new MockBroker);
CommonSetUp();
// Technically, the channel is an endpoint, but we need the proxy listener to
// receive the messages so that it can quit the message loop.
channel()->set_attachment_broker_endpoint(false);
get_proxy_listener()->set_listener(get_broker());

HANDLE h = CreateTempFile();
Expand Down
13 changes: 12 additions & 1 deletion ipc/attachment_broker_unprivileged.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@

#include "ipc/attachment_broker_unprivileged.h"

#include "ipc/ipc_channel.h"

namespace IPC {

AttachmentBrokerUnprivileged::AttachmentBrokerUnprivileged() {}
AttachmentBrokerUnprivileged::AttachmentBrokerUnprivileged()
: sender_(nullptr) {}

AttachmentBrokerUnprivileged::~AttachmentBrokerUnprivileged() {}

void AttachmentBrokerUnprivileged::DesignateBrokerCommunicationChannel(
IPC::Channel* channel) {
DCHECK(channel);
DCHECK(!sender_);
sender_ = channel;
channel->set_attachment_broker_endpoint(true);
}

} // namespace IPC
5 changes: 4 additions & 1 deletion ipc/attachment_broker_unprivileged.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace IPC {

class Channel;
class Sender;

// This abstract subclass of AttachmentBroker is intended for use in
Expand All @@ -19,7 +20,9 @@ class IPC_EXPORT AttachmentBrokerUnprivileged : public IPC::AttachmentBroker {
AttachmentBrokerUnprivileged();
~AttachmentBrokerUnprivileged() override;

void set_sender(IPC::Sender* sender) { sender_ = sender; }
// In each unprivileged process, exactly one channel should be used to
// communicate brokerable attachments with the broker process.
void DesignateBrokerCommunicationChannel(IPC::Channel* channel);

protected:
IPC::Sender* get_sender() { return sender_; }
Expand Down
12 changes: 12 additions & 0 deletions ipc/ipc_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class IPC_EXPORT Channel : public Sender {
Listener* listener,
AttachmentBroker* broker = nullptr);

Channel() : attachment_broker_endpoint_(false) {}
~Channel() override;

// Connect the pipe. On the server side, this will initiate
Expand Down Expand Up @@ -252,6 +253,17 @@ class IPC_EXPORT Channel : public Sender {
static void NotifyProcessForkedForTesting();
#endif

void set_attachment_broker_endpoint(bool is_endpoint) {
attachment_broker_endpoint_ = is_endpoint;
}

protected:
bool is_attachment_broker_endpoint() { return attachment_broker_endpoint_; }

private:
// Whether this channel is used as an endpoint for sending and receiving
// brokerable attachment messages to/from the broker process.
bool attachment_broker_endpoint_;
};

#if defined(OS_POSIX)
Expand Down
4 changes: 4 additions & 0 deletions ipc/ipc_channel_nacl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ base::ProcessId ChannelNacl::GetSenderPID() {
return base::kNullProcessId;
}

bool ChannelNacl::IsAttachmentBrokerEndpoint() {
return is_attachment_broker_endpoint();
}

// Channel's methods

// static
Expand Down
1 change: 1 addition & 0 deletions ipc/ipc_channel_nacl.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ChannelNacl : public Channel,
bool DidEmptyInputBuffers() override;
void HandleInternalMessage(const Message& msg) override;
base::ProcessId GetSenderPID() override;
bool IsAttachmentBrokerEndpoint() override;

Mode mode_;
bool waiting_connect_;
Expand Down
4 changes: 4 additions & 0 deletions ipc/ipc_channel_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ base::ProcessId ChannelPosix::GetSenderPID() {
return GetPeerPID();
}

bool ChannelPosix::IsAttachmentBrokerEndpoint() {
return is_attachment_broker_endpoint();
}

void ChannelPosix::Close() {
// Close can be called multiple time, so we need to make sure we're
// idempotent.
Expand Down
1 change: 1 addition & 0 deletions ipc/ipc_channel_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class IPC_EXPORT ChannelPosix : public Channel,
bool DidEmptyInputBuffers() override;
void HandleInternalMessage(const Message& msg) override;
base::ProcessId GetSenderPID() override;
bool IsAttachmentBrokerEndpoint() override;

// Finds the set of file descriptors in the given message. On success,
// appends the descriptors to the input_fds_ member and returns true
Expand Down
13 changes: 11 additions & 2 deletions ipc/ipc_channel_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,18 @@ void ChannelReader::DispatchMessage(Message* m) {
IPC_MESSAGE_ID_LINE(m->type()));
#endif
m->TraceMessageEnd();
if (IsInternalMessage(*m))

bool handled = false;
if (IsInternalMessage(*m)) {
HandleInternalMessage(*m);
else
handled = true;
}
#if USE_ATTACHMENT_BROKER
if (!handled && IsAttachmentBrokerEndpoint() && GetAttachmentBroker()) {
handled = GetAttachmentBroker()->OnMessageReceived(*m);
}
#endif // USE_ATTACHMENT_BROKER
if (!handled)
listener_->OnMessageReceived(*m);
if (m->dispatch_error())
listener_->OnBadMessageReceived(*m);
Expand Down
3 changes: 3 additions & 0 deletions ipc/ipc_channel_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ class IPC_EXPORT ChannelReader : public SupportsAttachmentBrokering,
// Get the process ID for the sender of the message.
virtual base::ProcessId GetSenderPID() = 0;

// Whether the channel is an endpoint of attachment brokering.
virtual bool IsAttachmentBrokerEndpoint() = 0;

private:
FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentAlreadyBrokered);
FRIEND_TEST_ALL_PREFIXES(ChannelReaderTest, AttachmentNotYetBrokered);
Expand Down
2 changes: 2 additions & 0 deletions ipc/ipc_channel_reader_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class MockChannelReader : public ChannelReader {

base::ProcessId GetSenderPID() override { return base::kNullProcessId; }

bool IsAttachmentBrokerEndpoint() override { return false; }

AttachmentBroker* GetAttachmentBroker() override { return broker_; }

// This instance takes ownership of |m|.
Expand Down
4 changes: 4 additions & 0 deletions ipc/ipc_channel_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ base::ProcessId ChannelWin::GetSenderPID() {
return GetPeerPID();
}

bool ChannelWin::IsAttachmentBrokerEndpoint() {
return is_attachment_broker_endpoint();
}

bool ChannelWin::DidEmptyInputBuffers() {
// We don't need to do anything here.
return true;
Expand Down
1 change: 1 addition & 0 deletions ipc/ipc_channel_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ChannelWin : public Channel,
bool DidEmptyInputBuffers() override;
void HandleInternalMessage(const Message& msg) override;
base::ProcessId GetSenderPID() override;
bool IsAttachmentBrokerEndpoint() override;

static const base::string16 PipeName(const std::string& channel_id,
int32* secret);
Expand Down

0 comments on commit 8c73f83

Please sign in to comment.