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 "ppapi/proxy/camera_device_resource.h"
7 #include "ppapi/proxy/camera_capabilities_resource.h"
8 #include "ppapi/proxy/plugin_resource_tracker.h"
9 #include "ppapi/proxy/ppapi_messages.h"
10 #include "ppapi/shared_impl/var.h"
15 CameraDeviceResource::CameraDeviceResource(Connection connection
,
17 : PluginResource(connection
, instance
),
18 open_state_(OpenState::BEFORE_OPEN
) {
19 SendCreate(RENDERER
, PpapiHostMsg_CameraDevice_Create());
22 CameraDeviceResource::~CameraDeviceResource() {
25 int32_t CameraDeviceResource::Open(
27 const scoped_refptr
<TrackedCallback
>& callback
) {
28 if (open_state_
!= OpenState::BEFORE_OPEN
)
29 return PP_ERROR_FAILED
;
31 if (TrackedCallback::IsPending(open_callback_
))
32 return PP_ERROR_INPROGRESS
;
34 scoped_refptr
<StringVar
> source_string_var(StringVar::FromPPVar(device_id
));
35 if (!source_string_var
|| source_string_var
->value().empty())
36 return PP_ERROR_BADARGUMENT
;
38 open_callback_
= callback
;
40 Call
<PpapiPluginMsg_CameraDevice_OpenReply
>(
41 RENDERER
, PpapiHostMsg_CameraDevice_Open(source_string_var
->value()),
42 base::Bind(&CameraDeviceResource::OnPluginMsgOpenReply
,
43 base::Unretained(this)));
44 return PP_OK_COMPLETIONPENDING
;
47 void CameraDeviceResource::Close() {
48 if (open_state_
== OpenState::CLOSED
)
51 if (TrackedCallback::IsPending(open_callback_
)) {
52 open_callback_
->PostAbort();
53 open_callback_
= nullptr;
56 if (TrackedCallback::IsPending(get_capabilities_callback_
)) {
57 get_capabilities_callback_
->PostAbort();
58 get_capabilities_callback_
= nullptr;
61 Post(RENDERER
, PpapiHostMsg_CameraDevice_Close());
63 open_state_
= OpenState::CLOSED
;
66 int32_t CameraDeviceResource::GetCameraCapabilities(
67 PP_Resource
* capabilities
,
68 const scoped_refptr
<TrackedCallback
>& callback
) {
70 return PP_ERROR_FAILED
;
72 if (TrackedCallback::IsPending(get_capabilities_callback_
))
73 return PP_ERROR_INPROGRESS
;
75 if (camera_capabilities_
.get()) {
76 *capabilities
= camera_capabilities_
->GetReference();
80 get_capabilities_callback_
= callback
;
81 Call
<PpapiPluginMsg_CameraDevice_GetSupportedVideoCaptureFormatsReply
>(
82 RENDERER
, PpapiHostMsg_CameraDevice_GetSupportedVideoCaptureFormats(),
83 base::Bind(&CameraDeviceResource::OnPluginMsgGetVideoCaptureFormatsReply
,
84 base::Unretained(this), capabilities
));
86 return PP_OK_COMPLETIONPENDING
;
89 void CameraDeviceResource::OnPluginMsgOpenReply(
90 const ResourceMessageReplyParams
& params
) {
91 // The callback may have been aborted by Close().
92 if (TrackedCallback::IsPending(open_callback_
)) {
93 if (open_state_
== OpenState::BEFORE_OPEN
&& params
.result() == PP_OK
)
94 open_state_
= OpenState::OPENED
;
96 open_callback_
->Run(params
.result());
100 void CameraDeviceResource::OnPluginMsgGetVideoCaptureFormatsReply(
101 PP_Resource
* capabilities_output
,
102 const ResourceMessageReplyParams
& params
,
103 const std::vector
<PP_VideoCaptureFormat
>& formats
) {
104 if (!TrackedCallback::IsPending(get_capabilities_callback_
))
107 // Return camera capabilities.
108 int32_t result
= params
.result();
109 scoped_refptr
<TrackedCallback
> callback
;
110 callback
.swap(get_capabilities_callback_
);
111 if (result
== PP_OK
) {
112 camera_capabilities_
=
113 new CameraCapabilitiesResource(pp_instance(), formats
);
114 *capabilities_output
= camera_capabilities_
->GetReference();
116 callback
->Run(result
== PP_OK
? PP_OK
: PP_ERROR_FAILED
);