summaryrefslogtreecommitdiffstats
path: root/ipc/chromium/src/chrome/common/ipc_message.h
blob: 934a9b345908dc5a41654c83e55a669a97520f48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_COMMON_IPC_MESSAGE_H__
#define CHROME_COMMON_IPC_MESSAGE_H__

#include <string>

#include "base/basictypes.h"
#include "base/pickle.h"
#include "mozilla/RefPtr.h"
#include "mozilla/TimeStamp.h"

#ifdef MOZ_TASK_TRACER
#  include "GeckoTaskTracer.h"
#endif

#ifdef FUZZING
#  include "mozilla/ipc/Faulty.h"
#endif

namespace base {
struct FileDescriptor;
}

namespace mozilla {
namespace ipc {
class MiniTransceiver;
}
}  // namespace mozilla

class FileDescriptorSet;

namespace IPC {

//------------------------------------------------------------------------------

// Generated by IPDL compiler
const char* StringFromIPCMessageType(uint32_t aMessageType);

class Channel;
class Message;
#ifdef FUZZING
class Faulty;
#endif
struct LogData;

class Message : public Pickle {
 public:
  typedef uint32_t msgid_t;

  enum NestedLevel {
    NOT_NESTED = 1,
    NESTED_INSIDE_SYNC = 2,
    NESTED_INSIDE_CPOW = 3
  };

  enum PriorityValue {
    NORMAL_PRIORITY = 0,
    INPUT_PRIORITY = 1,
    HIGH_PRIORITY = 2,
    MEDIUMHIGH_PRIORITY = 3,
  };

  enum MessageCompression {
    COMPRESSION_NONE,
    COMPRESSION_ENABLED,
    COMPRESSION_ALL
  };

  enum Sync {
    SYNC = 0,
    ASYNC = 1,
  };

  enum Interrupt {
    NOT_INTERRUPT = 0,
    INTERRUPT = 1,
  };

  enum Constructor {
    NOT_CONSTRUCTOR = 0,
    CONSTRUCTOR = 1,
  };

  enum Reply {
    NOT_REPLY = 0,
    REPLY = 1,
  };

  class HeaderFlags {
    friend class Message;

    enum {
      NESTED_MASK = 0x0003,
      PRIO_MASK = 0x000C,
      SYNC_BIT = 0x0010,
      REPLY_BIT = 0x0020,
      REPLY_ERROR_BIT = 0x0040,
      INTERRUPT_BIT = 0x0080,
      COMPRESS_BIT = 0x0100,
      COMPRESSALL_BIT = 0x0200,
      COMPRESS_MASK = 0x0300,
      CONSTRUCTOR_BIT = 0x0400,
#ifdef MOZ_TASK_TRACER
      TASKTRACER_BIT = 0x0800,
#endif
    };

   public:
    constexpr HeaderFlags() : mFlags(NOT_NESTED) {}

    explicit constexpr HeaderFlags(NestedLevel level) : mFlags(level) {}

    constexpr HeaderFlags(NestedLevel level, PriorityValue priority,
                          MessageCompression compression,
                          Constructor constructor, Sync sync,
                          Interrupt interrupt, Reply reply)
        : mFlags(level | (priority << 2) |
                 (compression == COMPRESSION_ENABLED ? COMPRESS_BIT
                  : compression == COMPRESSION_ALL   ? COMPRESSALL_BIT
                                                     : 0) |
                 (constructor == CONSTRUCTOR ? CONSTRUCTOR_BIT : 0) |
                 (sync == SYNC ? SYNC_BIT : 0) |
                 (interrupt == INTERRUPT ? INTERRUPT_BIT : 0) |
                 (reply == REPLY ? REPLY_BIT : 0)) {}

    NestedLevel Level() const {
      return static_cast<NestedLevel>(mFlags & NESTED_MASK);
    }

    PriorityValue Priority() const {
      return static_cast<PriorityValue>((mFlags & PRIO_MASK) >> 2);
    }

    MessageCompression Compression() const {
      return ((mFlags & COMPRESS_BIT)      ? COMPRESSION_ENABLED
              : (mFlags & COMPRESSALL_BIT) ? COMPRESSION_ALL
                                           : COMPRESSION_NONE);
    }

    bool IsConstructor() const { return (mFlags & CONSTRUCTOR_BIT) != 0; }
    bool IsSync() const { return (mFlags & SYNC_BIT) != 0; }
    bool IsInterrupt() const { return (mFlags & INTERRUPT_BIT) != 0; }
    bool IsReply() const { return (mFlags & REPLY_BIT) != 0; }

    bool IsReplyError() const { return (mFlags & REPLY_ERROR_BIT) != 0; }

#ifdef MOZ_TASK_TRACER
    bool IsTaskTracer() const { return (mFlags & TASKTRACER_BIT) != 0; }
#endif

   private:
    void SetSync() { mFlags |= SYNC_BIT; }
    void SetInterrupt() { mFlags |= INTERRUPT_BIT; }
    void SetReply() { mFlags |= REPLY_BIT; }
    void SetReplyError() { mFlags |= REPLY_ERROR_BIT; }

#ifdef MOZ_TASK_TRACER
    void SetTaskTracer() { mFlags |= TASKTRACER_BIT; }
#endif

    uint32_t mFlags;
  };

  virtual ~Message();

  Message();

  // Initialize a message with a user-defined type, priority value, and
  // destination WebView ID.
  //
  // NOTE: `recordWriteLatency` is only passed by IPDL generated message code,
  // and is used to trigger the IPC_WRITE_LATENCY_MS telemetry.
  Message(int32_t routing_id, msgid_t type,
          uint32_t segmentCapacity = 0,  // 0 for the default capacity.
          HeaderFlags flags = HeaderFlags(), bool recordWriteLatency = false);

  Message(const char* data, int data_len);

  Message(const Message& other) = delete;
  Message(Message&& other);
  Message& operator=(const Message& other) = delete;
  Message& operator=(Message&& other);

  void CopyFrom(const Message& other);

  // Helper method for the common case (default segmentCapacity, recording
  // the write latency of messages) of IPDL message creation.  This helps
  // move the malloc and some of the parameter setting out of autogenerated
  // code.
  static Message* IPDLMessage(int32_t routing_id, msgid_t type,
                              HeaderFlags flags);

  // One-off constructors for special error-handling messages.
  static Message* ForSyncDispatchError(NestedLevel level);
  static Message* ForInterruptDispatchError();

  NestedLevel nested_level() const { return header()->flags.Level(); }

  PriorityValue priority() const { return header()->flags.Priority(); }

  bool is_constructor() const { return header()->flags.IsConstructor(); }

  // True if this is a synchronous message.
  bool is_sync() const { return header()->flags.IsSync(); }

  // True if this is a synchronous message.
  bool is_interrupt() const { return header()->flags.IsInterrupt(); }

  MessageCompression compress_type() const {
    return header()->flags.Compression();
  }

  bool is_reply() const { return header()->flags.IsReply(); }

  bool is_reply_error() const { return header()->flags.IsReplyError(); }

  bool is_valid() const { return !!header(); }

  msgid_t type() const { return header()->type; }

  int32_t routing_id() const { return header()->routing; }

  void set_routing_id(int32_t new_id) { header()->routing = new_id; }

  int32_t transaction_id() const { return header()->txid; }

  void set_transaction_id(int32_t txid) { header()->txid = txid; }

  uint32_t interrupt_remote_stack_depth_guess() const {
    return header()->interrupt_remote_stack_depth_guess;
  }

  void set_interrupt_remote_stack_depth_guess(uint32_t depth) {
    DCHECK(is_interrupt());
    header()->interrupt_remote_stack_depth_guess = depth;
  }

  uint32_t interrupt_local_stack_depth() const {
    return header()->interrupt_local_stack_depth;
  }

  void set_interrupt_local_stack_depth(uint32_t depth) {
    DCHECK(is_interrupt());
    header()->interrupt_local_stack_depth = depth;
  }

  int32_t seqno() const { return header()->seqno; }

  void set_seqno(int32_t aSeqno) { header()->seqno = aSeqno; }

  const char* name() const { return StringFromIPCMessageType(type()); }

  const mozilla::TimeStamp& create_time() const { return create_time_; }

#if defined(OS_POSIX)
  uint32_t num_fds() const;
#endif

  template <class T>
  static bool Dispatch(const Message* msg, T* obj, void (T::*func)()) {
    (obj->*func)();
    return true;
  }

  template <class T>
  static bool Dispatch(const Message* msg, T* obj, void (T::*func)() const) {
    (obj->*func)();
    return true;
  }

  template <class T>
  static bool Dispatch(const Message* msg, T* obj,
                       void (T::*func)(const Message&)) {
    (obj->*func)(*msg);
    return true;
  }

  template <class T>
  static bool Dispatch(const Message* msg, T* obj,
                       void (T::*func)(const Message&) const) {
    (obj->*func)(*msg);
    return true;
  }

  // We should not be sending messages that are smaller than our header size.
  void AssertAsLargeAsHeader() const;

  // Used for async messages with no parameters.
  static void Log(const Message* msg, std::wstring* l) {}

  static int HeaderSizeFromData(const char* range_start,
                                const char* range_end) {
#ifdef MOZ_TASK_TRACER
    return ((static_cast<unsigned int>(range_end - range_start) >=
             sizeof(Header)) &&
            (reinterpret_cast<const Header*>(range_start)
                 ->flags.IsTaskTracer()))
               ? sizeof(HeaderTaskTracer)
               : sizeof(Header);
#else
    return sizeof(Header);
#endif
  }

  // Figure out how big the message starting at range_start is. Returns 0 if
  // there's no enough data to determine (i.e., if [range_start, range_end) does
  // not contain enough of the message header to know the size).
  static uint32_t MessageSize(const char* range_start, const char* range_end) {
    return Pickle::MessageSize(HeaderSizeFromData(range_start, range_end),
                               range_start, range_end);
  }

#if defined(OS_POSIX)
  // On POSIX, a message supports reading / writing FileDescriptor objects.
  // This is used to pass a file descriptor to the peer of an IPC channel.

  // Add a descriptor to the end of the set. Returns false iff the set is full.
  bool WriteFileDescriptor(const base::FileDescriptor& descriptor);
  // Get a file descriptor from the message. Returns false on error.
  //   iter: a Pickle iterator to the current location in the message.
  bool ReadFileDescriptor(PickleIterator* iter,
                          base::FileDescriptor* descriptor) const;

#  if defined(OS_MACOSX)
  void set_fd_cookie(uint32_t cookie) { header()->cookie = cookie; }
  uint32_t fd_cookie() const { return header()->cookie; }
#  endif
#endif

  friend class Channel;
  friend class MessageReplyDeserializer;
  friend class SyncMessage;
#ifdef FUZZING
  friend class mozilla::ipc::Faulty;
#endif
  friend class mozilla::ipc::MiniTransceiver;

#ifdef MOZ_TASK_TRACER
  void TaskTracerDispatch();
  class AutoTaskTracerRun : public mozilla::tasktracer::AutoSaveCurTraceInfo {
    Message& mMsg;
    uint64_t mTaskId;
    uint64_t mSourceEventId;

   public:
    explicit AutoTaskTracerRun(Message& aMsg);
    ~AutoTaskTracerRun();
  };
#endif

#if !defined(OS_MACOSX)
 protected:
#endif

  struct Header : Pickle::Header {
    int32_t routing;    // ID of the view that this message is destined for
    msgid_t type;       // specifies the user-defined message type
    HeaderFlags flags;  // specifies control flags for the message
#if defined(OS_POSIX)
    uint32_t num_fds;  // the number of descriptors included with this message
#  if defined(OS_MACOSX)
    uint32_t cookie;  // cookie to ACK that the descriptors have been read.
#  endif
#endif
    union {
      // For Interrupt messages, a guess at what the *other* side's stack depth
      // is.
      uint32_t interrupt_remote_stack_depth_guess;

      // For RPC and Urgent messages, a transaction ID for message ordering.
      int32_t txid;
    };
    // The actual local stack depth.
    uint32_t interrupt_local_stack_depth;
    // Sequence number
    int32_t seqno;
  };

#ifdef MOZ_TASK_TRACER
  /**
   * The type is used as headers of Messages only if TaskTracer is
   * enabled, or type |Header| would be used instead.
   */
  struct HeaderTaskTracer : public Header {
    uint64_t task_id;
    uint64_t source_event_id;
    uint64_t parent_task_id;
    mozilla::tasktracer::SourceEventType source_event_type;
  };
#endif

#ifdef MOZ_TASK_TRACER
  bool UseTaskTracerHeader() const {
    return sizeof(HeaderTaskTracer) == (size() - payload_size());
  }

  Header* header() {
    return UseTaskTracerHeader() ? headerT<HeaderTaskTracer>()
                                 : headerT<Header>();
  }
  const Header* header() const {
    return UseTaskTracerHeader() ? headerT<HeaderTaskTracer>()
                                 : headerT<Header>();
  }
#else
  Header* header() { return headerT<Header>(); }
  const Header* header() const { return headerT<Header>(); }
#endif

#if defined(OS_POSIX)
  // The set of file descriptors associated with this message.
  RefPtr<FileDescriptorSet> file_descriptor_set_;

  // Ensure that a FileDescriptorSet is allocated
  void EnsureFileDescriptorSet();

  FileDescriptorSet* file_descriptor_set() {
    EnsureFileDescriptorSet();
    return file_descriptor_set_.get();
  }
  const FileDescriptorSet* file_descriptor_set() const {
    return file_descriptor_set_.get();
  }
#endif

  mozilla::TimeStamp create_time_;
};

class MessageInfo {
 public:
  typedef uint32_t msgid_t;

  explicit MessageInfo(const Message& aMsg)
      : mSeqno(aMsg.seqno()), mType(aMsg.type()) {}

  int32_t seqno() const { return mSeqno; }
  msgid_t type() const { return mType; }

 private:
  int32_t mSeqno;
  msgid_t mType;
};

//------------------------------------------------------------------------------

}  // namespace IPC

enum SpecialRoutingIDs {
  // indicates that we don't have a routing ID yet.
  MSG_ROUTING_NONE = kint32min,

  // indicates a general message not sent to a particular tab.
  MSG_ROUTING_CONTROL = kint32max
};

#define IPC_REPLY_ID 0xFFF0    // Special message id for replies
#define IPC_LOGGING_ID 0xFFF1  // Special message id for logging

#endif  // CHROME_COMMON_IPC_MESSAGE_H__