Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/webcam_private/webcam_private_api.h" | 5 #include "extensions/browser/api/webcam_private/webcam_private_api.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "content/public/browser/browser_context.h" | 8 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/media_device_id.h" | 9 #include "content/public/browser/media_device_id.h" |
| 10 #include "content/public/browser/resource_context.h" | 10 #include "content/public/browser/resource_context.h" |
| 11 #include "extensions/browser/api/webcam_private/v4l2_webcam.h" | 11 #include "extensions/browser/api/webcam_private/v4l2_webcam.h" |
| 12 #include "extensions/browser/api/webcam_private/visca_webcam.h" | 12 #include "extensions/browser/api/webcam_private/visca_webcam.h" |
| 13 #include "extensions/browser/process_manager.h" | 13 #include "extensions/browser/process_manager.h" |
| 14 #include "extensions/browser/process_manager_factory.h" | 14 #include "extensions/browser/process_manager_factory.h" |
| 15 #include "extensions/common/api/webcam_private.h" | 15 #include "extensions/common/api/webcam_private.h" |
| 16 | 16 |
| 17 namespace webcam_private = extensions::api::webcam_private; | 17 namespace webcam_private = extensions::api::webcam_private; |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 class BrowserContext; | 20 class BrowserContext; |
| 21 } // namespace content | 21 } // namespace content |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | |
| 24 const char kPathInUse[] = "Path in use"; | 25 const char kPathInUse[] = "Path in use"; |
| 25 const char kUnknownWebcam[] = "Unknown webcam id"; | 26 const char kUnknownWebcam[] = "Unknown webcam id"; |
| 27 const char kGetWebcamPTZError[] = "Can't get web camera pan/tilt/zoom."; | |
| 28 const char kSetWebcamPTZError[] = "Can't set web camera pan/tilt/zoom."; | |
| 29 const char kResetWebcamError[] = "Can't reset web camera."; | |
| 30 | |
| 26 } // namespace | 31 } // namespace |
| 27 | 32 |
| 28 namespace extensions { | 33 namespace extensions { |
| 29 | 34 |
| 30 // static | 35 // static |
| 31 WebcamPrivateAPI* WebcamPrivateAPI::Get(content::BrowserContext* context) { | 36 WebcamPrivateAPI* WebcamPrivateAPI::Get(content::BrowserContext* context) { |
| 32 return GetFactoryInstance()->Get(context); | 37 return GetFactoryInstance()->Get(context); |
| 33 } | 38 } |
| 34 | 39 |
| 35 WebcamPrivateAPI::WebcamPrivateAPI(content::BrowserContext* context) | 40 WebcamPrivateAPI::WebcamPrivateAPI(content::BrowserContext* context) |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 return WebcamPrivateAPI::Get(browser_context()) | 222 return WebcamPrivateAPI::Get(browser_context()) |
| 218 ->CloseWebcam(extension_id(), params->webcam_id); | 223 ->CloseWebcam(extension_id(), params->webcam_id); |
| 219 } | 224 } |
| 220 | 225 |
| 221 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { | 226 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { |
| 222 } | 227 } |
| 223 | 228 |
| 224 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { | 229 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { |
| 225 } | 230 } |
| 226 | 231 |
| 227 bool WebcamPrivateSetFunction::RunSync() { | 232 bool WebcamPrivateSetFunction::RunAsync() { |
| 228 scoped_ptr<webcam_private::Set::Params> params( | 233 scoped_ptr<webcam_private::Set::Params> params( |
| 229 webcam_private::Set::Params::Create(*args_)); | 234 webcam_private::Set::Params::Create(*args_)); |
| 230 EXTENSION_FUNCTION_VALIDATE(params.get()); | 235 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 231 | 236 |
| 232 Webcam* webcam = WebcamPrivateAPI::Get(browser_context()) | 237 Webcam* webcam = WebcamPrivateAPI::Get(browser_context()) |
| 233 ->GetWebcam(extension_id(), params->webcam_id); | 238 ->GetWebcam(extension_id(), params->webcam_id); |
| 234 if (!webcam) { | 239 if (!webcam) { |
| 235 SetError(kUnknownWebcam); | 240 SetError(kUnknownWebcam); |
| 236 return false; | 241 return false; |
| 237 } | 242 } |
| 238 | 243 |
| 239 if (params->config.pan) { | 244 if (params->config.pan) { |
| 240 webcam->SetPan(*(params->config.pan)); | 245 webcam->SetPan( |
| 246 *(params->config.pan), | |
| 247 base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); | |
| 241 } | 248 } |
| 242 | 249 |
| 243 if (params->config.pan_direction) { | 250 if (params->config.pan_direction) { |
| 244 Webcam::PanDirection direction = Webcam::PAN_STOP; | 251 Webcam::PanDirection direction = Webcam::PAN_STOP; |
| 245 switch (params->config.pan_direction) { | 252 switch (params->config.pan_direction) { |
| 246 case webcam_private::PAN_DIRECTION_NONE: | 253 case webcam_private::PAN_DIRECTION_NONE: |
| 247 case webcam_private::PAN_DIRECTION_STOP: | 254 case webcam_private::PAN_DIRECTION_STOP: |
| 248 direction = Webcam::PAN_STOP; | 255 direction = Webcam::PAN_STOP; |
| 249 break; | 256 break; |
| 250 | 257 |
| 251 case webcam_private::PAN_DIRECTION_RIGHT: | 258 case webcam_private::PAN_DIRECTION_RIGHT: |
| 252 direction = Webcam::PAN_RIGHT; | 259 direction = Webcam::PAN_RIGHT; |
| 253 break; | 260 break; |
| 254 | 261 |
| 255 case webcam_private::PAN_DIRECTION_LEFT: | 262 case webcam_private::PAN_DIRECTION_LEFT: |
| 256 direction = Webcam::PAN_LEFT; | 263 direction = Webcam::PAN_LEFT; |
| 257 break; | 264 break; |
| 258 } | 265 } |
| 259 webcam->SetPanDirection(direction); | 266 webcam->SetPanDirection( |
| 267 direction, | |
| 268 base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); | |
| 260 } | 269 } |
| 261 | 270 |
| 262 if (params->config.tilt) { | 271 if (params->config.tilt) { |
| 263 webcam->SetTilt(*(params->config.tilt)); | 272 webcam->SetTilt( |
| 273 *(params->config.tilt), | |
| 274 base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); | |
| 264 } | 275 } |
| 265 | 276 |
| 266 if (params->config.tilt_direction) { | 277 if (params->config.tilt_direction) { |
| 267 Webcam::TiltDirection direction = Webcam::TILT_STOP; | 278 Webcam::TiltDirection direction = Webcam::TILT_STOP; |
| 268 switch (params->config.tilt_direction) { | 279 switch (params->config.tilt_direction) { |
| 269 case webcam_private::TILT_DIRECTION_NONE: | 280 case webcam_private::TILT_DIRECTION_NONE: |
| 270 case webcam_private::TILT_DIRECTION_STOP: | 281 case webcam_private::TILT_DIRECTION_STOP: |
| 271 direction = Webcam::TILT_STOP; | 282 direction = Webcam::TILT_STOP; |
| 272 break; | 283 break; |
| 273 | 284 |
| 274 case webcam_private::TILT_DIRECTION_UP: | 285 case webcam_private::TILT_DIRECTION_UP: |
| 275 direction = Webcam::TILT_UP; | 286 direction = Webcam::TILT_UP; |
| 276 break; | 287 break; |
| 277 | 288 |
| 278 case webcam_private::TILT_DIRECTION_DOWN: | 289 case webcam_private::TILT_DIRECTION_DOWN: |
| 279 direction = Webcam::TILT_DOWN; | 290 direction = Webcam::TILT_DOWN; |
| 280 break; | 291 break; |
| 281 } | 292 } |
| 282 webcam->SetTiltDirection(direction); | 293 webcam->SetTiltDirection( |
| 294 direction, | |
| 295 base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); | |
| 283 } | 296 } |
| 284 | 297 |
| 285 if (params->config.zoom) { | 298 if (params->config.zoom) { |
| 286 webcam->SetZoom(*(params->config.zoom)); | 299 webcam->SetZoom( |
| 300 *(params->config.zoom), | |
| 301 base::Bind(&WebcamPrivateSetFunction::OnSetWebcamParameters, this)); | |
| 287 } | 302 } |
| 288 | 303 |
| 289 | |
| 290 return true; | 304 return true; |
| 291 } | 305 } |
| 292 | 306 |
| 293 WebcamPrivateGetFunction::WebcamPrivateGetFunction() { | 307 void WebcamPrivateSetFunction::OnSetWebcamParameters(bool success) { |
| 308 if (!success) | |
| 309 SetError(kSetWebcamPTZError); | |
| 294 } | 310 } |
| 295 | 311 |
| 312 WebcamPrivateGetFunction::WebcamPrivateGetFunction() | |
| 313 : pan_(0), tilt_(0), zoom_(0), success_(true) {} | |
| 314 | |
| 296 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { | 315 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { |
| 297 } | 316 } |
| 298 | 317 |
| 299 bool WebcamPrivateGetFunction::RunSync() { | 318 bool WebcamPrivateGetFunction::RunAsync() { |
| 300 scoped_ptr<webcam_private::Get::Params> params( | 319 scoped_ptr<webcam_private::Get::Params> params( |
| 301 webcam_private::Get::Params::Create(*args_)); | 320 webcam_private::Get::Params::Create(*args_)); |
| 302 EXTENSION_FUNCTION_VALIDATE(params.get()); | 321 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 303 | 322 |
| 304 Webcam* webcam = WebcamPrivateAPI::Get(browser_context()) | 323 Webcam* webcam = WebcamPrivateAPI::Get(browser_context()) |
| 305 ->GetWebcam(extension_id(), params->webcam_id); | 324 ->GetWebcam(extension_id(), params->webcam_id); |
| 306 if (!webcam) { | 325 if (!webcam) { |
| 307 SetError(kUnknownWebcam); | 326 SetError(kUnknownWebcam); |
| 308 return false; | 327 return false; |
| 309 } | 328 } |
| 310 | 329 |
| 311 webcam_private::WebcamConfiguration result; | 330 webcam->GetPan(base::Bind(&WebcamPrivateGetFunction::OnGetWebcamParameters, |
| 312 | 331 this, INQUIRY_PAN)); |
| 313 int pan; | 332 webcam->GetTilt(base::Bind(&WebcamPrivateGetFunction::OnGetWebcamParameters, |
| 314 if (webcam->GetPan(&pan)) | 333 this, INQUIRY_TILT)); |
| 315 result.pan.reset(new double(pan)); | 334 webcam->GetZoom(base::Bind(&WebcamPrivateGetFunction::OnGetWebcamParameters, |
| 316 | 335 this, INQUIRY_ZOOM)); |
| 317 int tilt; | |
| 318 if (webcam->GetTilt(&tilt)) | |
| 319 result.tilt.reset(new double(tilt)); | |
| 320 | |
| 321 int zoom; | |
| 322 if (webcam->GetZoom(&zoom)) | |
| 323 result.zoom.reset(new double(zoom)); | |
| 324 | |
| 325 SetResult(result.ToValue().release()); | |
| 326 | 336 |
| 327 return true; | 337 return true; |
| 328 } | 338 } |
| 329 | 339 |
| 340 void WebcamPrivateGetFunction::OnGetWebcamParameters(InquiryType type, | |
| 341 bool success, | |
| 342 int value) { | |
| 343 if (!success_) | |
| 344 return; | |
| 345 success_ &= success; | |
|
Zachary Kuznia
2015/08/26 23:31:13
You shouldn't use bitwise AND for bools.
| |
| 346 | |
| 347 if (!success_) { | |
| 348 SetError(kGetWebcamPTZError); | |
| 349 SendResponse(false); | |
| 350 } else { | |
| 351 switch (type) { | |
| 352 case INQUIRY_PAN: | |
| 353 pan_ = value; | |
| 354 break; | |
| 355 case INQUIRY_TILT: | |
| 356 tilt_ = value; | |
| 357 break; | |
| 358 case INQUIRY_ZOOM: | |
| 359 zoom_ = value; | |
| 360 webcam_private::WebcamConfiguration result; | |
|
Zachary Kuznia
2015/08/26 23:31:13
You shouldn't assume that async functions are invo
| |
| 361 result.pan.reset(new double(pan_)); | |
| 362 result.tilt.reset(new double(tilt_)); | |
| 363 result.zoom.reset(new double(zoom_)); | |
| 364 SetResult(result.ToValue().release()); | |
| 365 SendResponse(true); | |
| 366 break; | |
| 367 } | |
| 368 } | |
| 369 } | |
| 370 | |
| 330 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { | 371 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { |
| 331 } | 372 } |
| 332 | 373 |
| 333 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { | 374 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { |
| 334 } | 375 } |
| 335 | 376 |
| 336 bool WebcamPrivateResetFunction::RunSync() { | 377 bool WebcamPrivateResetFunction::RunAsync() { |
| 337 scoped_ptr<webcam_private::Reset::Params> params( | 378 scoped_ptr<webcam_private::Reset::Params> params( |
| 338 webcam_private::Reset::Params::Create(*args_)); | 379 webcam_private::Reset::Params::Create(*args_)); |
| 339 EXTENSION_FUNCTION_VALIDATE(params.get()); | 380 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 340 | 381 |
| 341 Webcam* webcam = WebcamPrivateAPI::Get(browser_context()) | 382 Webcam* webcam = WebcamPrivateAPI::Get(browser_context()) |
| 342 ->GetWebcam(extension_id(), params->webcam_id); | 383 ->GetWebcam(extension_id(), params->webcam_id); |
| 343 if (!webcam) { | 384 if (!webcam) { |
| 344 SetError(kUnknownWebcam); | 385 SetError(kUnknownWebcam); |
| 345 return false; | 386 return false; |
| 346 } | 387 } |
| 347 | 388 |
| 348 webcam->Reset(params->config.pan, params->config.tilt, params->config.zoom); | 389 webcam->Reset(params->config.pan, params->config.tilt, params->config.zoom, |
| 390 base::Bind(&WebcamPrivateResetFunction::OnResetWebcam, this)); | |
| 349 | 391 |
| 350 return true; | 392 return true; |
| 351 } | 393 } |
| 352 | 394 |
| 395 void WebcamPrivateResetFunction::OnResetWebcam(bool success) { | |
| 396 if (!success) | |
| 397 SetError(kResetWebcamError); | |
| 398 } | |
| 399 | |
| 353 static base::LazyInstance<BrowserContextKeyedAPIFactory<WebcamPrivateAPI>> | 400 static base::LazyInstance<BrowserContextKeyedAPIFactory<WebcamPrivateAPI>> |
| 354 g_factory = LAZY_INSTANCE_INITIALIZER; | 401 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 355 | 402 |
| 356 // static | 403 // static |
| 357 BrowserContextKeyedAPIFactory<WebcamPrivateAPI>* | 404 BrowserContextKeyedAPIFactory<WebcamPrivateAPI>* |
| 358 WebcamPrivateAPI::GetFactoryInstance() { | 405 WebcamPrivateAPI::GetFactoryInstance() { |
| 359 return g_factory.Pointer(); | 406 return g_factory.Pointer(); |
| 360 } | 407 } |
| 361 | 408 |
| 362 template <> | 409 template <> |
| 363 void BrowserContextKeyedAPIFactory<WebcamPrivateAPI> | 410 void BrowserContextKeyedAPIFactory<WebcamPrivateAPI> |
| 364 ::DeclareFactoryDependencies() { | 411 ::DeclareFactoryDependencies() { |
| 365 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 412 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 366 DependsOn(ProcessManagerFactory::GetInstance()); | 413 DependsOn(ProcessManagerFactory::GetInstance()); |
| 367 } | 414 } |
| 368 | 415 |
| 369 } // namespace extensions | 416 } // namespace extensions |
| OLD | NEW |