Initialize Tizen 2.3
[external/chromium.git] / ipc / ipc_sync_message.h
1 // Copyright (c) 2011 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 IPC_IPC_SYNC_MESSAGE_H_
6 #define IPC_IPC_SYNC_MESSAGE_H_
7 #pragma once
8
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #endif
12 #include <string>
13 #include "base/basictypes.h"
14 #include "ipc/ipc_message.h"
15
16 namespace base {
17 class WaitableEvent;
18 }
19
20 namespace IPC {
21
22 class MessageReplyDeserializer;
23
24 class IPC_EXPORT SyncMessage : public Message {
25  public:
26   SyncMessage(int32 routing_id, uint32 type, PriorityValue priority,
27               MessageReplyDeserializer* deserializer);
28
29   // Call this to get a deserializer for the output parameters.
30   // Note that this can only be called once, and the caller is responsible
31   // for deleting the deserializer when they're done.
32   MessageReplyDeserializer* GetReplyDeserializer();
33
34   // If this message can cause the receiver to block while waiting for user
35   // input (i.e. by calling MessageBox), then the caller needs to pump window
36   // messages and dispatch asynchronous messages while waiting for the reply.
37   // If this event is passed in, then window messages will start being pumped
38   // when it's set.  Note that this behavior will continue even if the event is
39   // later reset.  The event must be valid until after the Send call returns.
40   void set_pump_messages_event(base::WaitableEvent* event) {
41     pump_messages_event_ = event;
42     if (event) {
43       header()->flags |= PUMPING_MSGS_BIT;
44     } else {
45       header()->flags &= ~PUMPING_MSGS_BIT;
46     }
47   }
48
49   // Call this if you always want to pump messages.  You can call this method
50   // or set_pump_messages_event but not both.
51   void EnableMessagePumping();
52
53   base::WaitableEvent* pump_messages_event() const {
54     return pump_messages_event_;
55   }
56
57   // Returns true if the message is a reply to the given request id.
58   static bool IsMessageReplyTo(const Message& msg, int request_id);
59
60   // Given a reply message, returns an iterator to the beginning of the data
61   // (i.e. skips over the synchronous specific data).
62   static void* GetDataIterator(const Message* msg);
63
64   // Given a synchronous message (or its reply), returns its id.
65   static int GetMessageId(const Message& msg);
66
67   // Generates a reply message to the given message.
68   static Message* GenerateReply(const Message* msg);
69
70  private:
71   struct SyncHeader {
72     // unique ID (unique per sender)
73     int message_id;
74   };
75
76   static bool ReadSyncHeader(const Message& msg, SyncHeader* header);
77   static bool WriteSyncHeader(Message* msg, const SyncHeader& header);
78
79   MessageReplyDeserializer* deserializer_;
80   base::WaitableEvent* pump_messages_event_;
81 };
82
83 // Used to deserialize parameters from a reply to a synchronous message
84 class IPC_EXPORT MessageReplyDeserializer {
85  public:
86   virtual ~MessageReplyDeserializer() {}
87   bool SerializeOutputParameters(const Message& msg);
88  private:
89   // Derived classes need to implement this, using the given iterator (which
90   // is skipped past the header for synchronous messages).
91   virtual bool SerializeOutputParameters(const Message& msg, void* iter) = 0;
92 };
93
94 // When sending a synchronous message, this structure contains an object
95 // that knows how to deserialize the response.
96 struct PendingSyncMsg {
97   PendingSyncMsg(int id,
98                  MessageReplyDeserializer* d,
99                  base::WaitableEvent* e)
100       : id(id), deserializer(d), done_event(e), send_result(false) { }
101   int id;
102   MessageReplyDeserializer* deserializer;
103   base::WaitableEvent* done_event;
104   bool send_result;
105 };
106
107 }  // namespace IPC
108
109 #endif  // IPC_IPC_SYNC_MESSAGE_H_