summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/talk_resource.cc
blob: b076064df6e1304c1084e4cec4be3b97db4a6e11 (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
// Copyright (c) 2012 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 "ppapi/proxy/talk_resource.h"

#include "base/bind.h"
#include "ppapi/proxy/ppapi_messages.h"

namespace ppapi {
namespace proxy {

TalkResource::TalkResource(Connection connection, PP_Instance instance)
    : PluginResource(connection, instance),
      event_callback_(NULL),
      event_callback_user_data_(NULL) {
  SendCreate(BROWSER, PpapiHostMsg_Talk_Create());
}

TalkResource::~TalkResource() {
}

thunk::PPB_Talk_Private_API* TalkResource::AsPPB_Talk_Private_API() {
  return this;
}

int32_t TalkResource::RequestPermission(
    PP_TalkPermission permission,
    scoped_refptr<TrackedCallback> callback) {
  if (TrackedCallback::IsPending(permission_callback_))
    return PP_ERROR_INPROGRESS;

  permission_callback_ = callback;

  Call<PpapiPluginMsg_Talk_RequestPermissionReply>(
      BROWSER,
      PpapiHostMsg_Talk_RequestPermission(permission),
      base::Bind(&TalkResource::OnRequestPermissionReply,
                 base::Unretained(this)));
  return PP_OK_COMPLETIONPENDING;
}

int32_t TalkResource::StartRemoting(PP_TalkEventCallback event_callback,
                                    void* user_data,
                                    scoped_refptr<TrackedCallback> callback) {
  if (TrackedCallback::IsPending(start_callback_) ||
      event_callback_ != NULL)
    return PP_ERROR_INPROGRESS;

  start_callback_ = callback;
  event_callback_ = event_callback;
  event_callback_user_data_ = user_data;

  Call<PpapiPluginMsg_Talk_StartRemotingReply>(
      BROWSER,
      PpapiHostMsg_Talk_StartRemoting(),
      base::Bind(&TalkResource::OnStartRemotingReply,
                 base::Unretained(this)));
  return PP_OK_COMPLETIONPENDING;
}

int32_t TalkResource::StopRemoting(scoped_refptr<TrackedCallback> callback) {
  if (TrackedCallback::IsPending(stop_callback_))
    return PP_ERROR_INPROGRESS;

  if (event_callback_ == NULL)
    return PP_ERROR_FAILED;

  stop_callback_ = callback;

  Call<PpapiPluginMsg_Talk_StopRemotingReply>(
      BROWSER,
      PpapiHostMsg_Talk_StopRemoting(),
      base::Bind(&TalkResource::OnStopRemotingReply,
                 base::Unretained(this)));
  return PP_OK_COMPLETIONPENDING;
}

void TalkResource::OnReplyReceived(const ResourceMessageReplyParams& params,
                                   const IPC::Message& msg) {
  PPAPI_BEGIN_MESSAGE_MAP(TalkResource, msg)
    PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(
        PpapiPluginMsg_Talk_NotifyEvent,
        OnNotifyEvent)
    PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(
        PluginResource::OnReplyReceived(params, msg))
  PPAPI_END_MESSAGE_MAP()
}

void TalkResource::OnNotifyEvent(const ResourceMessageReplyParams& params,
                                 PP_TalkEvent event) {
  if (event_callback_ != NULL)
    event_callback_(event_callback_user_data_, event);
}

void TalkResource::OnRequestPermissionReply(
    const ResourceMessageReplyParams& params) {
  permission_callback_->Run(params.result());
}

void TalkResource::OnStartRemotingReply(
    const ResourceMessageReplyParams& params) {
  start_callback_->Run(params.result());
}

void TalkResource::OnStopRemotingReply(
    const ResourceMessageReplyParams& params) {
  event_callback_ = NULL;
  event_callback_user_data_ = NULL;
  stop_callback_->Run(params.result());
}

}  // namespace proxy
}  // namespace ppapi