1 // Copyright 2015 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 CHROME_BROWSER_EXTENSIONS_API_DESKTOP_CAPTURE_DESKTOP_CAPTURE_BASE_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DESKTOP_CAPTURE_DESKTOP_CAPTURE_BASE_H_
10 #include "base/memory/singleton.h"
11 #include "chrome/browser/extensions/chrome_extension_function.h"
12 #include "chrome/browser/media/desktop_media_list.h"
13 #include "chrome/browser/media/desktop_media_picker.h"
14 #include "chrome/browser/media/native_desktop_media_list.h"
15 #include "chrome/common/extensions/api/desktop_capture.h"
16 #include "content/public/browser/web_contents_observer.h"
19 namespace extensions
{
21 class DesktopCaptureChooseDesktopMediaFunctionBase
22 : public ChromeAsyncExtensionFunction
,
23 public content::WebContentsObserver
{
25 // Factory creating DesktopMediaList and DesktopMediaPicker instances.
26 // Used for tests to supply fake picker.
29 virtual scoped_ptr
<DesktopMediaList
> CreateModel(bool show_screens
,
30 bool show_windows
) = 0;
31 virtual scoped_ptr
<DesktopMediaPicker
> CreatePicker() = 0;
33 virtual ~PickerFactory() {}
36 // Used to set PickerFactory used to create mock DesktopMediaPicker instances
37 // for tests. Calling tests keep ownership of the factory. Can be called with
38 // |factory| set to NULL at the end of the test.
39 static void SetPickerFactoryForTests(PickerFactory
* factory
);
41 DesktopCaptureChooseDesktopMediaFunctionBase();
46 ~DesktopCaptureChooseDesktopMediaFunctionBase() override
;
48 // |web_contents| is the WebContents for which the stream is created, and will
49 // also be used to determine where to show the picker's UI.
50 // |origin| is the origin for which the stream is created.
51 // |target_name| is the display name of the stream target.
53 const std::vector
<api::desktop_capture::DesktopCaptureSourceType
>&
55 content::WebContents
* web_contents
,
57 const base::string16 target_name
);
62 // content::WebContentsObserver overrides.
63 void WebContentsDestroyed() override
;
65 void OnPickerDialogResults(content::DesktopMediaID source
);
67 // URL of page that desktop capture was requested for.
70 scoped_ptr
<DesktopMediaPicker
> picker_
;
73 class DesktopCaptureCancelChooseDesktopMediaFunctionBase
74 : public ChromeSyncExtensionFunction
{
76 DesktopCaptureCancelChooseDesktopMediaFunctionBase();
79 ~DesktopCaptureCancelChooseDesktopMediaFunctionBase() override
;
82 // ExtensionFunction overrides.
83 bool RunSync() override
;
86 class DesktopCaptureRequestsRegistry
{
88 DesktopCaptureRequestsRegistry();
89 ~DesktopCaptureRequestsRegistry();
91 static DesktopCaptureRequestsRegistry
* GetInstance();
93 void AddRequest(int process_id
,
95 DesktopCaptureChooseDesktopMediaFunctionBase
* handler
);
96 void RemoveRequest(int process_id
, int request_id
);
97 void CancelRequest(int process_id
, int request_id
);
100 friend struct base::DefaultSingletonTraits
<DesktopCaptureRequestsRegistry
>;
103 RequestId(int process_id
, int request_id
);
105 // Need to use RequestId as a key in std::map<>.
106 bool operator<(const RequestId
& other
) const;
113 std::map
<RequestId
, DesktopCaptureChooseDesktopMediaFunctionBase
*>;
115 RequestsMap requests_
;
117 DISALLOW_COPY_AND_ASSIGN(DesktopCaptureRequestsRegistry
);
120 } // namespace extensions
122 #endif // CHROME_BROWSER_EXTENSIONS_API_DESKTOP_CAPTURE_DESKTOP_CAPTURE_BASE_H_