[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "ppapi/proxy/talk_resource.h" |
| 6 | |
| 7 | #include "ppapi/proxy/ppapi_messages.h" |
| 8 | |
| 9 | namespace ppapi { |
| 10 | namespace proxy { |
| 11 | |
| 12 | TalkResource::TalkResource(Connection connection, PP_Instance instance) |
[email protected] | ce97d745 | 2013-06-07 19:02:09 | [diff] [blame^] | 13 | : PluginResource(connection, instance), |
| 14 | event_callback_(NULL), |
| 15 | event_callback_user_data_(NULL) { |
| 16 | SendCreate(BROWSER, PpapiHostMsg_Talk_Create()); |
[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | TalkResource::~TalkResource() { |
| 20 | } |
| 21 | |
| 22 | thunk::PPB_Talk_Private_API* TalkResource::AsPPB_Talk_Private_API() { |
| 23 | return this; |
| 24 | } |
| 25 | |
[email protected] | ce97d745 | 2013-06-07 19:02:09 | [diff] [blame^] | 26 | int32_t TalkResource::RequestPermission( |
| 27 | PP_TalkPermission permission, |
| 28 | scoped_refptr<TrackedCallback> callback) { |
| 29 | if (TrackedCallback::IsPending(permission_callback_)) |
[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 30 | return PP_ERROR_INPROGRESS; |
[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 31 | |
[email protected] | ce97d745 | 2013-06-07 19:02:09 | [diff] [blame^] | 32 | permission_callback_ = callback; |
[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 33 | |
[email protected] | ce97d745 | 2013-06-07 19:02:09 | [diff] [blame^] | 34 | Call<PpapiPluginMsg_Talk_RequestPermissionReply>( |
[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 35 | BROWSER, |
[email protected] | ce97d745 | 2013-06-07 19:02:09 | [diff] [blame^] | 36 | PpapiHostMsg_Talk_RequestPermission(permission), |
| 37 | base::Bind(&TalkResource::OnRequestPermissionReply, |
| 38 | base::Unretained(this))); |
[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 39 | return PP_OK_COMPLETIONPENDING; |
| 40 | } |
| 41 | |
[email protected] | ce97d745 | 2013-06-07 19:02:09 | [diff] [blame^] | 42 | int32_t TalkResource::StartRemoting(PP_TalkEventCallback event_callback, |
| 43 | void* user_data, |
| 44 | scoped_refptr<TrackedCallback> callback) { |
| 45 | if (TrackedCallback::IsPending(start_callback_) || |
| 46 | event_callback_ != NULL) |
| 47 | return PP_ERROR_INPROGRESS; |
| 48 | |
| 49 | start_callback_ = callback; |
| 50 | event_callback_ = event_callback; |
| 51 | event_callback_user_data_ = user_data; |
| 52 | |
| 53 | Call<PpapiPluginMsg_Talk_StartRemotingReply>( |
| 54 | BROWSER, |
| 55 | PpapiHostMsg_Talk_StartRemoting(), |
| 56 | base::Bind(&TalkResource::OnStartRemotingReply, |
| 57 | base::Unretained(this))); |
| 58 | return PP_OK_COMPLETIONPENDING; |
| 59 | } |
| 60 | |
| 61 | int32_t TalkResource::StopRemoting(scoped_refptr<TrackedCallback> callback) { |
| 62 | if (TrackedCallback::IsPending(stop_callback_)) |
| 63 | return PP_ERROR_INPROGRESS; |
| 64 | |
| 65 | if (event_callback_ == NULL) |
| 66 | return PP_ERROR_FAILED; |
| 67 | |
| 68 | stop_callback_ = callback; |
| 69 | |
| 70 | Call<PpapiPluginMsg_Talk_StopRemotingReply>( |
| 71 | BROWSER, |
| 72 | PpapiHostMsg_Talk_StopRemoting(), |
| 73 | base::Bind(&TalkResource::OnStopRemotingReply, |
| 74 | base::Unretained(this))); |
| 75 | return PP_OK_COMPLETIONPENDING; |
| 76 | } |
| 77 | |
| 78 | void TalkResource::OnReplyReceived(const ResourceMessageReplyParams& params, |
| 79 | const IPC::Message& msg) { |
| 80 | IPC_BEGIN_MESSAGE_MAP(TalkResource, msg) |
| 81 | PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( |
| 82 | PpapiPluginMsg_Talk_NotifyEvent, |
| 83 | OnNotifyEvent) |
| 84 | PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED( |
| 85 | PluginResource::OnReplyReceived(params, msg)) |
| 86 | IPC_END_MESSAGE_MAP() |
| 87 | } |
| 88 | |
| 89 | void TalkResource::OnNotifyEvent(const ResourceMessageReplyParams& params, |
| 90 | PP_TalkEvent event) { |
| 91 | if (event_callback_ != NULL) |
| 92 | event_callback_(event_callback_user_data_, event); |
| 93 | } |
| 94 | |
| 95 | void TalkResource::OnRequestPermissionReply( |
[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 96 | const ResourceMessageReplyParams& params) { |
[email protected] | ce97d745 | 2013-06-07 19:02:09 | [diff] [blame^] | 97 | permission_callback_->Run(params.result()); |
| 98 | } |
| 99 | |
| 100 | void TalkResource::OnStartRemotingReply( |
| 101 | const ResourceMessageReplyParams& params) { |
| 102 | start_callback_->Run(params.result()); |
| 103 | } |
| 104 | |
| 105 | void TalkResource::OnStopRemotingReply( |
| 106 | const ResourceMessageReplyParams& params) { |
| 107 | event_callback_ = NULL; |
| 108 | event_callback_user_data_ = NULL; |
| 109 | stop_callback_->Run(params.result()); |
[email protected] | f20cc69 | 2012-11-28 19:21:20 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace proxy |
| 113 | } // namespace ppapi |