1 // Copyright 2019 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 #include "mojo/public/cpp/system/data_pipe.h"
6 
7 #include "base/logging.h"
8 
9 namespace mojo {
10 
11 namespace {
12 
CrashMojoResourceExhausted()13 NOINLINE void CrashMojoResourceExhausted() {
14   LOG(FATAL)
15       << "Failed to create data pipe due to MOJO_RESULT_RESOURCE_EXHAUSTED.";
16 }
17 
CrashIfResultNotOk(MojoResult result)18 void CrashIfResultNotOk(MojoResult result) {
19   if (LIKELY(result == MOJO_RESULT_OK))
20     return;
21 
22   // Include some extra information for resource exhausted failures.
23   if (result == MOJO_RESULT_RESOURCE_EXHAUSTED)
24     CrashMojoResourceExhausted();
25 
26   LOG(FATAL) << "Failed to create data pipe; result=" << result;
27 }
28 
29 }  // namespace
30 
DataPipe()31 DataPipe::DataPipe() {
32   MojoResult result =
33       CreateDataPipe(nullptr, &producer_handle, &consumer_handle);
34   CrashIfResultNotOk(result);
35 }
36 
DataPipe(uint32_t capacity_num_bytes)37 DataPipe::DataPipe(uint32_t capacity_num_bytes) {
38   MojoCreateDataPipeOptions options;
39   options.struct_size = sizeof(MojoCreateDataPipeOptions);
40   options.flags = MOJO_CREATE_DATA_PIPE_FLAG_NONE;
41   options.element_num_bytes = 1;
42   options.capacity_num_bytes = capacity_num_bytes;
43   MojoResult result =
44       CreateDataPipe(&options, &producer_handle, &consumer_handle);
45   CrashIfResultNotOk(result);
46 }
47 
DataPipe(const MojoCreateDataPipeOptions & options)48 DataPipe::DataPipe(const MojoCreateDataPipeOptions& options) {
49   MojoResult result =
50       CreateDataPipe(&options, &producer_handle, &consumer_handle);
51   CrashIfResultNotOk(result);
52 }
53 
~DataPipe()54 DataPipe::~DataPipe() {}
55 
56 }  // namespace mojo
57