Skip to content

Commit

Permalink
[camera_windows] Use CameraAccessDenied error code for camera permi…
Browse files Browse the repository at this point in the history
…ssion errors (#6154)
  • Loading branch information
loic-sharma authored Aug 1, 2022
1 parent a6d42f1 commit 0d6d03a
Show file tree
Hide file tree
Showing 16 changed files with 871 additions and 203 deletions.
6 changes: 6 additions & 0 deletions packages/camera/camera_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.2.0

**BREAKING CHANGES**:
* `CameraException.code` now has value `"CameraAccessDenied"` if camera access permission was denied.
* `CameraException.code` now has value `"camera_error"` if error occurs during capture.

## 0.1.0+5

* Fixes bugs in in error handling.
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_windows/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_windows
description: A Flutter plugin for getting information about and controlling the camera on Windows.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_windows
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.1.0+5
version: 0.2.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
71 changes: 53 additions & 18 deletions packages/camera/camera_windows/windows/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ constexpr char kVideoRecordedEvent[] = "video_recorded";
constexpr char kCameraClosingEvent[] = "camera_closing";
constexpr char kErrorEvent[] = "error";

// Camera error codes
constexpr char kCameraAccessDenied[] = "CameraAccessDenied";
constexpr char kCameraError[] = "camera_error";
constexpr char kPluginDisposed[] = "plugin_disposed";

std::string GetErrorCode(CameraResult result) {
assert(result != CameraResult::kSuccess);

switch (result) {
case CameraResult::kAccessDenied:
return kCameraAccessDenied;

case CameraResult::kSuccess:
case CameraResult::kError:
default:
return kCameraError;
}
}

CameraImpl::CameraImpl(const std::string& device_id)
: device_id_(device_id), Camera(device_id) {}

Expand All @@ -24,7 +43,7 @@ CameraImpl::~CameraImpl() {
OnCameraClosing();

capture_controller_ = nullptr;
SendErrorForPendingResults("plugin_disposed",
SendErrorForPendingResults(kPluginDisposed,
"Plugin disposed before request was handled");
}

Expand Down Expand Up @@ -121,11 +140,13 @@ void CameraImpl::OnCreateCaptureEngineSucceeded(int64_t texture_id) {
}
}

void CameraImpl::OnCreateCaptureEngineFailed(const std::string& error) {
void CameraImpl::OnCreateCaptureEngineFailed(CameraResult result,
const std::string& error) {
auto pending_result =
GetPendingResultByType(PendingResultType::kCreateCamera);
if (pending_result) {
pending_result->Error("camera_error", error);
std::string error_code = GetErrorCode(result);
pending_result->Error(error_code, error);
}
}

Expand All @@ -141,10 +162,12 @@ void CameraImpl::OnStartPreviewSucceeded(int32_t width, int32_t height) {
}
};

void CameraImpl::OnStartPreviewFailed(const std::string& error) {
void CameraImpl::OnStartPreviewFailed(CameraResult result,
const std::string& error) {
auto pending_result = GetPendingResultByType(PendingResultType::kInitialize);
if (pending_result) {
pending_result->Error("camera_error", error);
std::string error_code = GetErrorCode(result);
pending_result->Error(error_code, error);
}
};

Expand All @@ -156,11 +179,13 @@ void CameraImpl::OnResumePreviewSucceeded() {
}
}

void CameraImpl::OnResumePreviewFailed(const std::string& error) {
void CameraImpl::OnResumePreviewFailed(CameraResult result,
const std::string& error) {
auto pending_result =
GetPendingResultByType(PendingResultType::kResumePreview);
if (pending_result) {
pending_result->Error("camera_error", error);
std::string error_code = GetErrorCode(result);
pending_result->Error(error_code, error);
}
}

Expand All @@ -172,11 +197,13 @@ void CameraImpl::OnPausePreviewSucceeded() {
}
}

void CameraImpl::OnPausePreviewFailed(const std::string& error) {
void CameraImpl::OnPausePreviewFailed(CameraResult result,
const std::string& error) {
auto pending_result =
GetPendingResultByType(PendingResultType::kPausePreview);
if (pending_result) {
pending_result->Error("camera_error", error);
std::string error_code = GetErrorCode(result);
pending_result->Error(error_code, error);
}
}

Expand All @@ -187,10 +214,12 @@ void CameraImpl::OnStartRecordSucceeded() {
}
};

void CameraImpl::OnStartRecordFailed(const std::string& error) {
void CameraImpl::OnStartRecordFailed(CameraResult result,
const std::string& error) {
auto pending_result = GetPendingResultByType(PendingResultType::kStartRecord);
if (pending_result) {
pending_result->Error("camera_error", error);
std::string error_code = GetErrorCode(result);
pending_result->Error(error_code, error);
}
};

Expand All @@ -201,10 +230,12 @@ void CameraImpl::OnStopRecordSucceeded(const std::string& file_path) {
}
};

void CameraImpl::OnStopRecordFailed(const std::string& error) {
void CameraImpl::OnStopRecordFailed(CameraResult result,
const std::string& error) {
auto pending_result = GetPendingResultByType(PendingResultType::kStopRecord);
if (pending_result) {
pending_result->Error("camera_error", error);
std::string error_code = GetErrorCode(result);
pending_result->Error(error_code, error);
}
};

Expand All @@ -215,11 +246,13 @@ void CameraImpl::OnTakePictureSucceeded(const std::string& file_path) {
}
};

void CameraImpl::OnTakePictureFailed(const std::string& error) {
void CameraImpl::OnTakePictureFailed(CameraResult result,
const std::string& error) {
auto pending_take_picture_result =
GetPendingResultByType(PendingResultType::kTakePicture);
if (pending_take_picture_result) {
pending_take_picture_result->Error("camera_error", error);
std::string error_code = GetErrorCode(result);
pending_take_picture_result->Error(error_code, error);
}
};

Expand All @@ -238,9 +271,10 @@ void CameraImpl::OnVideoRecordSucceeded(const std::string& file_path,
}
}

void CameraImpl::OnVideoRecordFailed(const std::string& error){};
void CameraImpl::OnVideoRecordFailed(CameraResult result,
const std::string& error){};

void CameraImpl::OnCaptureError(const std::string& error) {
void CameraImpl::OnCaptureError(CameraResult result, const std::string& error) {
if (messenger_ && camera_id_ >= 0) {
auto channel = GetMethodChannel();

Expand All @@ -250,7 +284,8 @@ void CameraImpl::OnCaptureError(const std::string& error) {
channel->InvokeMethod(kErrorEvent, std::move(message_data));
}

SendErrorForPendingResults("capture_error", error);
std::string error_code = GetErrorCode(result);
SendErrorForPendingResults(error_code, error);
}

void CameraImpl::OnCameraClosing() {
Expand Down
26 changes: 17 additions & 9 deletions packages/camera/camera_windows/windows/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,31 @@ class CameraImpl : public Camera {

// CaptureControllerListener
void OnCreateCaptureEngineSucceeded(int64_t texture_id) override;
void OnCreateCaptureEngineFailed(const std::string& error) override;
void OnCreateCaptureEngineFailed(CameraResult result,
const std::string& error) override;
void OnStartPreviewSucceeded(int32_t width, int32_t height) override;
void OnStartPreviewFailed(const std::string& error) override;
void OnStartPreviewFailed(CameraResult result,
const std::string& error) override;
void OnPausePreviewSucceeded() override;
void OnPausePreviewFailed(const std::string& error) override;
void OnPausePreviewFailed(CameraResult result,
const std::string& error) override;
void OnResumePreviewSucceeded() override;
void OnResumePreviewFailed(const std::string& error) override;
void OnResumePreviewFailed(CameraResult result,
const std::string& error) override;
void OnStartRecordSucceeded() override;
void OnStartRecordFailed(const std::string& error) override;
void OnStartRecordFailed(CameraResult result,
const std::string& error) override;
void OnStopRecordSucceeded(const std::string& file_path) override;
void OnStopRecordFailed(const std::string& error) override;
void OnStopRecordFailed(CameraResult result,
const std::string& error) override;
void OnTakePictureSucceeded(const std::string& file_path) override;
void OnTakePictureFailed(const std::string& error) override;
void OnTakePictureFailed(CameraResult result,
const std::string& error) override;
void OnVideoRecordSucceeded(const std::string& file_path,
int64_t video_duration) override;
void OnVideoRecordFailed(const std::string& error) override;
void OnCaptureError(const std::string& error) override;
void OnVideoRecordFailed(CameraResult result,
const std::string& error) override;
void OnCaptureError(CameraResult result, const std::string& error) override;

// Camera
bool HasDeviceId(std::string& device_id) const override {
Expand Down
Loading

0 comments on commit 0d6d03a

Please sign in to comment.