render_frame_host_impl.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/browser/frame_host/render_frame_host_impl.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/bind.h"
8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/containers/hash_tables.h"
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/lazy_instance.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/metrics/user_metrics_action.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/browser/child_process_security_policy_impl.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/browser/frame_host/cross_process_frame_connector.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/frame_host/cross_site_transferring_request.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/browser/frame_host/frame_tree.h"
15a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/browser/frame_host/frame_tree_node.h"
16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/browser/frame_host/navigator.h"
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/browser/frame_host/render_frame_host_delegate.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/browser/frame_host/render_frame_proxy_host.h"
19effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/browser/renderer_host/input/input_router.h"
20effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/browser/renderer_host/input/timeout_monitor.h"
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/browser/renderer_host/render_view_host_impl.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/browser/renderer_host/render_widget_host_impl.h"
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/common/desktop_notification_messages.h"
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/common/frame_messages.h"
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/common/input_messages.h"
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/common/inter_process_time_ticks_converter.h"
2723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "content/common/swapped_out_messages.h"
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/content_browser_client.h"
300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/browser/desktop_notification_delegate.h"
314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/public/browser/render_process_host.h"
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/render_widget_host_view.h"
33a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/public/browser/user_metrics.h"
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/common/content_constants.h"
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/common/url_constants.h"
36c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "content/public/common/url_utils.h"
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "url/gurl.h"
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using base::TimeDelta;
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace content {
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochnamespace {
440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// The (process id, routing id) pair that identifies one RenderFrame.
46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)typedef std::pair<int32, int32> RenderFrameHostID;
47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)typedef base::hash_map<RenderFrameHostID, RenderFrameHostImpl*>
48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    RoutingIDFrameMap;
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochbase::LazyInstance<RoutingIDFrameMap> g_routing_id_frame_map =
50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass DesktopNotificationDelegateImpl : public DesktopNotificationDelegate {
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch public:
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  DesktopNotificationDelegateImpl(RenderFrameHost* render_frame_host,
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                  int notification_id)
560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      : render_process_id_(render_frame_host->GetProcess()->GetID()),
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        render_frame_id_(render_frame_host->GetRoutingID()),
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        notification_id_(notification_id) {}
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual ~DesktopNotificationDelegateImpl() {}
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual void NotificationDisplayed() OVERRIDE {
630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    RenderFrameHost* rfh =
640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        RenderFrameHost::FromID(render_process_id_, render_frame_id_);
650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (!rfh)
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    rfh->Send(new DesktopNotificationMsg_PostDisplay(
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        rfh->GetRoutingID(), notification_id_));
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual void NotificationError() OVERRIDE {
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    RenderFrameHost* rfh =
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        RenderFrameHost::FromID(render_process_id_, render_frame_id_);
750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (!rfh)
760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    rfh->Send(new DesktopNotificationMsg_PostError(
790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        rfh->GetRoutingID(), notification_id_));
800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    delete this;
810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual void NotificationClosed(bool by_user) OVERRIDE {
840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    RenderFrameHost* rfh =
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        RenderFrameHost::FromID(render_process_id_, render_frame_id_);
860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (!rfh)
870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    rfh->Send(new DesktopNotificationMsg_PostClose(
900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        rfh->GetRoutingID(), notification_id_, by_user));
910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    static_cast<RenderFrameHostImpl*>(rfh)->NotificationClosed(
920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        notification_id_);
930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    delete this;
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual void NotificationClick() OVERRIDE {
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    RenderFrameHost* rfh =
980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        RenderFrameHost::FromID(render_process_id_, render_frame_id_);
990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (!rfh)
1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    rfh->Send(new DesktopNotificationMsg_PostClick(
1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        rfh->GetRoutingID(), notification_id_));
1040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
1050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch private:
1070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  int render_process_id_;
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  int render_frame_id_;
1090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  int notification_id_;
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch};
1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Translate a WebKit text direction into a base::i18n one.
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)base::i18n::TextDirection WebTextDirectionToChromeTextDirection(
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    blink::WebTextDirection dir) {
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  switch (dir) {
116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case blink::WebTextDirectionLeftToRight:
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return base::i18n::LEFT_TO_RIGHT;
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case blink::WebTextDirectionRightToLeft:
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return base::i18n::RIGHT_TO_LEFT;
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    default:
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      NOTREACHED();
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return base::i18n::UNKNOWN_DIRECTION;
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}  // namespace
1270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)RenderFrameHost* RenderFrameHost::FromID(int render_process_id,
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                         int render_frame_id) {
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return RenderFrameHostImpl::FromID(render_process_id, render_frame_id);
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
133424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// static
134424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)RenderFrameHostImpl* RenderFrameHostImpl::FromID(
135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    int process_id, int routing_id) {
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  RoutingIDFrameMap* frames = g_routing_id_frame_map.Pointer();
138424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  RoutingIDFrameMap::iterator it = frames->find(
139424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      RenderFrameHostID(process_id, routing_id));
140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return it == frames->end() ? NULL : it->second;
141424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RenderFrameHostImpl::RenderFrameHostImpl(
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    RenderViewHostImpl* render_view_host,
145a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    RenderFrameHostDelegate* delegate,
1464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    FrameTree* frame_tree,
147a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    FrameTreeNode* frame_tree_node,
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    int routing_id,
149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    bool is_swapped_out)
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : render_view_host_(render_view_host),
151a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      delegate_(delegate),
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      cross_process_frame_connector_(NULL),
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      render_frame_proxy_host_(NULL),
1544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      frame_tree_(frame_tree),
155a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      frame_tree_node_(frame_tree_node),
156424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      routing_id_(routing_id),
1570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      is_swapped_out_(is_swapped_out),
1580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      weak_ptr_factory_(this) {
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_->RegisterRenderFrameHost(this);
160424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GetProcess()->AddRoute(routing_id_, this);
161424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  g_routing_id_frame_map.Get().insert(std::make_pair(
162424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      RenderFrameHostID(GetProcess()->GetID(), routing_id_),
163424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      this));
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RenderFrameHostImpl::~RenderFrameHostImpl() {
167424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GetProcess()->RemoveRoute(routing_id_);
168424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  g_routing_id_frame_map.Get().erase(
169424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      RenderFrameHostID(GetProcess()->GetID(), routing_id_));
170a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (delegate_)
171a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    delegate_->RenderFrameDeleted(this);
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Notify the FrameTree that this RFH is going away, allowing it to shut down
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the corresponding RenderViewHost if it is no longer needed.
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_->UnregisterRenderFrameHost(this);
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)int RenderFrameHostImpl::GetRoutingID() {
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return routing_id_;
180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)SiteInstance* RenderFrameHostImpl::GetSiteInstance() {
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return render_view_host_->GetSiteInstance();
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)RenderProcessHost* RenderFrameHostImpl::GetProcess() {
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(nasko): This should return its own process, once we have working
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // cross-process navigation for subframes.
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return render_view_host_->GetProcess();
190a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
191424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)RenderFrameHost* RenderFrameHostImpl::GetParent() {
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FrameTreeNode* parent_node = frame_tree_node_->parent();
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!parent_node)
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return NULL;
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return parent_node->current_frame_host();
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
19923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)const std::string& RenderFrameHostImpl::GetFrameName() {
20023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return frame_tree_node_->frame_name();
20123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
20223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
203a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool RenderFrameHostImpl::IsCrossProcessSubframe() {
204a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  FrameTreeNode* parent_node = frame_tree_node_->parent();
205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!parent_node)
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return GetSiteInstance() !=
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      parent_node->current_frame_host()->GetSiteInstance();
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)GURL RenderFrameHostImpl::GetLastCommittedURL() {
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return frame_tree_node_->current_url();
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)gfx::NativeView RenderFrameHostImpl::GetNativeView() {
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderWidgetHostView* view = render_view_host_->GetView();
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!view)
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return NULL;
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return view->GetNativeView();
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::ExecuteJavaScript(
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const base::string16& javascript) {
224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Send(new FrameMsg_JavaScriptExecuteRequest(routing_id_,
225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                             javascript,
226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                             0, false));
227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::ExecuteJavaScript(
230a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     const base::string16& javascript,
231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     const JavaScriptResultCallback& callback) {
232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static int next_id = 1;
233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int key = next_id++;
234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Send(new FrameMsg_JavaScriptExecuteRequest(routing_id_,
235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                             javascript,
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                             key, true));
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  javascript_callbacks_.insert(std::make_pair(key, callback));
238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)RenderViewHost* RenderFrameHostImpl::GetRenderViewHost() {
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return render_view_host_;
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool RenderFrameHostImpl::Send(IPC::Message* message) {
245effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (IPC_MESSAGE_ID_CLASS(message->type()) == InputMsgStart) {
246effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return render_view_host_->input_router()->SendInput(
247effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        make_scoped_ptr(message));
248effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
249effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
250cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (render_view_host_->IsSwappedOut()) {
251cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    DCHECK(render_frame_proxy_host_);
252cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return render_frame_proxy_host_->Send(message);
253cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
254cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
255424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return GetProcess()->Send(message);
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool RenderFrameHostImpl::OnMessageReceived(const IPC::Message &msg) {
25923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Filter out most IPC messages if this renderer is swapped out.
26023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // We still want to handle certain ACKs to keep our state consistent.
26123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // TODO(nasko): Only check RenderViewHost state, as this object's own state
26223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // isn't yet properly updated. Transition this check once the swapped out
26323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // state is correct in RenderFrameHost itself.
26423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (render_view_host_->IsSwappedOut()) {
26523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    if (!SwappedOutMessages::CanHandleWhileSwappedOut(msg)) {
26623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      // If this is a synchronous message and we decided not to handle it,
26723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      // we must send an error reply, or else the renderer will be stuck
26823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      // and won't respond to future requests.
26923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      if (msg.is_sync()) {
27023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg);
27123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        reply->set_reply_error();
27223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        Send(reply);
27323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      }
27423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      // Don't continue looking for someone to handle it.
27523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      return true;
27623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    }
27723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  }
27823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
279a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (delegate_->OnMessageReceived(this, msg))
280a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return true;
281a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
2826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  RenderFrameProxyHost* proxy =
2836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      frame_tree_node_->render_manager()->GetProxyToParent();
2846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (proxy && proxy->cross_process_frame_connector() &&
2856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      proxy->cross_process_frame_connector()->OnMessageReceived(msg))
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool handled = true;
289cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IPC_BEGIN_MESSAGE_MAP(RenderFrameHostImpl, msg)
290c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC_MESSAGE_HANDLER(FrameHostMsg_AddMessageToConsole, OnAddMessageToConsole)
2914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_Detach, OnDetach)
292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_FrameFocused, OnFrameFocused)
2938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidStartProvisionalLoadForFrame,
2948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                        OnDidStartProvisionalLoadForFrame)
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidFailProvisionalLoadWithError,
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        OnDidFailProvisionalLoadWithError)
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidRedirectProvisionalLoad,
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        OnDidRedirectProvisionalLoad)
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidFailLoadWithError,
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        OnDidFailLoadWithError)
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER_GENERIC(FrameHostMsg_DidCommitProvisionalLoad,
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                OnNavigate(msg))
303a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_OpenURL, OnOpenURL)
304010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DocumentOnLoadCompleted,
305010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                        OnDocumentOnLoadCompleted)
306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_BeforeUnload_ACK, OnBeforeUnloadACK)
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_SwapOut_ACK, OnSwapOutACK)
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_ContextMenu, OnContextMenu)
309a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_JavaScriptExecuteResponse,
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                        OnJavaScriptExecuteResponse)
311c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC_MESSAGE_HANDLER_DELAY_REPLY(FrameHostMsg_RunJavaScriptMessage,
312c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                    OnRunJavaScriptMessage)
313c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC_MESSAGE_HANDLER_DELAY_REPLY(FrameHostMsg_RunBeforeUnloadConfirm,
314c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                    OnRunBeforeUnloadConfirm)
3155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    IPC_MESSAGE_HANDLER(FrameHostMsg_DidAccessInitialDocument,
3165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                        OnDidAccessInitialDocument)
317010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisownOpener, OnDidDisownOpener)
318cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateTitle, OnUpdateTitle)
319cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateEncoding, OnUpdateEncoding)
3200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    IPC_MESSAGE_HANDLER(DesktopNotificationHostMsg_RequestPermission,
3210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                        OnRequestDesktopNotificationPermission)
3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    IPC_MESSAGE_HANDLER(DesktopNotificationHostMsg_Show,
3230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                        OnShowDesktopNotification)
3240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    IPC_MESSAGE_HANDLER(DesktopNotificationHostMsg_Cancel,
3250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                        OnCancelDesktopNotification)
326f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_TextSurroundingSelectionResponse,
327f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                        OnTextSurroundingSelectionResponse)
328cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IPC_END_MESSAGE_MAP()
329a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
3304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return handled;
331424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
332424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
333424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void RenderFrameHostImpl::Init() {
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetProcess()->ResumeRequestsForView(routing_id_);
335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
337c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::OnAddMessageToConsole(
338c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    int32 level,
339c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& message,
340c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    int32 line_no,
341c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& source_id) {
342c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (delegate_->AddMessageToConsole(level, message, line_no, source_id))
343c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    return;
344c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
345c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Pass through log level only on WebUI pages to limit console spew.
346c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  int32 resolved_level =
347c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      HasWebUIScheme(delegate_->GetMainFrameLastCommittedURL()) ? level : 0;
348c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
349c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (resolved_level >= ::logging::GetMinLogLevel()) {
350c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    logging::LogMessage("CONSOLE", line_no, resolved_level).stream() << "\"" <<
351c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        message << "\", source: " << source_id << " (" << line_no << ")";
352c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
353c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
354c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
355a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnCreateChildFrame(int new_routing_id,
3564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                             const std::string& frame_name) {
357a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RenderFrameHostImpl* new_frame = frame_tree_->AddFrame(
358a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      frame_tree_node_, new_routing_id, frame_name);
359a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (delegate_)
360a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    delegate_->RenderFrameCreated(new_frame);
3614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnDetach() {
364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_->RemoveFrame(frame_tree_node_);
365a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
367a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnFrameFocused() {
368a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_->SetFocusedFrame(frame_tree_node_);
369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnOpenURL(
372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const FrameHostMsg_OpenURL_Params& params) {
373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL validated_url(params.url);
374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GetProcess()->FilterURL(false, &validated_url);
375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_node_->navigator()->RequestOpenURL(
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      this, validated_url, params.referrer, params.disposition,
378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      params.should_replace_current_entry, params.user_gesture);
3794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
381010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void RenderFrameHostImpl::OnDocumentOnLoadCompleted() {
382010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // This message is only sent for top-level frames. TODO(avi): when frame tree
383010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // mirroring works correctly, add a check here to enforce it.
384010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  delegate_->DocumentOnLoadCompleted(this);
385010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
386010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
3878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void RenderFrameHostImpl::OnDidStartProvisionalLoadForFrame(
388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int parent_routing_id,
3898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const GURL& url) {
390a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  frame_tree_node_->navigator()->DidStartProvisionalLoad(
391effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      this, parent_routing_id, url);
3928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnDidFailProvisionalLoadWithError(
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const FrameHostMsg_DidFailProvisionalLoadWithError_Params& params) {
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_node_->navigator()->DidFailProvisionalLoadWithError(this, params);
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnDidFailLoadWithError(
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GURL& url,
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int error_code,
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::string16& error_description) {
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL validated_url(url);
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetProcess()->FilterURL(false, &validated_url);
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_node_->navigator()->DidFailLoadWithError(
407effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      this, validated_url, error_code, error_description);
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnDidRedirectProvisionalLoad(
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int32 page_id,
4125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GURL& source_url,
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GURL& target_url) {
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_node_->navigator()->DidRedirectProvisionalLoad(
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this, page_id, source_url, target_url);
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Called when the renderer navigates.  For every frame loaded, we'll get this
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// notification containing parameters identifying the navigation.
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Subframes are identified by the page transition type.  For subframes loaded
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// as part of a wider page load, the page_id will be the same as for the top
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// level frame.  If the user explicitly requests a subframe navigation, we will
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// get a new page_id because we need to create a new navigation entry for that
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// action.
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnNavigate(const IPC::Message& msg) {
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Read the parameters out of the IPC message directly to avoid making another
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // copy when we filter the URLs.
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PickleIterator iter(msg);
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FrameHostMsg_DidCommitProvisionalLoad_Params validated_params;
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!IPC::ParamTraits<FrameHostMsg_DidCommitProvisionalLoad_Params>::
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Read(&msg, &iter, &validated_params))
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If we're waiting for a cross-site beforeunload ack from this renderer and
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // we receive a Navigate message from the main frame, then the renderer was
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // navigating already and sent it before hearing the ViewMsg_Stop message.
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // We do not want to cancel the pending navigation in this case, since the
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // old page will soon be stopped.  Instead, treat this as a beforeunload ack
4405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // to allow the pending navigation to continue.
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (render_view_host_->is_waiting_for_beforeunload_ack_ &&
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      render_view_host_->unload_ack_is_for_cross_site_transition_ &&
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PageTransitionIsMainFrame(validated_params.transition)) {
444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    OnBeforeUnloadACK(true, send_before_unload_start_time_,
445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                      base::TimeTicks::Now());
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If we're waiting for an unload ack from this renderer and we receive a
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Navigate message, then the renderer was navigating before it received the
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // unload request.  It will either respond to the unload request soon or our
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // timer will expire.  Either way, we should ignore this message, because we
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // have already committed to closing this renderer.
4545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (render_view_host_->IsWaitingForUnloadACK())
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHost* process = GetProcess();
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Attempts to commit certain off-limits URL should be caught more strictly
4605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // than our FilterURL checks below.  If a renderer violates this policy, it
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // should be killed.
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!CanCommitURL(validated_params.url)) {
4635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    VLOG(1) << "Blocked URL " << validated_params.url.spec();
464f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    validated_params.url = GURL(url::kAboutBlankURL);
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RecordAction(base::UserMetricsAction("CanCommitURL_BlockedAndKilled"));
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Kills the process.
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    process->ReceivedBadMessage();
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Without this check, an evil renderer can trick the browser into creating
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // a navigation entry for a banned URL.  If the user clicks the back button
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // followed by the forward button (or clicks reload, or round-trips through
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // session restore, etc), we'll think that the browser commanded the
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // renderer to load the URL and grant the renderer the privileges to request
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the URL.  To prevent this attack, we block the renderer from inserting
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // banned URLs into the navigation controller in the first place.
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(false, &validated_params.url);
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.referrer.url);
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (std::vector<GURL>::iterator it(validated_params.redirects.begin());
4805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      it != validated_params.redirects.end(); ++it) {
4815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    process->FilterURL(false, &(*it));
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.searchable_form_url);
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Without this check, the renderer can trick the browser into using
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // filenames it can't access in a future session restore.
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!render_view_host_->CanAccessFilesOfPageState(
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          validated_params.page_state)) {
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetProcess()->ReceivedBadMessage();
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_node()->navigator()->DidNavigate(this, validated_params);
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
496cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)RenderWidgetHostImpl* RenderFrameHostImpl::GetRenderWidgetHost() {
497cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return static_cast<RenderWidgetHostImpl*>(render_view_host_);
498cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
499cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
500a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)int RenderFrameHostImpl::GetEnabledBindings() {
501a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return render_view_host_->GetEnabledBindings();
502a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
503a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
504a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnCrossSiteResponse(
505a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const GlobalRequestID& global_request_id,
506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::vector<GURL>& transfer_url_chain,
508a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const Referrer& referrer,
509a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    PageTransition page_transition,
510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool should_replace_current_entry) {
511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_node_->render_manager()->OnCrossSiteResponse(
512a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      this, global_request_id, cross_site_transferring_request.Pass(),
513a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      transfer_url_chain, referrer, page_transition,
514a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      should_replace_current_entry);
515a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
516a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
517cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RenderFrameHostImpl::SwapOut(RenderFrameProxyHost* proxy) {
518effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // TODO(creis): Move swapped out state to RFH.  Until then, only update it
519effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // when swapping out the main frame.
520effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!GetParent()) {
521effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // If this RenderViewHost is not in the default state, it must have already
522effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // gone through this, therefore just return.
523effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (render_view_host_->rvh_state_ != RenderViewHostImpl::STATE_DEFAULT)
524effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return;
525effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
526effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    render_view_host_->SetState(
527effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK);
528effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    render_view_host_->unload_event_monitor_timeout_->Start(
529effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        base::TimeDelta::FromMilliseconds(
530effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch            RenderViewHostImpl::kUnloadTimeoutMS));
5315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
532effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
533cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  set_render_frame_proxy_host(proxy);
534cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
535effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (render_view_host_->IsRenderViewLive())
536cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    Send(new FrameMsg_SwapOut(routing_id_, proxy->GetRoutingID()));
537effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
538effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!GetParent())
539effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    delegate_->SwappedOut(this);
540effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
541effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Allow the navigation to proceed.
542effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  frame_tree_node_->render_manager()->SwappedOut(this);
5435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnBeforeUnloadACK(
546a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool proceed,
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const base::TimeTicks& renderer_before_unload_start_time,
548a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const base::TimeTicks& renderer_before_unload_end_time) {
5490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // TODO(creis): Support properly beforeunload on subframes. For now just
5500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // pretend that the handler ran and allowed the navigation to proceed.
551a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (GetParent()) {
5520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    render_view_host_->is_waiting_for_beforeunload_ack_ = false;
5530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    frame_tree_node_->render_manager()->OnBeforeUnloadACK(
5540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        render_view_host_->unload_ack_is_for_cross_site_transition_, proceed,
5550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        renderer_before_unload_end_time);
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
557a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
558a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
559a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  render_view_host_->decrement_in_flight_event_count();
560a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  render_view_host_->StopHangMonitorTimeout();
561a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If this renderer navigated while the beforeunload request was in flight, we
562a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // may have cleared this state in OnNavigate, in which case we can ignore
563a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // this message.
564c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // However renderer might also be swapped out but we still want to proceed
565c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // with navigation, otherwise it would block future navigations. This can
566c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // happen when pending cross-site navigation is canceled by a second one just
567c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // before OnNavigate while current RVH is waiting for commit but second
568c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // navigation is started from the beginning.
569c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (!render_view_host_->is_waiting_for_beforeunload_ack_) {
570a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
571a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
572a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
573a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  render_view_host_->is_waiting_for_beforeunload_ack_ = false;
574a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
575a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TimeTicks before_unload_end_time;
576a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!send_before_unload_start_time_.is_null() &&
577a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !renderer_before_unload_start_time.is_null() &&
578a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !renderer_before_unload_end_time.is_null()) {
579a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // When passing TimeTicks across process boundaries, we need to compensate
580a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // for any skew between the processes. Here we are converting the
581a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // renderer's notion of before_unload_end_time to TimeTicks in the browser
582a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // process. See comments in inter_process_time_ticks_converter.h for more.
583a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    InterProcessTimeTicksConverter converter(
584a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        LocalTimeTicks::FromTimeTicks(send_before_unload_start_time_),
585a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        LocalTimeTicks::FromTimeTicks(base::TimeTicks::Now()),
586a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        RemoteTimeTicks::FromTimeTicks(renderer_before_unload_start_time),
587a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        RemoteTimeTicks::FromTimeTicks(renderer_before_unload_end_time));
588a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    LocalTimeTicks browser_before_unload_end_time =
589a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        converter.ToLocalTimeTicks(
590a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            RemoteTimeTicks::FromTimeTicks(renderer_before_unload_end_time));
591a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    before_unload_end_time = browser_before_unload_end_time.ToTimeTicks();
592a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
593a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_node_->render_manager()->OnBeforeUnloadACK(
594a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      render_view_host_->unload_ack_is_for_cross_site_transition_, proceed,
595a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      before_unload_end_time);
596a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
597a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If canceled, notify the delegate to cancel its pending navigation entry.
598a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!proceed)
599a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    render_view_host_->GetDelegate()->DidCancelLoading();
600a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
601a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
6025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnSwapOutACK() {
6035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  OnSwappedOut(false);
6045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
6055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnSwappedOut(bool timed_out) {
607effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // For now, we only need to update the RVH state machine for top-level swaps.
608effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Subframe swaps (in --site-per-process) can just continue via RFHM.
609effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!GetParent())
610effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    render_view_host_->OnSwappedOut(timed_out);
611effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  else
612effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    frame_tree_node_->render_manager()->SwappedOut(this);
6135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
6145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnContextMenu(const ContextMenuParams& params) {
6165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Validate the URLs in |params|.  If the renderer can't request the URLs
6175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // directly, don't show them in the context menu.
6185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ContextMenuParams validated_params(params);
6195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHost* process = GetProcess();
6205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // We don't validate |unfiltered_link_url| so that this field can be used
6225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // when users want to copy the original link URL.
6235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.link_url);
6245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.src_url);
6255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(false, &validated_params.page_url);
6265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.frame_url);
6275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  delegate_->ShowContextMenu(this, validated_params);
6295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
6305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
631a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnJavaScriptExecuteResponse(
632a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int id, const base::ListValue& result) {
633a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const base::Value* result_value;
634a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!result.Get(0, &result_value)) {
635a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Programming error or rogue renderer.
636a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    NOTREACHED() << "Got bad arguments for OnJavaScriptExecuteResponse";
637a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
638a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
639a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
640a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::map<int, JavaScriptResultCallback>::iterator it =
641a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      javascript_callbacks_.find(id);
642a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (it != javascript_callbacks_.end()) {
643a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    it->second.Run(result_value);
644a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    javascript_callbacks_.erase(it);
645a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else {
646a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    NOTREACHED() << "Received script response for unknown request";
647a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
648a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
649a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
650c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::OnRunJavaScriptMessage(
651c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& message,
652c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& default_prompt,
653c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const GURL& frame_url,
654c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    JavaScriptMessageType type,
655c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC::Message* reply_msg) {
656c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // While a JS message dialog is showing, tabs in the same process shouldn't
657c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // process input events.
658c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GetProcess()->SetIgnoreInputEvents(true);
659c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  render_view_host_->StopHangMonitorTimeout();
660c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  delegate_->RunJavaScriptMessage(this, message, default_prompt,
661c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                  frame_url, type, reply_msg);
662c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
663c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
664c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::OnRunBeforeUnloadConfirm(
665c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const GURL& frame_url,
666c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& message,
667c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    bool is_reload,
668c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC::Message* reply_msg) {
669c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // While a JS before unload dialog is showing, tabs in the same process
670c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // shouldn't process input events.
671c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GetProcess()->SetIgnoreInputEvents(true);
672c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  render_view_host_->StopHangMonitorTimeout();
673c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  delegate_->RunBeforeUnloadConfirm(this, message, is_reload, reply_msg);
674c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
675c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
6760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::OnRequestDesktopNotificationPermission(
6770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const GURL& source_origin, int callback_context) {
6780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::Closure done_callback = base::Bind(
6790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      &RenderFrameHostImpl::DesktopNotificationPermissionRequestDone,
6800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      weak_ptr_factory_.GetWeakPtr(), callback_context);
6810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  GetContentClient()->browser()->RequestDesktopNotificationPermission(
6820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      source_origin, this, done_callback);
6830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
6840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::OnShowDesktopNotification(
6860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int notification_id,
6870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const ShowDesktopNotificationHostMsgParams& params) {
6880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::Closure cancel_callback;
6890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  GetContentClient()->browser()->ShowDesktopNotification(
6900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      params, this,
6910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      new DesktopNotificationDelegateImpl(this, notification_id),
6920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      &cancel_callback);
6930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cancel_notification_callbacks_[notification_id] = cancel_callback;
6940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
6950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::OnCancelDesktopNotification(int notification_id) {
6970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!cancel_notification_callbacks_.count(notification_id)) {
6980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    NOTREACHED();
6990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
7000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
7010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cancel_notification_callbacks_[notification_id].Run();
7020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cancel_notification_callbacks_.erase(notification_id);
7030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
7040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
705f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void RenderFrameHostImpl::OnTextSurroundingSelectionResponse(
706f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const base::string16& content,
707f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_t start_offset,
708f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_t end_offset) {
709f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  render_view_host_->OnTextSurroundingSelectionResponse(
710f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      content, start_offset, end_offset);
711f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
712f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
7135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid RenderFrameHostImpl::OnDidAccessInitialDocument() {
7145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  delegate_->DidAccessInitialDocument();
7155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
7165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
717010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void RenderFrameHostImpl::OnDidDisownOpener() {
718010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // This message is only sent for top-level frames. TODO(avi): when frame tree
719010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // mirroring works correctly, add a check here to enforce it.
720010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  delegate_->DidDisownOpener(this);
721010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
722010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
723cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RenderFrameHostImpl::OnUpdateTitle(
724cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    int32 page_id,
725cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::string16& title,
726cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    blink::WebTextDirection title_direction) {
727cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This message is only sent for top-level frames. TODO(avi): when frame tree
728cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // mirroring works correctly, add a check here to enforce it.
729cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (title.length() > kMaxTitleChars) {
730cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    NOTREACHED() << "Renderer sent too many characters in title.";
731cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
732cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
733cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
734cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  delegate_->UpdateTitle(this, page_id, title,
735cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                         WebTextDirectionToChromeTextDirection(
736cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                             title_direction));
737cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
738cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
739cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RenderFrameHostImpl::OnUpdateEncoding(const std::string& encoding_name) {
740cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This message is only sent for top-level frames. TODO(avi): when frame tree
741cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // mirroring works correctly, add a check here to enforce it.
742cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  delegate_->UpdateEncoding(this, encoding_name);
743cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
744cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::SetPendingShutdown(const base::Closure& on_swap_out) {
7465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  render_view_host_->SetPendingShutdown(on_swap_out);
7475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool RenderFrameHostImpl::CanCommitURL(const GURL& url) {
7505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(creis): We should also check for WebUI pages here.  Also, when the
7515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // out-of-process iframes implementation is ready, we should check for
7525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // cross-site URLs that are not allowed to commit in this process.
7535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Give the client a chance to disallow URLs from committing.
7555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return GetContentClient()->browser()->CanCommitURL(GetProcess(), url);
7565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::Navigate(const FrameMsg_Navigate_Params& params) {
7595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TRACE_EVENT0("frame_host", "RenderFrameHostImpl::Navigate");
7605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Browser plugin guests are not allowed to navigate outside web-safe schemes,
7615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // so do not grant them the ability to request additional URLs.
762cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!GetProcess()->IsIsolatedGuest()) {
7635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ChildProcessSecurityPolicyImpl::GetInstance()->GrantRequestURL(
7645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        GetProcess()->GetID(), params.url);
765cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (params.url.SchemeIs(url::kDataScheme) &&
766cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        params.base_url_for_data_url.SchemeIs(url::kFileScheme)) {
7675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // If 'data:' is used, and we have a 'file:' base url, grant access to
7685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // local files.
7695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ChildProcessSecurityPolicyImpl::GetInstance()->GrantRequestURL(
7705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          GetProcess()->GetID(), params.base_url_for_data_url);
7715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
7725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Only send the message if we aren't suspended at the start of a cross-site
7755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // request.
7765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (render_view_host_->navigations_suspended_) {
7775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Shouldn't be possible to have a second navigation while suspended, since
7785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // navigations will only be suspended during a cross-site request.  If a
7795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // second navigation occurs, RenderFrameHostManager will cancel this pending
7805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // RFH and create a new pending RFH.
7815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK(!render_view_host_->suspended_nav_params_.get());
7825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    render_view_host_->suspended_nav_params_.reset(
7835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new FrameMsg_Navigate_Params(params));
7845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
7855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Get back to a clean state, in case we start a new navigation without
7865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // completing a RVH swap or unload handler.
7875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    render_view_host_->SetState(RenderViewHostImpl::STATE_DEFAULT);
7885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
789a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Send(new FrameMsg_Navigate(routing_id_, params));
7905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Force the throbber to start. We do this because Blink's "started
7935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // loading" message will be received asynchronously from the UI of the
7945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // browser. But we want to keep the throbber in sync with what's happening
7955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // in the UI. For example, we want to start throbbing immediately when the
7965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // user naivgates even if the renderer is delayed. There is also an issue
7975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // with the throbber starting because the WebUI (which controls whether the
7985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // favicon is displayed) happens synchronously. If the start loading
7995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // messages was asynchronous, then the default favicon would flash in.
8005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
8015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Blink doesn't send throb notifications for JavaScript URLs, so we
8025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // don't want to either.
803cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!params.url.SchemeIs(url::kJavaScriptScheme))
804effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    delegate_->DidStartLoading(this, true);
8055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
8065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::NavigateToURL(const GURL& url) {
8085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FrameMsg_Navigate_Params params;
8095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.page_id = -1;
8105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.pending_history_list_offset = -1;
8115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.current_history_list_offset = -1;
8125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.current_history_list_length = 0;
8135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.url = url;
8145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.transition = PAGE_TRANSITION_LINK;
8155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.navigation_type = FrameMsg_Navigate_Type::NORMAL;
8165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Navigate(params);
8175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
8185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
819c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::DispatchBeforeUnload(bool for_cross_site_transition) {
820c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // TODO(creis): Support subframes.
8210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!render_view_host_->IsRenderViewLive() || GetParent()) {
822c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // We don't have a live renderer, so just skip running beforeunload.
823c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->is_waiting_for_beforeunload_ack_ = true;
824c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->unload_ack_is_for_cross_site_transition_ =
825c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        for_cross_site_transition;
826c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    base::TimeTicks now = base::TimeTicks::Now();
827c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    OnBeforeUnloadACK(true, now, now);
828c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    return;
829c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
830c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
831c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // This may be called more than once (if the user clicks the tab close button
832c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // several times, or if she clicks the tab close button then the browser close
833c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // button), and we only send the message once.
834c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (render_view_host_->is_waiting_for_beforeunload_ack_) {
835c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Some of our close messages could be for the tab, others for cross-site
836c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // transitions. We always want to think it's for closing the tab if any
837c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // of the messages were, since otherwise it might be impossible to close
838c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // (if there was a cross-site "close" request pending when the user clicked
839c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // the close button). We want to keep the "for cross site" flag only if
840c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // both the old and the new ones are also for cross site.
841c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->unload_ack_is_for_cross_site_transition_ =
842c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        render_view_host_->unload_ack_is_for_cross_site_transition_ &&
843c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        for_cross_site_transition;
844c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  } else {
845c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Start the hang monitor in case the renderer hangs in the beforeunload
846c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // handler.
847c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->is_waiting_for_beforeunload_ack_ = true;
848c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->unload_ack_is_for_cross_site_transition_ =
849c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        for_cross_site_transition;
850c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Increment the in-flight event count, to ensure that input events won't
851c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // cancel the timeout timer.
852c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->increment_in_flight_event_count();
853c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->StartHangMonitorTimeout(
854c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        TimeDelta::FromMilliseconds(RenderViewHostImpl::kUnloadTimeoutMS));
855c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    send_before_unload_start_time_ = base::TimeTicks::Now();
856c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    Send(new FrameMsg_BeforeUnload(routing_id_));
857c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
858effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
859effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
860effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid RenderFrameHostImpl::ExtendSelectionAndDelete(size_t before,
861effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                   size_t after) {
862effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  Send(new FrameMsg_ExtendSelectionAndDelete(routing_id_, before, after));
863effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
864effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
865c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::JavaScriptDialogClosed(
866c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC::Message* reply_msg,
867c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    bool success,
868c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& user_input,
869c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    bool dialog_was_suppressed) {
870c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GetProcess()->SetIgnoreInputEvents(false);
871c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool is_waiting = render_view_host_->is_waiting_for_beforeunload_ack() ||
872c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                    render_view_host_->IsWaitingForUnloadACK();
873c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
874c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // If we are executing as part of (before)unload event handling, we don't
875c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // want to use the regular hung_renderer_delay_ms_ if the user has agreed to
876c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // leave the current page. In this case, use the regular timeout value used
877c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // during the (before)unload handling.
878c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (is_waiting) {
879c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(
880c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        success ? RenderViewHostImpl::kUnloadTimeoutMS
881c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                : render_view_host_->hung_renderer_delay_ms_));
882c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
883c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
884c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  FrameHostMsg_RunJavaScriptMessage::WriteReplyParams(reply_msg,
885c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                      success, user_input);
886c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  Send(reply_msg);
887c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
888c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // If we are waiting for an unload or beforeunload ack and the user has
889c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // suppressed messages, kill the tab immediately; a page that's spamming
890c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // alerts in onbeforeunload is presumably malicious, so there's no point in
891c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // continuing to run its script and dragging out the process.
892c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // This must be done after sending the reply since RenderView can't close
893c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // correctly while waiting for a response.
894c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (is_waiting && dialog_was_suppressed)
895c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->delegate_->RendererUnresponsive(
896c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        render_view_host_,
897c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        render_view_host_->is_waiting_for_beforeunload_ack(),
898c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        render_view_host_->IsWaitingForUnloadACK());
899c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
900c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
9010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::NotificationClosed(int notification_id) {
9020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cancel_notification_callbacks_.erase(notification_id);
9030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
9040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
9050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::DesktopNotificationPermissionRequestDone(
9060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int callback_context) {
9070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  Send(new DesktopNotificationMsg_PermissionRequestDone(
9080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      routing_id_, callback_context));
9090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
9100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
911868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace content
912