Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / desktop_capture / desktop_capture_base.h
blob92ea6cd3ad67370a020658e75b4b6b3e2e4bfc0c
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_
8 #include <map>
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"
17 #include "url/gurl.h"
19 namespace extensions {
21 class DesktopCaptureChooseDesktopMediaFunctionBase
22 : public ChromeAsyncExtensionFunction,
23 public content::WebContentsObserver {
24 public:
25 // Factory creating DesktopMediaList and DesktopMediaPicker instances.
26 // Used for tests to supply fake picker.
27 class PickerFactory {
28 public:
29 virtual scoped_ptr<DesktopMediaList> CreateModel(bool show_screens,
30 bool show_windows) = 0;
31 virtual scoped_ptr<DesktopMediaPicker> CreatePicker() = 0;
32 protected:
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();
43 void Cancel();
45 protected:
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.
52 bool Execute(
53 const std::vector<api::desktop_capture::DesktopCaptureSourceType>&
54 sources,
55 content::WebContents* web_contents,
56 const GURL& origin,
57 const base::string16 target_name);
59 int request_id_;
61 private:
62 // content::WebContentsObserver overrides.
63 void WebContentsDestroyed() override;
65 void OnPickerDialogResults(content::DesktopMediaID source);
67 // URL of page that desktop capture was requested for.
68 GURL origin_;
70 scoped_ptr<DesktopMediaPicker> picker_;
73 class DesktopCaptureCancelChooseDesktopMediaFunctionBase
74 : public ChromeSyncExtensionFunction {
75 public:
76 DesktopCaptureCancelChooseDesktopMediaFunctionBase();
78 protected:
79 ~DesktopCaptureCancelChooseDesktopMediaFunctionBase() override;
81 private:
82 // ExtensionFunction overrides.
83 bool RunSync() override;
86 class DesktopCaptureRequestsRegistry {
87 public:
88 DesktopCaptureRequestsRegistry();
89 ~DesktopCaptureRequestsRegistry();
91 static DesktopCaptureRequestsRegistry* GetInstance();
93 void AddRequest(int process_id,
94 int request_id,
95 DesktopCaptureChooseDesktopMediaFunctionBase* handler);
96 void RemoveRequest(int process_id, int request_id);
97 void CancelRequest(int process_id, int request_id);
99 private:
100 friend struct base::DefaultSingletonTraits<DesktopCaptureRequestsRegistry>;
102 struct RequestId {
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;
108 int process_id;
109 int request_id;
112 using RequestsMap =
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_