summaryrefslogtreecommitdiffstats
path: root/content/browser/browser_plugin/browser_plugin_host.cc
blob: 49b044c0b660579b456425bac27662bd37c29146 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// 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.

#include "content/browser/browser_plugin/browser_plugin_host.h"

#include "content/browser/browser_plugin/browser_plugin_host_helper.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/browser_plugin_messages.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents_view.h"
#include "ppapi/proxy/ppapi_messages.h"

namespace content {

BrowserPluginHost::BrowserPluginHost(
    WebContentsImpl* web_contents)
    : WebContentsObserver(web_contents),
      embedder_render_process_host_(NULL),
      instance_id_(0) {
  // Listen to visibility changes so that an embedder hides its guests
  // as well.
  registrar_.Add(this,
                 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED,
                 Source<WebContents>(web_contents));
  // Construct plumbing helpers when a new RenderViewHost is created for
  // this BrowserPluginHost's WebContentsImpl.
  registrar_.Add(this,
                 NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB,
                 Source<WebContents>(web_contents));
}

BrowserPluginHost::~BrowserPluginHost() {
}

BrowserPluginHost* BrowserPluginHost::GetGuestByContainerID(int container_id) {
  ContainerInstanceMap::const_iterator it =
      guests_by_container_id_.find(container_id);
  if (it != guests_by_container_id_.end())
    return it->second;
  return NULL;
}

void BrowserPluginHost::RegisterContainerInstance(
    int container_id,
    BrowserPluginHost* observer) {
  DCHECK(guests_by_container_id_.find(container_id) ==
      guests_by_container_id_.end());
  guests_by_container_id_[container_id] = observer;
}

bool BrowserPluginHost::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(BrowserPluginHost, message)
    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateFromGuest,
                        OnNavigateFromGuest)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void BrowserPluginHost::NavigateGuestFromEmbedder(
    RenderViewHost* render_view_host,
    int container_instance_id,
    long long frame_id,
    const std::string& src) {
  BrowserPluginHost* guest_observer =
      GetGuestByContainerID(container_instance_id);
  WebContentsImpl* guest_web_contents =
      guest_observer ?
          static_cast<WebContentsImpl*>(guest_observer->web_contents()): NULL;
  if (!guest_observer) {
    guest_web_contents =
        static_cast<WebContentsImpl*>(
            WebContents::Create(
                web_contents()->GetBrowserContext(),
                render_view_host->GetSiteInstance(),
                MSG_ROUTING_NONE,
                NULL, // base WebContents
                NULL  // session storage namespace
            ));
    guest_observer =
        guest_web_contents->browser_plugin_host();
    guest_observer->set_embedder_render_process_host(
        render_view_host->GetProcess());
    guest_observer->set_instance_id(container_instance_id);
    RegisterContainerInstance(container_instance_id, guest_observer);
    AddGuest(guest_web_contents, frame_id);
  }
  GURL url(src);
  guest_observer->web_contents()->SetDelegate(guest_observer);
  guest_observer->web_contents()->GetController().LoadURL(
      url,
      Referrer(),
      PAGE_TRANSITION_AUTO_SUBFRAME,
      std::string());
}

void BrowserPluginHost::OnNavigateFromGuest(
    PP_Instance instance,
    const std::string& src) {
  DCHECK(embedder_render_process_host());
  GURL url(src);
  web_contents()->GetController().LoadURL(
      url,
      Referrer(),
      PAGE_TRANSITION_AUTO_SUBFRAME,
      std::string());
}

void BrowserPluginHost::ConnectEmbedderToChannel(
    RenderViewHost* render_view_host,
    const IPC::ChannelHandle& channel_handle) {
  DCHECK(embedder_render_process_host());
  // Tell the BrowserPlugin in the embedder that we're done and that it can
  // begin using the guest renderer.
  embedder_render_process_host()->Send(
      new BrowserPluginMsg_LoadGuest(
          instance_id(),
          render_view_host->GetProcess()->
              GetID(),
          channel_handle));
}

void BrowserPluginHost::AddGuest(WebContentsImpl* guest, int64 frame_id) {
  guests_[guest] = frame_id;
}

void BrowserPluginHost::RemoveGuest(WebContentsImpl* guest) {
  guests_.erase(guest);
}

void BrowserPluginHost::DestroyGuests() {
  for (GuestMap::const_iterator it = guests_.begin();
       it != guests_.end(); ++it) {
    WebContentsImpl* web_contents = it->first;
    delete web_contents;
  }
  guests_.clear();
  guests_by_container_id_.clear();
}

void BrowserPluginHost::DidCommitProvisionalLoadForFrame(
    int64 frame_id,
    bool is_main_frame,
    const GURL& url,
    PageTransition transition_type,
    RenderViewHost* render_view_host) {
  // Clean-up guests that lie in the frame that we're navigating.
  typedef std::set<WebContentsImpl*> GuestSet;
  GuestSet guests_to_delete;
  for (GuestMap::const_iterator it = guests_.begin();
       it != guests_.end(); ++it) {
    WebContentsImpl* web_contents = it->first;
    if (it->second == frame_id) {
      guests_to_delete.insert(web_contents);
    }
  }
  for (GuestSet::const_iterator it = guests_to_delete.begin();
       it != guests_to_delete.end(); ++it) {
    delete *it;
    guests_.erase(*it);
  }
}

void BrowserPluginHost::RenderViewDeleted(RenderViewHost* render_view_host) {
  // TODO(fsamuel): For some reason ToT hangs when killing a guest, this wasn't
  // the case earlier. Presumably, when a guest is killed/crashes, one of
  // RenderViewDeleted, RenderViewGone or WebContentsDestroyed will get called.
  // At that point, we should remove reference to this BrowserPluginHost
  // from its embedder, in addition to destroying its guests, to ensure
  // that we do not attempt a double delete.
  DestroyGuests();
}

void BrowserPluginHost::RenderViewGone(base::TerminationStatus status) {
  DestroyGuests();
}

void BrowserPluginHost::WebContentsDestroyed(WebContents* web_contents) {
  DestroyGuests();
}

void BrowserPluginHost::Observe(
    int type,
    const NotificationSource& source,
    const NotificationDetails& details) {
  switch (type) {
    case NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB: {
      RenderViewHost* render_view_host =
          Details<RenderViewHost>(details).ptr();
      // BrowserPluginHostHelper is destroyed when its associated RenderViewHost
      // is destroyed.
      new BrowserPluginHostHelper(this, render_view_host);
      break;
    }
    case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: {
      bool visible = *Details<bool>(details).ptr();
      // If the embedder is hidden we need to hide the guests as well.
      for (GuestMap::const_iterator it = guests_.begin();
          it != guests_.end(); ++it) {
        WebContentsImpl* web_contents = it->first;
        if (visible)
          web_contents->ShowContents();
        else
          web_contents->HideContents();
      }
      break;
    }
    default:
      NOTREACHED() << "Unexpected notification type: " << type;
  }
}

}  // namespace content