summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/bindings/lib/router.h
diff options
context:
space:
mode:
authoryzshen <yzshen@chromium.org>2016-02-22 17:22:10 -0800
committerCommit bot <commit-bot@chromium.org>2016-02-23 01:23:00 +0000
commit8ef9022a0d352066587791b3e225ca264c688ade (patch)
tree099161b2828cb2e83dfb77037930a6ae68c78b8e /mojo/public/cpp/bindings/lib/router.h
parent9166c9c8a3fbbd0dc74f837660ca6952a2203402 (diff)
downloadchromium_src-8ef9022a0d352066587791b3e225ca264c688ade.zip
chromium_src-8ef9022a0d352066587791b3e225ca264c688ade.tar.gz
chromium_src-8ef9022a0d352066587791b3e225ca264c688ade.tar.bz2
Reland "Mojo C++ bindings: support sync methods - part 2"
This CL introduces the correct re-entrancy behavior: when a sync call is waiting for response, allow incoming sync requests on the same thread to re-enter, async messages are queued until the sync call completes. The following will be in future CLs: - Support sync calls with associated interfaces. The original CL is at https://codereview.chromium.org/1713203002/ BUG=577699 Review URL: https://codereview.chromium.org/1723673002 Cr-Commit-Position: refs/heads/master@{#376892}
Diffstat (limited to 'mojo/public/cpp/bindings/lib/router.h')
-rw-r--r--mojo/public/cpp/bindings/lib/router.h36
1 files changed, 31 insertions, 5 deletions
diff --git a/mojo/public/cpp/bindings/lib/router.h b/mojo/public/cpp/bindings/lib/router.h
index f7ce513..12aa7e72 100644
--- a/mojo/public/cpp/bindings/lib/router.h
+++ b/mojo/public/cpp/bindings/lib/router.h
@@ -8,8 +8,10 @@
#include <stdint.h>
#include <map>
+#include <queue>
-#include "base/logging.h"
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "mojo/public/cpp/bindings/callback.h"
@@ -24,6 +26,7 @@ class Router : public MessageReceiverWithResponder {
public:
Router(ScopedMessagePipeHandle message_pipe,
FilterChain filters,
+ bool expects_sync_requests,
const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter());
~Router() override;
@@ -104,13 +107,29 @@ class Router : public MessageReceiverWithResponder {
// Returns true if this Router has any pending callbacks.
bool has_pending_responders() const {
DCHECK(thread_checker_.CalledOnValidThread());
- return !async_responders_.empty() || !sync_responders_.empty();
+ return !async_responders_.empty() || !sync_responses_.empty();
}
private:
// Maps from the id of a response to the MessageReceiver that handles the
// response.
- typedef std::map<uint64_t, MessageReceiver*> ResponderMap;
+ using AsyncResponderMap = std::map<uint64_t, scoped_ptr<MessageReceiver>>;
+
+ struct SyncResponseInfo {
+ public:
+ explicit SyncResponseInfo(bool* in_response_received);
+ ~SyncResponseInfo();
+
+ scoped_ptr<Message> response;
+
+ // Points to a stack-allocated variable.
+ bool* response_received;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(SyncResponseInfo);
+ };
+
+ using SyncResponseMap = std::map<uint64_t, scoped_ptr<SyncResponseInfo>>;
class HandleIncomingMessageThunk : public MessageReceiver {
public:
@@ -125,15 +144,22 @@ class Router : public MessageReceiverWithResponder {
};
bool HandleIncomingMessage(Message* message);
+ void HandleQueuedMessages();
+
+ bool HandleMessageInternal(Message* message);
HandleIncomingMessageThunk thunk_;
FilterChain filters_;
Connector connector_;
MessageReceiverWithResponderStatus* incoming_receiver_;
- ResponderMap async_responders_;
- ResponderMap sync_responders_;
+ AsyncResponderMap async_responders_;
+ SyncResponseMap sync_responses_;
uint64_t next_request_id_;
bool testing_mode_;
+ std::queue<scoped_ptr<Message>> pending_messages_;
+ // Whether a task has been posted to trigger processing of
+ // |pending_messages_|.
+ bool pending_task_for_messages_;
base::ThreadChecker thread_checker_;
base::WeakPtrFactory<Router> weak_factory_;
};