Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 0d6d03a

Browse files
authored
[camera_windows] Use CameraAccessDenied error code for camera permission errors (#6154)
1 parent a6d42f1 commit 0d6d03a

16 files changed

+871
-203
lines changed

packages/camera/camera_windows/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.2.0
2+
3+
**BREAKING CHANGES**:
4+
* `CameraException.code` now has value `"CameraAccessDenied"` if camera access permission was denied.
5+
* `CameraException.code` now has value `"camera_error"` if error occurs during capture.
6+
17
## 0.1.0+5
28

39
* Fixes bugs in in error handling.

packages/camera/camera_windows/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_windows
22
description: A Flutter plugin for getting information about and controlling the camera on Windows.
33
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_windows
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.1.0+5
5+
version: 0.2.0
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"

packages/camera/camera_windows/windows/camera.cpp

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ constexpr char kVideoRecordedEvent[] = "video_recorded";
1616
constexpr char kCameraClosingEvent[] = "camera_closing";
1717
constexpr char kErrorEvent[] = "error";
1818

19+
// Camera error codes
20+
constexpr char kCameraAccessDenied[] = "CameraAccessDenied";
21+
constexpr char kCameraError[] = "camera_error";
22+
constexpr char kPluginDisposed[] = "plugin_disposed";
23+
24+
std::string GetErrorCode(CameraResult result) {
25+
assert(result != CameraResult::kSuccess);
26+
27+
switch (result) {
28+
case CameraResult::kAccessDenied:
29+
return kCameraAccessDenied;
30+
31+
case CameraResult::kSuccess:
32+
case CameraResult::kError:
33+
default:
34+
return kCameraError;
35+
}
36+
}
37+
1938
CameraImpl::CameraImpl(const std::string& device_id)
2039
: device_id_(device_id), Camera(device_id) {}
2140

@@ -24,7 +43,7 @@ CameraImpl::~CameraImpl() {
2443
OnCameraClosing();
2544

2645
capture_controller_ = nullptr;
27-
SendErrorForPendingResults("plugin_disposed",
46+
SendErrorForPendingResults(kPluginDisposed,
2847
"Plugin disposed before request was handled");
2948
}
3049

@@ -121,11 +140,13 @@ void CameraImpl::OnCreateCaptureEngineSucceeded(int64_t texture_id) {
121140
}
122141
}
123142

124-
void CameraImpl::OnCreateCaptureEngineFailed(const std::string& error) {
143+
void CameraImpl::OnCreateCaptureEngineFailed(CameraResult result,
144+
const std::string& error) {
125145
auto pending_result =
126146
GetPendingResultByType(PendingResultType::kCreateCamera);
127147
if (pending_result) {
128-
pending_result->Error("camera_error", error);
148+
std::string error_code = GetErrorCode(result);
149+
pending_result->Error(error_code, error);
129150
}
130151
}
131152

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

144-
void CameraImpl::OnStartPreviewFailed(const std::string& error) {
165+
void CameraImpl::OnStartPreviewFailed(CameraResult result,
166+
const std::string& error) {
145167
auto pending_result = GetPendingResultByType(PendingResultType::kInitialize);
146168
if (pending_result) {
147-
pending_result->Error("camera_error", error);
169+
std::string error_code = GetErrorCode(result);
170+
pending_result->Error(error_code, error);
148171
}
149172
};
150173

@@ -156,11 +179,13 @@ void CameraImpl::OnResumePreviewSucceeded() {
156179
}
157180
}
158181

159-
void CameraImpl::OnResumePreviewFailed(const std::string& error) {
182+
void CameraImpl::OnResumePreviewFailed(CameraResult result,
183+
const std::string& error) {
160184
auto pending_result =
161185
GetPendingResultByType(PendingResultType::kResumePreview);
162186
if (pending_result) {
163-
pending_result->Error("camera_error", error);
187+
std::string error_code = GetErrorCode(result);
188+
pending_result->Error(error_code, error);
164189
}
165190
}
166191

@@ -172,11 +197,13 @@ void CameraImpl::OnPausePreviewSucceeded() {
172197
}
173198
}
174199

175-
void CameraImpl::OnPausePreviewFailed(const std::string& error) {
200+
void CameraImpl::OnPausePreviewFailed(CameraResult result,
201+
const std::string& error) {
176202
auto pending_result =
177203
GetPendingResultByType(PendingResultType::kPausePreview);
178204
if (pending_result) {
179-
pending_result->Error("camera_error", error);
205+
std::string error_code = GetErrorCode(result);
206+
pending_result->Error(error_code, error);
180207
}
181208
}
182209

@@ -187,10 +214,12 @@ void CameraImpl::OnStartRecordSucceeded() {
187214
}
188215
};
189216

190-
void CameraImpl::OnStartRecordFailed(const std::string& error) {
217+
void CameraImpl::OnStartRecordFailed(CameraResult result,
218+
const std::string& error) {
191219
auto pending_result = GetPendingResultByType(PendingResultType::kStartRecord);
192220
if (pending_result) {
193-
pending_result->Error("camera_error", error);
221+
std::string error_code = GetErrorCode(result);
222+
pending_result->Error(error_code, error);
194223
}
195224
};
196225

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

204-
void CameraImpl::OnStopRecordFailed(const std::string& error) {
233+
void CameraImpl::OnStopRecordFailed(CameraResult result,
234+
const std::string& error) {
205235
auto pending_result = GetPendingResultByType(PendingResultType::kStopRecord);
206236
if (pending_result) {
207-
pending_result->Error("camera_error", error);
237+
std::string error_code = GetErrorCode(result);
238+
pending_result->Error(error_code, error);
208239
}
209240
};
210241

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

218-
void CameraImpl::OnTakePictureFailed(const std::string& error) {
249+
void CameraImpl::OnTakePictureFailed(CameraResult result,
250+
const std::string& error) {
219251
auto pending_take_picture_result =
220252
GetPendingResultByType(PendingResultType::kTakePicture);
221253
if (pending_take_picture_result) {
222-
pending_take_picture_result->Error("camera_error", error);
254+
std::string error_code = GetErrorCode(result);
255+
pending_take_picture_result->Error(error_code, error);
223256
}
224257
};
225258

@@ -238,9 +271,10 @@ void CameraImpl::OnVideoRecordSucceeded(const std::string& file_path,
238271
}
239272
}
240273

241-
void CameraImpl::OnVideoRecordFailed(const std::string& error){};
274+
void CameraImpl::OnVideoRecordFailed(CameraResult result,
275+
const std::string& error){};
242276

243-
void CameraImpl::OnCaptureError(const std::string& error) {
277+
void CameraImpl::OnCaptureError(CameraResult result, const std::string& error) {
244278
if (messenger_ && camera_id_ >= 0) {
245279
auto channel = GetMethodChannel();
246280

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

253-
SendErrorForPendingResults("capture_error", error);
287+
std::string error_code = GetErrorCode(result);
288+
SendErrorForPendingResults(error_code, error);
254289
}
255290

256291
void CameraImpl::OnCameraClosing() {

packages/camera/camera_windows/windows/camera.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,31 @@ class CameraImpl : public Camera {
8787

8888
// CaptureControllerListener
8989
void OnCreateCaptureEngineSucceeded(int64_t texture_id) override;
90-
void OnCreateCaptureEngineFailed(const std::string& error) override;
90+
void OnCreateCaptureEngineFailed(CameraResult result,
91+
const std::string& error) override;
9192
void OnStartPreviewSucceeded(int32_t width, int32_t height) override;
92-
void OnStartPreviewFailed(const std::string& error) override;
93+
void OnStartPreviewFailed(CameraResult result,
94+
const std::string& error) override;
9395
void OnPausePreviewSucceeded() override;
94-
void OnPausePreviewFailed(const std::string& error) override;
96+
void OnPausePreviewFailed(CameraResult result,
97+
const std::string& error) override;
9598
void OnResumePreviewSucceeded() override;
96-
void OnResumePreviewFailed(const std::string& error) override;
99+
void OnResumePreviewFailed(CameraResult result,
100+
const std::string& error) override;
97101
void OnStartRecordSucceeded() override;
98-
void OnStartRecordFailed(const std::string& error) override;
102+
void OnStartRecordFailed(CameraResult result,
103+
const std::string& error) override;
99104
void OnStopRecordSucceeded(const std::string& file_path) override;
100-
void OnStopRecordFailed(const std::string& error) override;
105+
void OnStopRecordFailed(CameraResult result,
106+
const std::string& error) override;
101107
void OnTakePictureSucceeded(const std::string& file_path) override;
102-
void OnTakePictureFailed(const std::string& error) override;
108+
void OnTakePictureFailed(CameraResult result,
109+
const std::string& error) override;
103110
void OnVideoRecordSucceeded(const std::string& file_path,
104111
int64_t video_duration) override;
105-
void OnVideoRecordFailed(const std::string& error) override;
106-
void OnCaptureError(const std::string& error) override;
112+
void OnVideoRecordFailed(CameraResult result,
113+
const std::string& error) override;
114+
void OnCaptureError(CameraResult result, const std::string& error) override;
107115

108116
// Camera
109117
bool HasDeviceId(std::string& device_id) const override {

0 commit comments

Comments
 (0)