summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/pepper/pepper_camera_device_host.cc
blob: 81381b399e972ae68187b1d5c579cc7e3247bf53 (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
// Copyright 2015 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 "content/renderer/pepper/pepper_camera_device_host.h"

#include "content/renderer/pepper/pepper_platform_camera_device.h"
#include "content/renderer/pepper/renderer_ppapi_host_impl.h"
#include "content/renderer/render_frame_impl.h"
#include "ppapi/host/dispatch_host_message.h"
#include "ppapi/proxy/ppapi_messages.h"

namespace content {

PepperCameraDeviceHost::PepperCameraDeviceHost(RendererPpapiHostImpl* host,
                                               PP_Instance instance,
                                               PP_Resource resource)
    : ResourceHost(host->GetPpapiHost(), instance, resource),
      renderer_ppapi_host_(host) {
}

PepperCameraDeviceHost::~PepperCameraDeviceHost() {
  DetachPlatformCameraDevice();
}

bool PepperCameraDeviceHost::Init() {
  return !!renderer_ppapi_host_->GetPluginInstance(pp_instance());
}

int32_t PepperCameraDeviceHost::OnResourceMessageReceived(
    const IPC::Message& msg,
    ppapi::host::HostMessageContext* context) {
  int32_t result = PP_ERROR_FAILED;

  PPAPI_BEGIN_MESSAGE_MAP(PepperCameraDeviceHost, msg)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_CameraDevice_Open, OnOpen)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
        PpapiHostMsg_CameraDevice_GetSupportedVideoCaptureFormats,
        OnGetSupportedVideoCaptureFormats)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_CameraDevice_Close,
                                        OnClose)
  PPAPI_END_MESSAGE_MAP()
  return result;
}

void PepperCameraDeviceHost::OnInitialized(bool succeeded) {
  if (!open_reply_context_.is_valid())
    return;

  if (succeeded) {
    open_reply_context_.params.set_result(PP_OK);
  } else {
    DetachPlatformCameraDevice();
    open_reply_context_.params.set_result(PP_ERROR_FAILED);
  }

  host()->SendReply(open_reply_context_,
                    PpapiPluginMsg_CameraDevice_OpenReply());
  open_reply_context_ = ppapi::host::ReplyMessageContext();
}

void PepperCameraDeviceHost::OnVideoCaptureFormatsEnumerated(
    const std::vector<PP_VideoCaptureFormat>& formats) {
  if (!video_capture_formats_reply_context_.is_valid())
    return;

  if (formats.size() > 0)
    video_capture_formats_reply_context_.params.set_result(PP_OK);
  else
    video_capture_formats_reply_context_.params.set_result(PP_ERROR_FAILED);
  host()->SendReply(
      video_capture_formats_reply_context_,
      PpapiPluginMsg_CameraDevice_GetSupportedVideoCaptureFormatsReply(
          formats));
  video_capture_formats_reply_context_ = ppapi::host::ReplyMessageContext();
}

int32_t PepperCameraDeviceHost::OnOpen(ppapi::host::HostMessageContext* context,
                                       const std::string& device_id) {
  if (open_reply_context_.is_valid())
    return PP_ERROR_INPROGRESS;

  if (platform_camera_device_.get())
    return PP_ERROR_FAILED;

  GURL document_url = renderer_ppapi_host_->GetDocumentURL(pp_instance());
  if (!document_url.is_valid())
    return PP_ERROR_FAILED;

  platform_camera_device_.reset(new PepperPlatformCameraDevice(
      renderer_ppapi_host_->GetRenderFrameForInstance(pp_instance())
          ->GetRoutingID(),
      device_id, this));

  open_reply_context_ = context->MakeReplyMessageContext();

  return PP_OK_COMPLETIONPENDING;
}

int32_t PepperCameraDeviceHost::OnClose(
    ppapi::host::HostMessageContext* context) {
  DetachPlatformCameraDevice();
  return PP_OK;
}

int32_t PepperCameraDeviceHost::OnGetSupportedVideoCaptureFormats(
    ppapi::host::HostMessageContext* context) {
  if (video_capture_formats_reply_context_.is_valid())
    return PP_ERROR_INPROGRESS;
  if (!platform_camera_device_)
    return PP_ERROR_FAILED;

  video_capture_formats_reply_context_ = context->MakeReplyMessageContext();
  platform_camera_device_->GetSupportedVideoCaptureFormats();

  return PP_OK_COMPLETIONPENDING;
}

void PepperCameraDeviceHost::DetachPlatformCameraDevice() {
  if (platform_camera_device_) {
    platform_camera_device_->DetachEventHandler();
    platform_camera_device_.reset();
  }
}

}  // namespace content