1 // Copyright (c) 2012 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_TAB_CAPTURE_TAB_CAPTURE_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_TAB_CAPTURE_TAB_CAPTURE_REGISTRY_H_
12 #include "base/memory/scoped_vector.h"
13 #include "base/scoped_observer.h"
14 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
15 #include "chrome/common/extensions/api/tab_capture.h"
16 #include "content/public/browser/media_request_state.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/browser/extension_registry_observer.h"
29 namespace extensions {
31 class ExtensionRegistry;
33 namespace tab_capture = api::tab_capture;
35 class TabCaptureRegistry : public BrowserContextKeyedAPI,
36 public ExtensionRegistryObserver,
37 public MediaCaptureDevicesDispatcher::Observer {
39 static TabCaptureRegistry* Get(content::BrowserContext* context);
41 // Used by BrowserContextKeyedAPI.
42 static BrowserContextKeyedAPIFactory<TabCaptureRegistry>*
45 // List all pending, active and stopped capture requests.
46 void GetCapturedTabs(const std::string& extension_id,
47 base::ListValue* list_of_capture_info) const;
49 // Add a tab capture request to the registry when a stream is requested
50 // through the API. |target_contents| refers to the WebContents associated
51 // with the tab to be captured. |extension_id| refers to the Extension
52 // initiating the request.
53 bool AddRequest(content::WebContents* target_contents,
54 const std::string& extension_id);
56 // Called by MediaStreamDevicesController to verify the request before
57 // creating the stream. |render_process_id| and |render_frame_id| are used to
58 // look-up a WebContents instance, which should match the |target_contents|
59 // from the prior call to AddRequest(). In addition, a request is not
60 // verified unless the |extension_id| also matches AND the request itself is
61 // in the PENDING state.
62 bool VerifyRequest(int render_process_id,
64 const std::string& extension_id);
67 friend class BrowserContextKeyedAPIFactory<TabCaptureRegistry>;
70 explicit TabCaptureRegistry(content::BrowserContext* context);
71 virtual ~TabCaptureRegistry();
73 // Used by BrowserContextKeyedAPI.
74 static const char* service_name() {
75 return "TabCaptureRegistry";
78 static const bool kServiceIsCreatedWithBrowserContext = false;
79 static const bool kServiceRedirectedInIncognito = true;
81 // ExtensionRegistryObserver implementation.
82 virtual void OnExtensionUnloaded(
83 content::BrowserContext* browser_context,
84 const Extension* extension,
85 UnloadedExtensionInfo::Reason reason) OVERRIDE;
87 // MediaCaptureDevicesDispatcher::Observer implementation.
88 virtual void OnRequestUpdate(
89 int original_target_render_process_id,
90 int original_target_render_frame_id,
91 content::MediaStreamType stream_type,
92 const content::MediaRequestState state) OVERRIDE;
94 // Send a StatusChanged event containing the current state of |request|.
95 void DispatchStatusChangeEvent(const LiveRequest* request) const;
97 // Look-up a LiveRequest associated with the given |target_contents| (or
98 // the originally targetted RenderFrameHost), if any.
99 LiveRequest* FindRequest(const content::WebContents* target_contents) const;
100 LiveRequest* FindRequest(int original_target_render_process_id,
101 int original_target_render_frame_id) const;
103 // Removes the |request| from |requests_|, thus causing its destruction.
104 void KillRequest(LiveRequest* request);
106 content::BrowserContext* const browser_context_;
107 ScopedVector<LiveRequest> requests_;
109 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
110 extension_registry_observer_;
112 DISALLOW_COPY_AND_ASSIGN(TabCaptureRegistry);
115 } // namespace extensions
117 #endif // CHROME_BROWSER_EXTENSIONS_API_TAB_CAPTURE_TAB_CAPTURE_REGISTRY_H_