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