Chromium Code Reviews| Index: extensions/browser/api/webcam_private/webcam_private_api_chromeos.cc |
| diff --git a/extensions/browser/api/webcam_private/webcam_private_api_chromeos.cc b/extensions/browser/api/webcam_private/webcam_private_api_chromeos.cc |
| index 5f024b07929c03fdc5e1c1bfa9cbaae71902521c..93d5503413ff2e7256f79887fd45f37b80771fb1 100644 |
| --- a/extensions/browser/api/webcam_private/webcam_private_api_chromeos.cc |
| +++ b/extensions/browser/api/webcam_private/webcam_private_api_chromeos.cc |
| @@ -21,8 +21,13 @@ class BrowserContext; |
| } // namespace content |
| namespace { |
| + |
| const char kPathInUse[] = "Path in use"; |
| const char kUnknownWebcam[] = "Unknown webcam id"; |
| +const char kGetWebcamPTZError[] = "Can't get web camera pan/tilt/zoom."; |
| +const char kSetWebcamPTZError[] = "Can't set web camera pan/tilt/zoom."; |
| +const char kResetWebcamError[] = "Can't reset web camera."; |
| + |
| } // namespace |
| namespace extensions { |
| @@ -224,7 +229,7 @@ WebcamPrivateSetFunction::WebcamPrivateSetFunction() { |
| WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { |
| } |
| -bool WebcamPrivateSetFunction::RunSync() { |
| +bool WebcamPrivateSetFunction::RunAsync() { |
| scoped_ptr<webcam_private::Set::Params> params( |
| webcam_private::Set::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| @@ -237,7 +242,9 @@ bool WebcamPrivateSetFunction::RunSync() { |
| } |
| if (params->config.pan) { |
| - webcam->SetPan(*(params->config.pan)); |
| + webcam->SetPan( |
| + *(params->config.pan), |
| + base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); |
| } |
| if (params->config.pan_direction) { |
| @@ -256,11 +263,15 @@ bool WebcamPrivateSetFunction::RunSync() { |
| direction = Webcam::PAN_LEFT; |
| break; |
| } |
| - webcam->SetPanDirection(direction); |
| + webcam->SetPanDirection( |
| + direction, |
| + base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); |
| } |
| if (params->config.tilt) { |
| - webcam->SetTilt(*(params->config.tilt)); |
| + webcam->SetTilt( |
| + *(params->config.tilt), |
| + base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); |
| } |
| if (params->config.tilt_direction) { |
| @@ -279,24 +290,32 @@ bool WebcamPrivateSetFunction::RunSync() { |
| direction = Webcam::TILT_DOWN; |
| break; |
| } |
| - webcam->SetTiltDirection(direction); |
| + webcam->SetTiltDirection( |
| + direction, |
| + base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); |
| } |
| if (params->config.zoom) { |
| - webcam->SetZoom(*(params->config.zoom)); |
| + webcam->SetZoom( |
| + *(params->config.zoom), |
| + base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); |
| } |
| - |
| return true; |
| } |
| -WebcamPrivateGetFunction::WebcamPrivateGetFunction() { |
| +void WebcamPrivateSetFunction::OnSetWebcamParameters(bool success) { |
| + if (!success) |
| + SetError(kSetWebcamPTZError); |
| } |
| +WebcamPrivateGetFunction::WebcamPrivateGetFunction() |
| + : pan_(0), tilt_(0), zoom_(0), success_(true) {} |
| + |
| WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { |
| } |
| -bool WebcamPrivateGetFunction::RunSync() { |
| +bool WebcamPrivateGetFunction::RunAsync() { |
| scoped_ptr<webcam_private::Get::Params> params( |
| webcam_private::Get::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| @@ -308,23 +327,45 @@ bool WebcamPrivateGetFunction::RunSync() { |
| return false; |
| } |
| - webcam_private::WebcamConfiguration result; |
| - |
| - int pan; |
| - if (webcam->GetPan(&pan)) |
| - result.pan.reset(new double(pan)); |
| + webcam->GetPan(base::Bind(&WebcamPrivateGetFunction::OnGetWebcamParameters, |
| + this, INQUIRY_PAN)); |
| + webcam->GetTilt(base::Bind(&WebcamPrivateGetFunction::OnGetWebcamParameters, |
| + this, INQUIRY_TILT)); |
| + webcam->GetZoom(base::Bind(&WebcamPrivateGetFunction::OnGetWebcamParameters, |
| + this, INQUIRY_ZOOM)); |
| - int tilt; |
| - if (webcam->GetTilt(&tilt)) |
| - result.tilt.reset(new double(tilt)); |
| - |
| - int zoom; |
| - if (webcam->GetZoom(&zoom)) |
| - result.zoom.reset(new double(zoom)); |
| + return true; |
| +} |
| - SetResult(result.ToValue().release()); |
| +void WebcamPrivateGetFunction::OnGetWebcamParameters(InquiryType type, |
| + bool success, |
| + int value) { |
| + if (!success_) |
| + return; |
| + success_ &= success; |
|
Zachary Kuznia
2015/08/26 23:31:13
You shouldn't use bitwise AND for bools.
|
| - return true; |
| + if (!success_) { |
| + SetError(kGetWebcamPTZError); |
| + SendResponse(false); |
| + } else { |
| + switch (type) { |
| + case INQUIRY_PAN: |
| + pan_ = value; |
| + break; |
| + case INQUIRY_TILT: |
| + tilt_ = value; |
| + break; |
| + case INQUIRY_ZOOM: |
| + zoom_ = value; |
| + webcam_private::WebcamConfiguration result; |
|
Zachary Kuznia
2015/08/26 23:31:13
You shouldn't assume that async functions are invo
|
| + result.pan.reset(new double(pan_)); |
| + result.tilt.reset(new double(tilt_)); |
| + result.zoom.reset(new double(zoom_)); |
| + SetResult(result.ToValue().release()); |
| + SendResponse(true); |
| + break; |
| + } |
| + } |
| } |
| WebcamPrivateResetFunction::WebcamPrivateResetFunction() { |
| @@ -333,7 +374,7 @@ WebcamPrivateResetFunction::WebcamPrivateResetFunction() { |
| WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { |
| } |
| -bool WebcamPrivateResetFunction::RunSync() { |
| +bool WebcamPrivateResetFunction::RunAsync() { |
| scoped_ptr<webcam_private::Reset::Params> params( |
| webcam_private::Reset::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params.get()); |
| @@ -345,11 +386,17 @@ bool WebcamPrivateResetFunction::RunSync() { |
| return false; |
| } |
| - webcam->Reset(params->config.pan, params->config.tilt, params->config.zoom); |
| + webcam->Reset(params->config.pan, params->config.tilt, params->config.zoom, |
| + base::Bind(&WebcamPrivateResetFunction::OnResetWebcam, this)); |
| return true; |
| } |
| +void WebcamPrivateResetFunction::OnResetWebcam(bool success) { |
| + if (!success) |
| + SetError(kResetWebcamError); |
| +} |
| + |
| static base::LazyInstance<BrowserContextKeyedAPIFactory<WebcamPrivateAPI>> |
| g_factory = LAZY_INSTANCE_INITIALIZER; |