Chromium Code Reviews| Index: ppapi/proxy/image_capture_resource.cc |
| diff --git a/ppapi/proxy/image_capture_resource.cc b/ppapi/proxy/image_capture_resource.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fb0432f2a8b020b0b5c62108c33507fa31782cb2 |
| --- /dev/null |
| +++ b/ppapi/proxy/image_capture_resource.cc |
| @@ -0,0 +1,105 @@ |
| +// 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 "ppapi/proxy/image_capture_resource.h" |
| + |
| +#include "ppapi/proxy/camera_capabilities_resource.h" |
| +#include "ppapi/proxy/plugin_resource_tracker.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "ppapi/shared_impl/var.h" |
| + |
| +namespace ppapi { |
| +namespace proxy { |
| + |
| +ImageCaptureResource::ImageCaptureResource(Connection connection, |
| + PP_Instance instance) |
| + : PluginResource(connection, instance), |
| + open_state_(BEFORE_OPEN) { |
| + SendCreate(RENDERER, PpapiHostMsg_ImageCapture_Create()); |
| +} |
| + |
| +ImageCaptureResource::~ImageCaptureResource() { |
| +} |
| + |
| +int32_t ImageCaptureResource::Open( |
| + PP_Var device_id, |
| + const scoped_refptr<TrackedCallback>& callback) { |
| + if (open_state_ != BEFORE_OPEN) |
| + return PP_ERROR_FAILED; |
| + |
| + if (TrackedCallback::IsPending(open_callback_)) |
| + return PP_ERROR_INPROGRESS; |
| + |
| + scoped_refptr<StringVar> source_string_var; |
| + source_string_var = StringVar::FromPPVar(device_id); |
| + if (!source_string_var || source_string_var->value().empty()) |
| + return PP_ERROR_BADARGUMENT; |
| + |
| + open_callback_ = callback; |
| + |
| + Call<PpapiPluginMsg_ImageCapture_OpenReply>( |
| + RENDERER, PpapiHostMsg_ImageCapture_Open(source_string_var->value()), |
| + base::Bind(&ImageCaptureResource::OnPluginMsgOpenReply, |
| + base::Unretained(this))); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +void ImageCaptureResource::Close() { |
| + if (open_state_ == CLOSED) |
| + return; |
| + |
| + if (TrackedCallback::IsPending(open_callback_)) { |
| + open_callback_->PostAbort(); |
| + open_callback_ = NULL; |
| + } |
| + |
|
Justin Chuang
2015/02/10 17:56:38
Note that it doesn't handle a race condition becau
dmichael (off chromium)
2015/02/10 18:12:32
Oh, thanks for bringing that up. I wasn't thinking
Justin Chuang
2015/02/10 19:02:08
Do you think it's worth the extra code to just for
dmichael (off chromium)
2015/02/10 19:07:47
Yes, I would recommend that. It's our convention f
|
| + Post(RENDERER, PpapiHostMsg_ImageCapture_Close()); |
| + |
| + open_state_ = CLOSED; |
| +} |
| + |
| +int32_t ImageCaptureResource::GetCameraCapabilities( |
| + PP_Resource* capabilities, |
| + const scoped_refptr<TrackedCallback>& callback) { |
| + if (!is_opened()) |
| + return PP_ERROR_FAILED; |
| + |
| + if (camera_capabilities_.get()) { |
| + *capabilities = camera_capabilities_->GetReference(); |
| + return PP_OK; |
| + } |
| + |
| + Call<PpapiPluginMsg_ImageCapture_GetSupportedPreviewSizesReply>( |
| + RENDERER, PpapiHostMsg_ImageCapture_GetSupportedPreviewSizes(), |
| + base::Bind(&ImageCaptureResource::OnPluginMsgGetPreviewSizesReply, |
| + base::Unretained(this), capabilities, callback)); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +void ImageCaptureResource::OnPluginMsgOpenReply( |
| + const ResourceMessageReplyParams& params) { |
| + if (open_state_ == BEFORE_OPEN && params.result() == PP_OK) |
| + open_state_ = OPENED; |
| + |
| + // The callback may have been aborted by Close(). |
| + if (TrackedCallback::IsPending(open_callback_)) |
| + open_callback_->Run(params.result()); |
| +} |
| + |
| +void ImageCaptureResource::OnPluginMsgGetPreviewSizesReply( |
| + PP_Resource* output_capabilities, |
| + scoped_refptr<TrackedCallback> callback, |
| + const ResourceMessageReplyParams& params, |
| + const std::vector<PP_Size>& preview_sizes) { |
| + int32_t result = params.result(); |
| + if (result == PP_OK) { |
| + camera_capabilities_ = |
| + new CameraCapabilitiesResource(pp_instance(), preview_sizes); |
| + *output_capabilities = camera_capabilities_->GetReference(); |
| + } |
| + callback->Run(result); |
| +} |
| + |
| +} // namespace proxy |
| +} // namespace ppapi |