Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "ppapi/proxy/image_capture_resource.h" | |
| 6 | |
| 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" | |
| 11 | |
| 12 namespace ppapi { | |
| 13 namespace proxy { | |
| 14 | |
| 15 ImageCaptureResource::ImageCaptureResource(Connection connection, | |
| 16 PP_Instance instance) | |
| 17 : PluginResource(connection, instance), | |
| 18 open_state_(BEFORE_OPEN) { | |
| 19 SendCreate(RENDERER, PpapiHostMsg_ImageCapture_Create()); | |
| 20 } | |
| 21 | |
| 22 ImageCaptureResource::~ImageCaptureResource() { | |
| 23 } | |
| 24 | |
| 25 int32_t ImageCaptureResource::Open( | |
| 26 PP_Var device_id, | |
| 27 const scoped_refptr<TrackedCallback>& callback) { | |
| 28 if (open_state_ != BEFORE_OPEN) | |
| 29 return PP_ERROR_FAILED; | |
| 30 | |
| 31 if (TrackedCallback::IsPending(open_callback_)) | |
| 32 return PP_ERROR_INPROGRESS; | |
| 33 | |
| 34 scoped_refptr<StringVar> source_string_var; | |
| 35 source_string_var = StringVar::FromPPVar(device_id); | |
|
dcheng
2015/02/12 21:47:20
Nit: combine declaration and initialization
Justin Chuang
2015/02/16 19:15:24
Done. Thx
| |
| 36 if (!source_string_var || source_string_var->value().empty()) | |
| 37 return PP_ERROR_BADARGUMENT; | |
| 38 | |
| 39 open_callback_ = callback; | |
| 40 | |
| 41 Call<PpapiPluginMsg_ImageCapture_OpenReply>( | |
| 42 RENDERER, PpapiHostMsg_ImageCapture_Open(source_string_var->value()), | |
| 43 base::Bind(&ImageCaptureResource::OnPluginMsgOpenReply, | |
| 44 base::Unretained(this))); | |
|
dcheng
2015/02/12 21:47:20
Why is it OK to use base::Unretained() here? A rou
Justin Chuang
2015/02/16 19:15:24
Actually I see most code use Unretained(this), vid
| |
| 45 return PP_OK_COMPLETIONPENDING; | |
| 46 } | |
| 47 | |
| 48 void ImageCaptureResource::Close() { | |
| 49 if (open_state_ == CLOSED) | |
| 50 return; | |
| 51 | |
| 52 if (TrackedCallback::IsPending(open_callback_)) { | |
| 53 open_callback_->PostAbort(); | |
| 54 open_callback_ = NULL; | |
|
dcheng
2015/02/12 21:47:20
Use nullptr instead of NULL.
Justin Chuang
2015/02/16 19:15:24
Done.
| |
| 55 } | |
| 56 | |
| 57 if (TrackedCallback::IsPending(get_capabilities_callback_)) { | |
|
dcheng
2015/02/12 21:47:20
This doesn't seem scalable. Are we going to need t
Justin Chuang
2015/02/16 19:15:24
Hmm... There are only two. For now, I prefer not t
| |
| 58 get_capabilities_callback_->PostAbort(); | |
| 59 get_capabilities_callback_ = NULL; | |
| 60 } | |
| 61 | |
| 62 Post(RENDERER, PpapiHostMsg_ImageCapture_Close()); | |
| 63 | |
| 64 open_state_ = CLOSED; | |
| 65 } | |
| 66 | |
| 67 int32_t ImageCaptureResource::GetCameraCapabilities( | |
| 68 PP_Resource* capabilities, | |
| 69 const scoped_refptr<TrackedCallback>& callback) { | |
| 70 if (!is_opened()) | |
| 71 return PP_ERROR_FAILED; | |
| 72 | |
| 73 if (TrackedCallback::IsPending(get_capabilities_callback_)) | |
| 74 return PP_ERROR_INPROGRESS; | |
| 75 | |
| 76 if (camera_capabilities_.get()) { | |
| 77 *capabilities = camera_capabilities_->GetReference(); | |
| 78 return PP_OK; | |
| 79 } | |
| 80 | |
| 81 get_capabilities_callback_ = callback; | |
| 82 Call<PpapiPluginMsg_ImageCapture_GetSupportedPreviewSizesReply>( | |
| 83 RENDERER, PpapiHostMsg_ImageCapture_GetSupportedPreviewSizes(), | |
| 84 base::Bind(&ImageCaptureResource::OnPluginMsgGetPreviewSizesReply, | |
| 85 base::Unretained(this), capabilities)); | |
|
dcheng
2015/02/12 21:47:20
Ditto--why is base::Unretained() safe here?
Justin Chuang
2015/02/16 19:15:24
Ditto
| |
| 86 return PP_OK_COMPLETIONPENDING; | |
| 87 } | |
| 88 | |
| 89 void ImageCaptureResource::OnPluginMsgOpenReply( | |
| 90 const ResourceMessageReplyParams& params) { | |
| 91 if (open_state_ == BEFORE_OPEN && params.result() == PP_OK) | |
| 92 open_state_ = OPENED; | |
| 93 | |
| 94 // The callback may have been aborted by Close(). | |
| 95 if (TrackedCallback::IsPending(open_callback_)) | |
|
dcheng
2015/02/12 21:47:20
Shouldn't this happen before we update open_state_
Justin Chuang
2015/02/16 19:15:24
No, I think the state should be updated before inv
dcheng
2015/02/16 19:24:22
Let's say I call Open(), and then I call Close() b
Justin Chuang
2015/02/16 19:54:22
It will be CLOSED
1. Close() set it to CLOSED and
dcheng
2015/02/16 20:00:14
As a general pattern, and for consistency, I think
| |
| 96 open_callback_->Run(params.result()); | |
| 97 } | |
| 98 | |
| 99 void ImageCaptureResource::OnPluginMsgGetPreviewSizesReply( | |
| 100 PP_Resource* capabilities_output, | |
| 101 const ResourceMessageReplyParams& params, | |
| 102 const std::vector<PP_Size>& preview_sizes) { | |
| 103 if (!TrackedCallback::IsPending(get_capabilities_callback_)) | |
| 104 return; | |
| 105 | |
| 106 // Return camera capabilities. | |
| 107 int32_t result = params.result(); | |
| 108 scoped_refptr<TrackedCallback> callback; | |
| 109 callback.swap(get_capabilities_callback_); | |
| 110 if (result == PP_OK) { | |
| 111 camera_capabilities_ = | |
| 112 new CameraCapabilitiesResource(pp_instance(), preview_sizes); | |
| 113 *capabilities_output = camera_capabilities_->GetReference(); | |
| 114 } | |
| 115 callback->Run(result == PP_OK ? PP_OK : PP_ERROR_FAILED); | |
| 116 } | |
| 117 | |
| 118 } // namespace proxy | |
| 119 } // namespace ppapi | |
| OLD | NEW |