summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppb_broker_impl.cc
blob: 16aa316080384df6becd9f4ac952575c2f25364c (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
// Copyright (c) 2011 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.

#include "webkit/plugins/ppapi/ppb_broker_impl.h"

#include "base/logging.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/plugin_module.h"

namespace webkit {
namespace ppapi {

namespace {

// PPB_BrokerTrusted ----------------------------------------------------

PP_Resource CreateTrusted(PP_Instance instance_id) {
  PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
  if (!instance)
    return 0;
  scoped_refptr<PPB_Broker_Impl> broker(new PPB_Broker_Impl(instance));
  return broker->GetReference();
}

PP_Bool IsBrokerTrusted(PP_Resource resource) {
  scoped_refptr<PPB_Broker_Impl> broker =
      Resource::GetAs<PPB_Broker_Impl>(resource);
  return BoolToPPBool(!!broker);
}

int32_t Connect(PP_Resource broker_id,
                PP_CompletionCallback connect_callback) {
  scoped_refptr<PPB_Broker_Impl> broker =
      Resource::GetAs<PPB_Broker_Impl>(broker_id);
  if (!broker)
    return PP_ERROR_BADRESOURCE;
  if (!connect_callback.func) {
    // Synchronous calls are not supported.
    return PP_ERROR_BADARGUMENT;
  }
  return broker->Connect(broker->instance()->delegate(), connect_callback);
}

int32_t GetHandle(PP_Resource broker_id, int32_t* handle) {
  scoped_refptr<PPB_Broker_Impl> broker =
      Resource::GetAs<PPB_Broker_Impl>(broker_id);
  if (!broker)
    return PP_ERROR_BADRESOURCE;
  if (!handle)
    return PP_ERROR_BADARGUMENT;
  return broker->GetHandle(handle);
}

const PPB_BrokerTrusted ppb_brokertrusted = {
  &CreateTrusted,
  &IsBrokerTrusted,
  &Connect,
  &GetHandle,
};

// TODO(ddorwin): Put conversion functions in a common place and/or add an
// invalid value to sync_socket.h.
int32_t PlatformFileToInt(base::PlatformFile handle) {
#if defined(OS_WIN)
  return static_cast<int32_t>(reinterpret_cast<intptr_t>(handle));
#elif defined(OS_POSIX)
  return handle;
#else
  #error Not implemented.
#endif
}

}  // namespace

// PPB_Broker_Impl ------------------------------------------------------

PPB_Broker_Impl::PPB_Broker_Impl(PluginInstance* instance)
    : Resource(instance),
      broker_(NULL),
      connect_callback_(),
      pipe_handle_(PlatformFileToInt(base::kInvalidPlatformFileValue)) {
}

PPB_Broker_Impl::~PPB_Broker_Impl() {
  if (broker_) {
    broker_->Disconnect(this);
    broker_ = NULL;
  }

  // The plugin owns the handle.
  pipe_handle_ = PlatformFileToInt(base::kInvalidPlatformFileValue);
}

const PPB_BrokerTrusted* PPB_Broker_Impl::GetTrustedInterface() {
  return &ppb_brokertrusted;
}

int32_t PPB_Broker_Impl::Connect(
    PluginDelegate* plugin_delegate,
    PP_CompletionCallback connect_callback) {
  // TODO(ddorwin): Return PP_ERROR_FAILED if plugin is in-process.

  if (broker_) {
    // May only be called once.
    return PP_ERROR_FAILED;
  }

  broker_ = plugin_delegate->ConnectToPpapiBroker(this);
  if (!broker_)
    return PP_ERROR_FAILED;

  PP_Resource resource_id = GetReferenceNoAddRef();
  CHECK(resource_id);
  connect_callback_ = new TrackedCompletionCallback(
      instance()->module()->GetCallbackTracker(), resource_id,
      connect_callback);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_Broker_Impl::GetHandle(int32_t* handle) {
  *handle = pipe_handle_;
  return PP_OK;
}

PPB_Broker_Impl* PPB_Broker_Impl::AsPPB_Broker_Impl() {
  return this;
}

// Transfers ownership of the handle to the plugin.
void PPB_Broker_Impl::BrokerConnected(int32_t handle, int32_t result) {
  DCHECK(result == PP_OK ||
         handle == PlatformFileToInt(base::kInvalidPlatformFileValue));

  pipe_handle_ = handle;

  // Synchronous calls are not supported.
  DCHECK(connect_callback_.get() && !connect_callback_->completed());

  scoped_refptr<TrackedCompletionCallback> callback;
  callback.swap(connect_callback_);
  callback->Run(result);  // Will complete abortively if necessary.
}

}  // namespace ppapi
}  // namespace webkit