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 CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_MANAGER_H_
6#define CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_MANAGER_H_
7
8#include "base/id_map.h"
9#include "base/memory/ref_counted.h"
10#include "base/memory/weak_ptr.h"
11#include "base/observer_list.h"
12#include "content/public/renderer/render_view_observer.h"
13#include "ipc/ipc_sender.h"
14
15namespace base {
16class DictionaryValue;
17}
18
19namespace blink {
20class WebFrame;
21class WebNode;
22struct WebPluginParams;
23}
24
25namespace content {
26
27class BrowserPlugin;
28class BrowserPluginDelegate;
29class BrowserPluginManagerFactory;
30class RenderViewImpl;
31
32// BrowserPluginManager manages the routing of messages to the appropriate
33// BrowserPlugin object based on its instance ID.
34class CONTENT_EXPORT BrowserPluginManager
35    : public RenderViewObserver,
36      public base::RefCounted<BrowserPluginManager> {
37 public:
38  // Returns the one BrowserPluginManager for this process.
39  static BrowserPluginManager* Create(RenderViewImpl* render_view);
40
41  // Overrides factory for testing. Default (NULL) value indicates regular
42  // (non-test) environment.
43  static void set_factory_for_testing(BrowserPluginManagerFactory* factory) {
44    BrowserPluginManager::factory_ = factory;
45  }
46
47  explicit BrowserPluginManager(RenderViewImpl* render_view);
48
49  // Creates a new BrowserPlugin object.
50  // BrowserPlugin is responsible for associating itself with the
51  // BrowserPluginManager via AddBrowserPlugin. When it is destroyed, it is
52  // responsible for removing its association via RemoveBrowserPlugin.
53  virtual BrowserPlugin* CreateBrowserPlugin(
54      RenderViewImpl* render_view,
55      blink::WebFrame* frame,
56      scoped_ptr<BrowserPluginDelegate> delegate) = 0;
57
58  void Attach(int browser_plugin_instance_id);
59
60  void AddBrowserPlugin(int browser_plugin_instance_id,
61                        BrowserPlugin* browser_plugin);
62  void RemoveBrowserPlugin(int browser_plugin_instance_id);
63  BrowserPlugin* GetBrowserPlugin(int browser_plugin_instance_id) const;
64
65  void UpdateDeviceScaleFactor();
66  void UpdateFocusState();
67  RenderViewImpl* render_view() const { return render_view_.get(); }
68  int GetNextInstanceID();
69
70  // RenderViewObserver implementation.
71
72  // BrowserPluginManager must override the default Send behavior.
73  virtual bool Send(IPC::Message* msg) OVERRIDE = 0;
74
75  // Don't destroy the BrowserPluginManager when the RenderViewImpl goes away.
76  // BrowserPluginManager's lifetime is managed by a reference count. Once
77  // the host RenderViewImpl and all BrowserPlugins release their references,
78  // then the BrowserPluginManager will be destroyed.
79  virtual void OnDestruct() OVERRIDE {}
80
81 protected:
82  // Friend RefCounted so that the dtor can be non-public.
83  friend class base::RefCounted<BrowserPluginManager>;
84
85  // Static factory instance (always NULL for non-test).
86  static BrowserPluginManagerFactory* factory_;
87
88  virtual ~BrowserPluginManager();
89  // This map is keyed by guest instance IDs.
90  IDMap<BrowserPlugin> instances_;
91  int current_instance_id_;
92  base::WeakPtr<RenderViewImpl> render_view_;
93
94  DISALLOW_COPY_AND_ASSIGN(BrowserPluginManager);
95};
96
97}  // namespace content
98
99#endif //  CONTENT_RENDERER_BROWSER_PLUGIN_BROWSER_PLUGIN_MANAGER_H_
100