Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / pepper_camera_device_host.cc
blobcf199e23c3a393e2e05c8a15d31e13d1ee9da423
1 // Copyright 2015 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.
5 #include "content/renderer/pepper/pepper_camera_device_host.h"
7 #include "content/renderer/pepper/pepper_platform_camera_device.h"
8 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
9 #include "content/renderer/render_frame_impl.h"
10 #include "ppapi/host/dispatch_host_message.h"
11 #include "ppapi/proxy/ppapi_messages.h"
13 namespace content {
15 PepperCameraDeviceHost::PepperCameraDeviceHost(RendererPpapiHostImpl* host,
16 PP_Instance instance,
17 PP_Resource resource)
18 : ResourceHost(host->GetPpapiHost(), instance, resource),
19 renderer_ppapi_host_(host) {
22 PepperCameraDeviceHost::~PepperCameraDeviceHost() {
23 DetachPlatformCameraDevice();
26 bool PepperCameraDeviceHost::Init() {
27 return !!renderer_ppapi_host_->GetPluginInstance(pp_instance());
30 int32_t PepperCameraDeviceHost::OnResourceMessageReceived(
31 const IPC::Message& msg,
32 ppapi::host::HostMessageContext* context) {
33 int32_t result = PP_ERROR_FAILED;
35 PPAPI_BEGIN_MESSAGE_MAP(PepperCameraDeviceHost, msg)
36 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_CameraDevice_Open, OnOpen)
37 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
38 PpapiHostMsg_CameraDevice_GetSupportedVideoCaptureFormats,
39 OnGetSupportedVideoCaptureFormats)
40 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_CameraDevice_Close,
41 OnClose)
42 PPAPI_END_MESSAGE_MAP()
43 return result;
46 void PepperCameraDeviceHost::OnInitialized(bool succeeded) {
47 if (!open_reply_context_.is_valid())
48 return;
50 if (succeeded) {
51 open_reply_context_.params.set_result(PP_OK);
52 } else {
53 DetachPlatformCameraDevice();
54 open_reply_context_.params.set_result(PP_ERROR_FAILED);
57 host()->SendReply(open_reply_context_,
58 PpapiPluginMsg_CameraDevice_OpenReply());
59 open_reply_context_ = ppapi::host::ReplyMessageContext();
62 void PepperCameraDeviceHost::OnVideoCaptureFormatsEnumerated(
63 const std::vector<PP_VideoCaptureFormat>& formats) {
64 if (!video_capture_formats_reply_context_.is_valid())
65 return;
67 if (formats.size() > 0)
68 video_capture_formats_reply_context_.params.set_result(PP_OK);
69 else
70 video_capture_formats_reply_context_.params.set_result(PP_ERROR_FAILED);
71 host()->SendReply(
72 video_capture_formats_reply_context_,
73 PpapiPluginMsg_CameraDevice_GetSupportedVideoCaptureFormatsReply(
74 formats));
75 video_capture_formats_reply_context_ = ppapi::host::ReplyMessageContext();
78 int32_t PepperCameraDeviceHost::OnOpen(ppapi::host::HostMessageContext* context,
79 const std::string& device_id) {
80 if (open_reply_context_.is_valid())
81 return PP_ERROR_INPROGRESS;
83 if (platform_camera_device_.get())
84 return PP_ERROR_FAILED;
86 GURL document_url = renderer_ppapi_host_->GetDocumentURL(pp_instance());
87 if (!document_url.is_valid())
88 return PP_ERROR_FAILED;
90 platform_camera_device_.reset(new PepperPlatformCameraDevice(
91 renderer_ppapi_host_->GetRenderFrameForInstance(pp_instance())
92 ->GetRoutingID(),
93 device_id, document_url, this));
95 open_reply_context_ = context->MakeReplyMessageContext();
97 return PP_OK_COMPLETIONPENDING;
100 int32_t PepperCameraDeviceHost::OnClose(
101 ppapi::host::HostMessageContext* context) {
102 DetachPlatformCameraDevice();
103 return PP_OK;
106 int32_t PepperCameraDeviceHost::OnGetSupportedVideoCaptureFormats(
107 ppapi::host::HostMessageContext* context) {
108 if (video_capture_formats_reply_context_.is_valid())
109 return PP_ERROR_INPROGRESS;
110 if (!platform_camera_device_)
111 return PP_ERROR_FAILED;
113 video_capture_formats_reply_context_ = context->MakeReplyMessageContext();
114 platform_camera_device_->GetSupportedVideoCaptureFormats();
116 return PP_OK_COMPLETIONPENDING;
119 void PepperCameraDeviceHost::DetachPlatformCameraDevice() {
120 if (platform_camera_device_) {
121 platform_camera_device_->DetachEventHandler();
122 platform_camera_device_.reset();
126 } // namespace content