summaryrefslogtreecommitdiffstats
path: root/content/browser/browser_plugin/browser_plugin_embedder.h
blob: b4e846aef1c953bbb254d29e1d00dbae8a61e552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// A BrowserPluginEmbedder has a list of guests it manages.
// In the beginning when a renderer sees one or more guests (BrowserPlugin
// instance(s)) and there is a request to navigate to them, the WebContents for
// that renderer creates a BrowserPluginEmbedder for itself. The
// BrowserPluginEmbedder, in turn, manages a set of BrowserPluginGuests -- one
// BrowserPluginGuest for each guest in the embedding WebContents. Note that
// each of these BrowserPluginGuest objects has its own WebContents.
// BrowserPluginEmbedder routes any messages directed to a guest from the
// renderer (BrowserPlugin) to the appropriate guest (identified by the guest's
// |instance_id|).
//
// BrowserPluginEmbedder is responsible for cleaning up the guests when the
// embedder frame navigates away to a different page or deletes the guests from
// the existing page.

#ifndef CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_
#define CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_

#include <map>

#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"

struct BrowserPluginHostMsg_CreateGuest_Params;
struct BrowserPluginHostMsg_ResizeGuest_Params;

namespace gfx {
class Point;
}

namespace content {

class BrowserPluginGuest;
class BrowserPluginHostFactory;
class WebContentsImpl;

// A browser plugin embedder provides functionality for WebContents to operate
// in the 'embedder' role. It manages list of guests inside the embedder.
//
// The embedder's WebContents manages the lifetime of the embedder. They are
// created when a renderer asks WebContents to navigate (for the first time) to
// some guest. It gets destroyed when either the WebContents goes away or there
// is a RenderViewHost swap in WebContents.
class CONTENT_EXPORT BrowserPluginEmbedder : public WebContentsObserver,
                                             public NotificationObserver {
 public:
  typedef std::map<int, WebContents*> ContainerInstanceMap;

  virtual ~BrowserPluginEmbedder();

  static BrowserPluginEmbedder* Create(WebContentsImpl* web_contents,
                                       RenderViewHost* render_view_host);

  // Overrides factory for testing. Default (NULL) value indicates regular
  // (non-test) environment.
  static void set_factory_for_testing(BrowserPluginHostFactory* factory) {
    factory_ = factory;
  }

  bool visible() const { return visible_; }

  // Returns the RenderViewHost at a point (|x|, |y|) asynchronously via
  // |callback|. We need a roundtrip to renderer process to get this
  // information.
  void GetRenderViewHostAtPosition(
      int x,
      int y,
      const WebContents::GetRenderViewHostCallback& callback);

  // WebContentsObserver implementation.
  virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE;
  virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE;
  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;

  // NotificationObserver method override.
  virtual void Observe(int type,
                       const NotificationSource& source,
                       const NotificationDetails& details) OVERRIDE;

 private:
  friend class TestBrowserPluginEmbedder;

  BrowserPluginEmbedder(WebContentsImpl* web_contents,
                        RenderViewHost* render_view_host);

  // Returns a guest browser plugin delegate by its container ID specified
  // in BrowserPlugin.
  BrowserPluginGuest* GetGuestByInstanceID(int instance_id) const;
  // Adds a new guest web_contents to the embedder (overridable in test).
  virtual void AddGuest(int instance_id, WebContents* guest_web_contents);
  void DestroyGuestByInstanceID(int instance_id);
  void CleanUp();

  // Called when visiblity of web_contents changes, so the embedder will
  // show/hide its guest.
  void WebContentsVisibilityChanged(bool visible);

  static bool ShouldForwardToBrowserPluginGuest(const IPC::Message& message);

  // Message handlers.

  void OnCreateGuest(int instance_id,
                     const BrowserPluginHostMsg_CreateGuest_Params& params);
  void OnNavigateGuest(int instance_id, const std::string& src);
  void OnPluginAtPositionResponse(int instance_id,
                                  int request_id,
                                  const gfx::Point& position);
  void OnPluginDestroyed(int instance_id);
  void OnSwapBuffersACK(int route_id,
                        int gpu_host_id,
                        const std::string& mailbox_name,
                        uint32 sync_point);

  // Static factory instance (always NULL for non-test).
  static BrowserPluginHostFactory* factory_;

  // A scoped container for notification registries.
  NotificationRegistrar registrar_;

  // Contains guests' WebContents, mapping from their instance ids.
  ContainerInstanceMap guest_web_contents_by_instance_id_;
  RenderViewHost* render_view_host_;
  // Tracks the visibility state of the embedder.
  bool visible_;
  // Map that contains outstanding queries to |GetBrowserPluginAt|.
  // We need a roundtrip to renderer process to know the answer, therefore
  // storing these callbacks is required.
  typedef std::map<int, WebContents::GetRenderViewHostCallback>
      GetRenderViewHostCallbackMap;
  GetRenderViewHostCallbackMap pending_get_render_view_callbacks_;
  // Next request id for BrowserPluginMsg_PluginAtPositionRequest query.
  int next_get_render_view_request_id_;

  DISALLOW_COPY_AND_ASSIGN(BrowserPluginEmbedder);
};

}  // namespace content

#endif  // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_