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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_API_TAB_CAPTURE_TAB_CAPTURE_REGISTRY_H_
6#define CHROME_BROWSER_EXTENSIONS_API_TAB_CAPTURE_TAB_CAPTURE_REGISTRY_H_
7
8#include <string>
9#include <utility>
10#include <vector>
11
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"
19
20namespace base {
21class ListValue;
22}
23
24namespace content {
25class BrowserContext;
26class WebContents;
27}
28
29namespace extensions {
30
31class ExtensionRegistry;
32
33namespace tab_capture = api::tab_capture;
34
35class TabCaptureRegistry : public BrowserContextKeyedAPI,
36                           public ExtensionRegistryObserver,
37                           public MediaCaptureDevicesDispatcher::Observer {
38 public:
39  static TabCaptureRegistry* Get(content::BrowserContext* context);
40
41  // Used by BrowserContextKeyedAPI.
42  static BrowserContextKeyedAPIFactory<TabCaptureRegistry>*
43      GetFactoryInstance();
44
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;
48
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);
55
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,
63                     int render_frame_id,
64                     const std::string& extension_id);
65
66 private:
67  friend class BrowserContextKeyedAPIFactory<TabCaptureRegistry>;
68  class LiveRequest;
69
70  explicit TabCaptureRegistry(content::BrowserContext* context);
71  virtual ~TabCaptureRegistry();
72
73  // Used by BrowserContextKeyedAPI.
74  static const char* service_name() {
75    return "TabCaptureRegistry";
76  }
77
78  static const bool kServiceIsCreatedWithBrowserContext = false;
79  static const bool kServiceRedirectedInIncognito = true;
80
81  // ExtensionRegistryObserver implementation.
82  virtual void OnExtensionUnloaded(
83      content::BrowserContext* browser_context,
84      const Extension* extension,
85      UnloadedExtensionInfo::Reason reason) OVERRIDE;
86
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;
93
94  // Send a StatusChanged event containing the current state of |request|.
95  void DispatchStatusChangeEvent(const LiveRequest* request) const;
96
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;
102
103  // Removes the |request| from |requests_|, thus causing its destruction.
104  void KillRequest(LiveRequest* request);
105
106  content::BrowserContext* const browser_context_;
107  ScopedVector<LiveRequest> requests_;
108
109  ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
110      extension_registry_observer_;
111
112  DISALLOW_COPY_AND_ASSIGN(TabCaptureRegistry);
113};
114
115}  // namespace extensions
116
117#endif  // CHROME_BROWSER_EXTENSIONS_API_TAB_CAPTURE_TAB_CAPTURE_REGISTRY_H_
118