/extensions/browser/api/webcam_private/webcam_private_api.h

https://github.com/chromium/chromium · C Header · 285 lines · 198 code · 72 blank · 15 comment · 0 complexity · 39de36b4c6a051b88571a59408fc7601 MD5 · raw file

  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
  3. // found in the LICENSE file.
  4. #ifndef EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_WEBCAM_PRIVATE_API_H_
  5. #define EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_WEBCAM_PRIVATE_API_H_
  6. #include <map>
  7. #include <memory>
  8. #include "extensions/browser/api/api_resource_manager.h"
  9. #include "extensions/browser/api/webcam_private/webcam.h"
  10. #include "extensions/browser/browser_context_keyed_api_factory.h"
  11. #include "extensions/browser/extension_function.h"
  12. #include "extensions/browser/process_manager_observer.h"
  13. namespace extensions {
  14. class WebcamPrivateAPI : public BrowserContextKeyedAPI {
  15. public:
  16. static BrowserContextKeyedAPIFactory<WebcamPrivateAPI>* GetFactoryInstance();
  17. // Convenience method to get the WebcamPrivateAPI for a BrowserContext.
  18. static WebcamPrivateAPI* Get(content::BrowserContext* context);
  19. explicit WebcamPrivateAPI(content::BrowserContext* context);
  20. WebcamPrivateAPI(const WebcamPrivateAPI&) = delete;
  21. WebcamPrivateAPI& operator=(const WebcamPrivateAPI&) = delete;
  22. ~WebcamPrivateAPI() override;
  23. Webcam* GetWebcam(const std::string& extension_id,
  24. const std::string& device_id);
  25. bool OpenSerialWebcam(
  26. const std::string& extension_id,
  27. const std::string& device_path,
  28. const base::RepeatingCallback<void(const std::string&, bool)>& callback);
  29. bool CloseWebcam(const std::string& extension_id,
  30. const std::string& device_id);
  31. private:
  32. friend class BrowserContextKeyedAPIFactory<WebcamPrivateAPI>;
  33. void OnOpenSerialWebcam(
  34. const std::string& extension_id,
  35. const std::string& device_path,
  36. scoped_refptr<Webcam> webcam,
  37. const base::RepeatingCallback<void(const std::string&, bool)>& callback,
  38. bool success);
  39. // Note: This function does not work for serial devices. Do not use this
  40. // function for serial devices.
  41. bool GetDeviceId(const std::string& extension_id,
  42. const std::string& webcam_id,
  43. std::string* device_id);
  44. std::string GetWebcamId(const std::string& extension_id,
  45. const std::string& device_id);
  46. WebcamResource* FindWebcamResource(const std::string& extension_id,
  47. const std::string& webcam_id) const;
  48. bool RemoveWebcamResource(const std::string& extension_id,
  49. const std::string& webcam_id);
  50. // BrowserContextKeyedAPI:
  51. static const char* service_name() {
  52. return "WebcamPrivateAPI";
  53. }
  54. static const bool kServiceIsNULLWhileTesting = true;
  55. static const bool kServiceRedirectedInIncognito = true;
  56. content::BrowserContext* const browser_context_;
  57. std::unique_ptr<ApiResourceManager<WebcamResource>> webcam_resource_manager_;
  58. base::WeakPtrFactory<WebcamPrivateAPI> weak_ptr_factory_{this};
  59. };
  60. template <>
  61. void BrowserContextKeyedAPIFactory<WebcamPrivateAPI>
  62. ::DeclareFactoryDependencies();
  63. class WebcamPrivateOpenSerialWebcamFunction : public ExtensionFunction {
  64. public:
  65. WebcamPrivateOpenSerialWebcamFunction();
  66. WebcamPrivateOpenSerialWebcamFunction(
  67. const WebcamPrivateOpenSerialWebcamFunction&) = delete;
  68. WebcamPrivateOpenSerialWebcamFunction& operator=(
  69. const WebcamPrivateOpenSerialWebcamFunction&) = delete;
  70. DECLARE_EXTENSION_FUNCTION("webcamPrivate.openSerialWebcam",
  71. WEBCAMPRIVATE_OPENSERIALWEBCAM)
  72. protected:
  73. ~WebcamPrivateOpenSerialWebcamFunction() override;
  74. // ExtensionFunction:
  75. ResponseAction Run() override;
  76. private:
  77. void OnOpenWebcam(const std::string& webcam_id, bool success);
  78. };
  79. class WebcamPrivateCloseWebcamFunction : public ExtensionFunction {
  80. public:
  81. WebcamPrivateCloseWebcamFunction();
  82. WebcamPrivateCloseWebcamFunction(const WebcamPrivateCloseWebcamFunction&) =
  83. delete;
  84. WebcamPrivateCloseWebcamFunction& operator=(
  85. const WebcamPrivateCloseWebcamFunction&) = delete;
  86. DECLARE_EXTENSION_FUNCTION("webcamPrivate.closeWebcam",
  87. WEBCAMPRIVATE_CLOSEWEBCAM)
  88. protected:
  89. ~WebcamPrivateCloseWebcamFunction() override;
  90. // ExtensionFunction:
  91. ResponseAction Run() override;
  92. };
  93. class WebcamPrivateSetFunction : public ExtensionFunction {
  94. public:
  95. WebcamPrivateSetFunction();
  96. WebcamPrivateSetFunction(const WebcamPrivateSetFunction&) = delete;
  97. WebcamPrivateSetFunction& operator=(const WebcamPrivateSetFunction&) = delete;
  98. DECLARE_EXTENSION_FUNCTION("webcamPrivate.set", WEBCAMPRIVATE_SET)
  99. protected:
  100. ~WebcamPrivateSetFunction() override;
  101. // ExtensionFunction:
  102. ResponseAction Run() override;
  103. private:
  104. void OnSetWebcamParameters(bool success);
  105. int pending_num_set_webcam_param_requests_ = 0;
  106. bool failed_ = false;
  107. };
  108. class WebcamPrivateGetFunction : public ExtensionFunction {
  109. public:
  110. WebcamPrivateGetFunction();
  111. WebcamPrivateGetFunction(const WebcamPrivateGetFunction&) = delete;
  112. WebcamPrivateGetFunction& operator=(const WebcamPrivateGetFunction&) = delete;
  113. DECLARE_EXTENSION_FUNCTION("webcamPrivate.get", WEBCAMPRIVATE_GET)
  114. protected:
  115. ~WebcamPrivateGetFunction() override;
  116. // ExtensionFunction:
  117. ResponseAction Run() override;
  118. private:
  119. enum InquiryType {
  120. INQUIRY_PAN,
  121. INQUIRY_TILT,
  122. INQUIRY_ZOOM,
  123. INQUIRY_FOCUS,
  124. };
  125. enum AutofocusState {
  126. AUTOFOCUSSTATE_ON,
  127. AUTOFOCUSSTATE_OFF,
  128. };
  129. void OnGetWebcamParameters(InquiryType type,
  130. bool success,
  131. int value,
  132. int min_value,
  133. int max_value);
  134. int min_pan_;
  135. int max_pan_;
  136. int pan_;
  137. int min_tilt_;
  138. int max_tilt_;
  139. int tilt_;
  140. int min_zoom_;
  141. int max_zoom_;
  142. int zoom_;
  143. int min_focus_;
  144. int max_focus_;
  145. int focus_;
  146. bool got_pan_;
  147. bool got_tilt_;
  148. bool got_zoom_;
  149. bool got_focus_;
  150. bool success_;
  151. };
  152. class WebcamPrivateResetFunction : public ExtensionFunction {
  153. public:
  154. WebcamPrivateResetFunction();
  155. WebcamPrivateResetFunction(const WebcamPrivateResetFunction&) = delete;
  156. WebcamPrivateResetFunction& operator=(const WebcamPrivateResetFunction&) =
  157. delete;
  158. DECLARE_EXTENSION_FUNCTION("webcamPrivate.reset", WEBCAMPRIVATE_RESET)
  159. protected:
  160. ~WebcamPrivateResetFunction() override;
  161. // ExtensionFunction:
  162. ResponseAction Run() override;
  163. private:
  164. void OnResetWebcam(bool success);
  165. };
  166. class WebcamPrivateSetHomeFunction : public ExtensionFunction {
  167. public:
  168. WebcamPrivateSetHomeFunction();
  169. WebcamPrivateSetHomeFunction(const WebcamPrivateSetHomeFunction&) = delete;
  170. WebcamPrivateSetHomeFunction& operator=(const WebcamPrivateSetHomeFunction&) =
  171. delete;
  172. DECLARE_EXTENSION_FUNCTION("webcamPrivate.setHome", WEBCAMPRIVATE_SET_HOME)
  173. protected:
  174. ~WebcamPrivateSetHomeFunction() override;
  175. // ExtensionFunction:
  176. ResponseAction Run() override;
  177. private:
  178. void OnSetHomeWebcam(bool success);
  179. };
  180. class WebcamPrivateRestoreCameraPresetFunction : public ExtensionFunction {
  181. public:
  182. WebcamPrivateRestoreCameraPresetFunction();
  183. WebcamPrivateRestoreCameraPresetFunction(
  184. const WebcamPrivateRestoreCameraPresetFunction&) = delete;
  185. WebcamPrivateRestoreCameraPresetFunction& operator=(
  186. const WebcamPrivateRestoreCameraPresetFunction&) = delete;
  187. DECLARE_EXTENSION_FUNCTION("webcamPrivate.restoreCameraPreset",
  188. WEBCAMPRIVATE_RESTORE_CAMERA_PRESET)
  189. protected:
  190. ~WebcamPrivateRestoreCameraPresetFunction() override;
  191. // ExtensionFunction:
  192. ResponseAction Run() override;
  193. private:
  194. void OnRestoreCameraPresetWebcam(bool success);
  195. };
  196. class WebcamPrivateSetCameraPresetFunction : public ExtensionFunction {
  197. public:
  198. WebcamPrivateSetCameraPresetFunction();
  199. WebcamPrivateSetCameraPresetFunction(
  200. const WebcamPrivateSetCameraPresetFunction&) = delete;
  201. WebcamPrivateSetCameraPresetFunction& operator=(
  202. const WebcamPrivateSetCameraPresetFunction&) = delete;
  203. DECLARE_EXTENSION_FUNCTION("webcamPrivate.setCameraPreset",
  204. WEBCAMPRIVATE_SET_CAMERA_PRESET)
  205. protected:
  206. ~WebcamPrivateSetCameraPresetFunction() override;
  207. // ExtensionFunction:
  208. ResponseAction Run() override;
  209. private:
  210. void OnSetCameraPresetWebcam(bool success);
  211. };
  212. } // namespace extensions
  213. #endif // EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_WEBCAM_PRIVATE_API_H_