summaryrefslogtreecommitdiffstats
path: root/ipc/attachment_broker_unprivileged.h
blob: f6d520de6e3bb1a0ad4faa455ce450bf8c93c192 (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
// Copyright 2015 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 IPC_ATTACHMENT_BROKER_UNPRIVILEGED_H_
#define IPC_ATTACHMENT_BROKER_UNPRIVILEGED_H_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "ipc/attachment_broker.h"
#include "ipc/ipc_export.h"

namespace IPC {

class Endpoint;
class Sender;

// This abstract subclass of AttachmentBroker is intended for use in
// non-privileged processes.
class IPC_EXPORT AttachmentBrokerUnprivileged : public IPC::AttachmentBroker {
 public:
  AttachmentBrokerUnprivileged();
  ~AttachmentBrokerUnprivileged() override;

  // If there is no global attachment broker, makes a new
  // AttachmentBrokerUnprivileged and sets it as the global attachment broker.
  // This method is thread safe.
  static void CreateBrokerIfNeeded();

  // AttachmentBroker:
  void RegisterBrokerCommunicationChannel(Endpoint* endpoint) override;
  void DeregisterBrokerCommunicationChannel(Endpoint* endpoint) override;
  bool IsPrivilegedBroker() override;

 protected:
  IPC::Sender* get_sender() { return sender_; }

  // Errors that can be reported by subclasses.
  // These match tools/metrics/histograms/histograms.xml.
  // This enum is append-only.
  enum UMAError {
    // The brokerable attachment was successfully processed.
    SUCCESS = 0,
    // The brokerable attachment's destination was not the process that received
    // the attachment.
    WRONG_DESTINATION = 1,
    // An error occurred while trying to receive a Mach port with mach_msg().
    ERR_RECEIVE_MACH_MESSAGE = 2,
    ERROR_MAX
  };

  // Emits an UMA metric.
  void LogError(UMAError error);

 private:
  // |sender_| is used to send Messages to the privileged broker process.
  // |sender_| must live at least as long as this instance.
  IPC::Sender* sender_;
  DISALLOW_COPY_AND_ASSIGN(AttachmentBrokerUnprivileged);
};

}  // namespace IPC

#endif  // IPC_ATTACHMENT_BROKER_UNPRIVILEGED_H_