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.
5 #ifndef EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_WEBCAM_PRIVATE_API_H_
6 #define EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_WEBCAM_PRIVATE_API_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/scoped_observer.h"
12 #include "extensions/browser/api/api_resource_manager.h"
13 #include "extensions/browser/api/webcam_private/webcam.h"
14 #include "extensions/browser/browser_context_keyed_api_factory.h"
15 #include "extensions/browser/extension_function.h"
16 #include "extensions/browser/process_manager_observer.h"
20 namespace extensions
{
24 class WebcamPrivateAPI
: public BrowserContextKeyedAPI
{
26 static BrowserContextKeyedAPIFactory
<WebcamPrivateAPI
>* GetFactoryInstance();
28 // Convenience method to get the WebcamPrivateAPI for a BrowserContext.
29 static WebcamPrivateAPI
* Get(content::BrowserContext
* context
);
31 explicit WebcamPrivateAPI(content::BrowserContext
* context
);
32 ~WebcamPrivateAPI() override
;
34 Webcam
* GetWebcam(const std::string
& extension_id
,
35 const std::string
& device_id
);
37 bool OpenSerialWebcam(
38 const std::string
& extension_id
,
39 const std::string
& device_path
,
40 const base::Callback
<void(const std::string
&, bool)>& callback
);
41 bool CloseWebcam(const std::string
& extension_id
,
42 const std::string
& device_id
);
45 friend class BrowserContextKeyedAPIFactory
<WebcamPrivateAPI
>;
47 void OnOpenSerialWebcam(
48 const std::string
& extension_id
,
49 const std::string
& device_path
,
50 scoped_refptr
<Webcam
> webcam
,
51 const base::Callback
<void(const std::string
&, bool)>& callback
,
54 // Note: This function does not work for serial devices. Do not use this
55 // function for serial devices.
56 bool GetDeviceId(const std::string
& extension_id
,
57 const std::string
& webcam_id
,
58 std::string
* device_id
);
59 std::string
GetWebcamId(const std::string
& extension_id
,
60 const std::string
& device_id
);
62 WebcamResource
* FindWebcamResource(const std::string
& extension_id
,
63 const std::string
& webcam_id
) const;
64 bool RemoveWebcamResource(const std::string
& extension_id
,
65 const std::string
& webcam_id
);
67 // BrowserContextKeyedAPI:
68 static const char* service_name() {
69 return "WebcamPrivateAPI";
71 static const bool kServiceIsNULLWhileTesting
= true;
72 static const bool kServiceRedirectedInIncognito
= true;
74 content::BrowserContext
* const browser_context_
;
75 scoped_ptr
<ApiResourceManager
<WebcamResource
>> webcam_resource_manager_
;
77 base::WeakPtrFactory
<WebcamPrivateAPI
> weak_ptr_factory_
;
79 DISALLOW_COPY_AND_ASSIGN(WebcamPrivateAPI
);
83 void BrowserContextKeyedAPIFactory
<WebcamPrivateAPI
>
84 ::DeclareFactoryDependencies();
86 class WebcamPrivateOpenSerialWebcamFunction
: public AsyncExtensionFunction
{
88 WebcamPrivateOpenSerialWebcamFunction();
89 DECLARE_EXTENSION_FUNCTION("webcamPrivate.openSerialWebcam",
90 WEBCAMPRIVATE_OPENSERIALWEBCAM
);
93 ~WebcamPrivateOpenSerialWebcamFunction() override
;
95 // AsyncExtensionFunction:
96 bool RunAsync() override
;
99 void OnOpenWebcam(const std::string
& webcam_id
, bool success
);
101 DISALLOW_COPY_AND_ASSIGN(WebcamPrivateOpenSerialWebcamFunction
);
104 class WebcamPrivateCloseWebcamFunction
: public AsyncExtensionFunction
{
106 WebcamPrivateCloseWebcamFunction();
107 DECLARE_EXTENSION_FUNCTION("webcamPrivate.closeWebcam",
108 WEBCAMPRIVATE_CLOSEWEBCAM
);
111 ~WebcamPrivateCloseWebcamFunction() override
;
114 bool RunAsync() override
;
117 DISALLOW_COPY_AND_ASSIGN(WebcamPrivateCloseWebcamFunction
);
120 class WebcamPrivateSetFunction
: public SyncExtensionFunction
{
122 WebcamPrivateSetFunction();
123 DECLARE_EXTENSION_FUNCTION("webcamPrivate.set", WEBCAMPRIVATE_SET
);
126 ~WebcamPrivateSetFunction() override
;
127 bool RunSync() override
;
130 DISALLOW_COPY_AND_ASSIGN(WebcamPrivateSetFunction
);
133 class WebcamPrivateGetFunction
: public SyncExtensionFunction
{
135 WebcamPrivateGetFunction();
136 DECLARE_EXTENSION_FUNCTION("webcamPrivate.get", WEBCAMPRIVATE_GET
);
139 ~WebcamPrivateGetFunction() override
;
140 bool RunSync() override
;
143 DISALLOW_COPY_AND_ASSIGN(WebcamPrivateGetFunction
);
146 class WebcamPrivateResetFunction
: public SyncExtensionFunction
{
148 WebcamPrivateResetFunction();
149 DECLARE_EXTENSION_FUNCTION("webcamPrivate.reset", WEBCAMPRIVATE_RESET
);
152 ~WebcamPrivateResetFunction() override
;
153 bool RunSync() override
;
156 DISALLOW_COPY_AND_ASSIGN(WebcamPrivateResetFunction
);
159 } // namespace extensions
161 #endif // EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_WEBCAM_PRIVATE_API_H_