render_frame_host_impl.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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)
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (cross_process_frame_connector_ &&
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      cross_process_frame_connector_->OnMessageReceived(msg))
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool handled = true;
287cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IPC_BEGIN_MESSAGE_MAP(RenderFrameHostImpl, msg)
288c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC_MESSAGE_HANDLER(FrameHostMsg_AddMessageToConsole, OnAddMessageToConsole)
2894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_Detach, OnDetach)
290a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_FrameFocused, OnFrameFocused)
2918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidStartProvisionalLoadForFrame,
2928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                        OnDidStartProvisionalLoadForFrame)
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidFailProvisionalLoadWithError,
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        OnDidFailProvisionalLoadWithError)
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidRedirectProvisionalLoad,
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        OnDidRedirectProvisionalLoad)
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidFailLoadWithError,
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        OnDidFailLoadWithError)
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER_GENERIC(FrameHostMsg_DidCommitProvisionalLoad,
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                OnNavigate(msg))
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_OpenURL, OnOpenURL)
302010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DocumentOnLoadCompleted,
303010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                        OnDocumentOnLoadCompleted)
304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_BeforeUnload_ACK, OnBeforeUnloadACK)
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_SwapOut_ACK, OnSwapOutACK)
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_ContextMenu, OnContextMenu)
307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_JavaScriptExecuteResponse,
308a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                        OnJavaScriptExecuteResponse)
309c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC_MESSAGE_HANDLER_DELAY_REPLY(FrameHostMsg_RunJavaScriptMessage,
310c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                    OnRunJavaScriptMessage)
311c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC_MESSAGE_HANDLER_DELAY_REPLY(FrameHostMsg_RunBeforeUnloadConfirm,
312c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                    OnRunBeforeUnloadConfirm)
3135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    IPC_MESSAGE_HANDLER(FrameHostMsg_DidAccessInitialDocument,
3145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                        OnDidAccessInitialDocument)
315010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisownOpener, OnDidDisownOpener)
316cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateTitle, OnUpdateTitle)
317cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateEncoding, OnUpdateEncoding)
3180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    IPC_MESSAGE_HANDLER(DesktopNotificationHostMsg_RequestPermission,
3190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                        OnRequestDesktopNotificationPermission)
3200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    IPC_MESSAGE_HANDLER(DesktopNotificationHostMsg_Show,
3210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                        OnShowDesktopNotification)
3220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    IPC_MESSAGE_HANDLER(DesktopNotificationHostMsg_Cancel,
3230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                        OnCancelDesktopNotification)
324cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IPC_END_MESSAGE_MAP()
325a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
3264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return handled;
327424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
328424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
329424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void RenderFrameHostImpl::Init() {
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetProcess()->ResumeRequestsForView(routing_id_);
331868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
332868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
333c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::OnAddMessageToConsole(
334c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    int32 level,
335c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& message,
336c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    int32 line_no,
337c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& source_id) {
338c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (delegate_->AddMessageToConsole(level, message, line_no, source_id))
339c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    return;
340c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
341c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Pass through log level only on WebUI pages to limit console spew.
342c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  int32 resolved_level =
343c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      HasWebUIScheme(delegate_->GetMainFrameLastCommittedURL()) ? level : 0;
344c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
345c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (resolved_level >= ::logging::GetMinLogLevel()) {
346c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    logging::LogMessage("CONSOLE", line_no, resolved_level).stream() << "\"" <<
347c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        message << "\", source: " << source_id << " (" << line_no << ")";
348c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
349c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
350c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
351a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnCreateChildFrame(int new_routing_id,
3524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                             const std::string& frame_name) {
353a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RenderFrameHostImpl* new_frame = frame_tree_->AddFrame(
354a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      frame_tree_node_, new_routing_id, frame_name);
355a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (delegate_)
356a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    delegate_->RenderFrameCreated(new_frame);
3574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnDetach() {
360a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_->RemoveFrame(frame_tree_node_);
361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnFrameFocused() {
364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_->SetFocusedFrame(frame_tree_node_);
365a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
367a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnOpenURL(
368a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const FrameHostMsg_OpenURL_Params& params) {
369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL validated_url(params.url);
370a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GetProcess()->FilterURL(false, &validated_url);
371a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
372a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_node_->navigator()->RequestOpenURL(
373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      this, validated_url, params.referrer, params.disposition,
374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      params.should_replace_current_entry, params.user_gesture);
3754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
3764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
377010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void RenderFrameHostImpl::OnDocumentOnLoadCompleted() {
378010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // This message is only sent for top-level frames. TODO(avi): when frame tree
379010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // mirroring works correctly, add a check here to enforce it.
380010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  delegate_->DocumentOnLoadCompleted(this);
381010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
382010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
3838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void RenderFrameHostImpl::OnDidStartProvisionalLoadForFrame(
384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int parent_routing_id,
3858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const GURL& url) {
386a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  frame_tree_node_->navigator()->DidStartProvisionalLoad(
387effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      this, parent_routing_id, url);
3888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnDidFailProvisionalLoadWithError(
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const FrameHostMsg_DidFailProvisionalLoadWithError_Params& params) {
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_node_->navigator()->DidFailProvisionalLoadWithError(this, params);
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnDidFailLoadWithError(
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GURL& url,
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int error_code,
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::string16& error_description) {
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL validated_url(url);
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GetProcess()->FilterURL(false, &validated_url);
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_node_->navigator()->DidFailLoadWithError(
403effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      this, validated_url, error_code, error_description);
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnDidRedirectProvisionalLoad(
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int32 page_id,
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GURL& source_url,
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GURL& target_url) {
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_node_->navigator()->DidRedirectProvisionalLoad(
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this, page_id, source_url, target_url);
4125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Called when the renderer navigates.  For every frame loaded, we'll get this
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// notification containing parameters identifying the navigation.
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Subframes are identified by the page transition type.  For subframes loaded
4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// as part of a wider page load, the page_id will be the same as for the top
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// level frame.  If the user explicitly requests a subframe navigation, we will
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// get a new page_id because we need to create a new navigation entry for that
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// action.
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnNavigate(const IPC::Message& msg) {
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Read the parameters out of the IPC message directly to avoid making another
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // copy when we filter the URLs.
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PickleIterator iter(msg);
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FrameHostMsg_DidCommitProvisionalLoad_Params validated_params;
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!IPC::ParamTraits<FrameHostMsg_DidCommitProvisionalLoad_Params>::
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Read(&msg, &iter, &validated_params))
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If we're waiting for a cross-site beforeunload ack from this renderer and
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // we receive a Navigate message from the main frame, then the renderer was
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // navigating already and sent it before hearing the ViewMsg_Stop message.
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // We do not want to cancel the pending navigation in this case, since the
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // old page will soon be stopped.  Instead, treat this as a beforeunload ack
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // to allow the pending navigation to continue.
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (render_view_host_->is_waiting_for_beforeunload_ack_ &&
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      render_view_host_->unload_ack_is_for_cross_site_transition_ &&
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PageTransitionIsMainFrame(validated_params.transition)) {
440a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    OnBeforeUnloadACK(true, send_before_unload_start_time_,
441a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                      base::TimeTicks::Now());
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If we're waiting for an unload ack from this renderer and we receive a
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Navigate message, then the renderer was navigating before it received the
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // unload request.  It will either respond to the unload request soon or our
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // timer will expire.  Either way, we should ignore this message, because we
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // have already committed to closing this renderer.
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (render_view_host_->IsWaitingForUnloadACK())
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHost* process = GetProcess();
4545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Attempts to commit certain off-limits URL should be caught more strictly
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // than our FilterURL checks below.  If a renderer violates this policy, it
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // should be killed.
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!CanCommitURL(validated_params.url)) {
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    VLOG(1) << "Blocked URL " << validated_params.url.spec();
4605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    validated_params.url = GURL(kAboutBlankURL);
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RecordAction(base::UserMetricsAction("CanCommitURL_BlockedAndKilled"));
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Kills the process.
4635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    process->ReceivedBadMessage();
4645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Without this check, an evil renderer can trick the browser into creating
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // a navigation entry for a banned URL.  If the user clicks the back button
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // followed by the forward button (or clicks reload, or round-trips through
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // session restore, etc), we'll think that the browser commanded the
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // renderer to load the URL and grant the renderer the privileges to request
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the URL.  To prevent this attack, we block the renderer from inserting
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // banned URLs into the navigation controller in the first place.
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(false, &validated_params.url);
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.referrer.url);
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (std::vector<GURL>::iterator it(validated_params.redirects.begin());
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      it != validated_params.redirects.end(); ++it) {
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    process->FilterURL(false, &(*it));
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.searchable_form_url);
4805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Without this check, the renderer can trick the browser into using
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // filenames it can't access in a future session restore.
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!render_view_host_->CanAccessFilesOfPageState(
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          validated_params.page_state)) {
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetProcess()->ReceivedBadMessage();
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_tree_node()->navigator()->DidNavigate(this, validated_params);
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
492cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)RenderWidgetHostImpl* RenderFrameHostImpl::GetRenderWidgetHost() {
493cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return static_cast<RenderWidgetHostImpl*>(render_view_host_);
494cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
495cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
496a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)int RenderFrameHostImpl::GetEnabledBindings() {
497a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return render_view_host_->GetEnabledBindings();
498a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
499a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
500a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnCrossSiteResponse(
501a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const GlobalRequestID& global_request_id,
502a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
503a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::vector<GURL>& transfer_url_chain,
504a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const Referrer& referrer,
505a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    PageTransition page_transition,
506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool should_replace_current_entry) {
507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_node_->render_manager()->OnCrossSiteResponse(
508a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      this, global_request_id, cross_site_transferring_request.Pass(),
509a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      transfer_url_chain, referrer, page_transition,
510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      should_replace_current_entry);
511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
512a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
513cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RenderFrameHostImpl::SwapOut(RenderFrameProxyHost* proxy) {
514effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // TODO(creis): Move swapped out state to RFH.  Until then, only update it
515effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // when swapping out the main frame.
516effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!GetParent()) {
517effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // If this RenderViewHost is not in the default state, it must have already
518effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // gone through this, therefore just return.
519effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (render_view_host_->rvh_state_ != RenderViewHostImpl::STATE_DEFAULT)
520effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return;
521effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
522effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    render_view_host_->SetState(
523effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK);
524effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    render_view_host_->unload_event_monitor_timeout_->Start(
525effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        base::TimeDelta::FromMilliseconds(
526effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch            RenderViewHostImpl::kUnloadTimeoutMS));
5275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
528effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
529cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  set_render_frame_proxy_host(proxy);
530cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
531effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (render_view_host_->IsRenderViewLive())
532cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    Send(new FrameMsg_SwapOut(routing_id_, proxy->GetRoutingID()));
533effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
534effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!GetParent())
535effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    delegate_->SwappedOut(this);
536effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
537effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Allow the navigation to proceed.
538effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  frame_tree_node_->render_manager()->SwappedOut(this);
5395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
541a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnBeforeUnloadACK(
542a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool proceed,
543a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const base::TimeTicks& renderer_before_unload_start_time,
544a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const base::TimeTicks& renderer_before_unload_end_time) {
5450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // TODO(creis): Support properly beforeunload on subframes. For now just
5460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // pretend that the handler ran and allowed the navigation to proceed.
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (GetParent()) {
5480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    render_view_host_->is_waiting_for_beforeunload_ack_ = false;
5490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    frame_tree_node_->render_manager()->OnBeforeUnloadACK(
5500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        render_view_host_->unload_ack_is_for_cross_site_transition_, proceed,
5510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        renderer_before_unload_end_time);
552a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
553a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
554a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
555a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  render_view_host_->decrement_in_flight_event_count();
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  render_view_host_->StopHangMonitorTimeout();
557a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If this renderer navigated while the beforeunload request was in flight, we
558a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // may have cleared this state in OnNavigate, in which case we can ignore
559a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // this message.
560c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // However renderer might also be swapped out but we still want to proceed
561c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // with navigation, otherwise it would block future navigations. This can
562c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // happen when pending cross-site navigation is canceled by a second one just
563c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // before OnNavigate while current RVH is waiting for commit but second
564c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // navigation is started from the beginning.
565c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (!render_view_host_->is_waiting_for_beforeunload_ack_) {
566a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
567a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
568a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
569a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  render_view_host_->is_waiting_for_beforeunload_ack_ = false;
570a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
571a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TimeTicks before_unload_end_time;
572a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!send_before_unload_start_time_.is_null() &&
573a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !renderer_before_unload_start_time.is_null() &&
574a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      !renderer_before_unload_end_time.is_null()) {
575a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // When passing TimeTicks across process boundaries, we need to compensate
576a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // for any skew between the processes. Here we are converting the
577a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // renderer's notion of before_unload_end_time to TimeTicks in the browser
578a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // process. See comments in inter_process_time_ticks_converter.h for more.
579a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    InterProcessTimeTicksConverter converter(
580a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        LocalTimeTicks::FromTimeTicks(send_before_unload_start_time_),
581a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        LocalTimeTicks::FromTimeTicks(base::TimeTicks::Now()),
582a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        RemoteTimeTicks::FromTimeTicks(renderer_before_unload_start_time),
583a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        RemoteTimeTicks::FromTimeTicks(renderer_before_unload_end_time));
584a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    LocalTimeTicks browser_before_unload_end_time =
585a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        converter.ToLocalTimeTicks(
586a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            RemoteTimeTicks::FromTimeTicks(renderer_before_unload_end_time));
587a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    before_unload_end_time = browser_before_unload_end_time.ToTimeTicks();
588a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
589a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  frame_tree_node_->render_manager()->OnBeforeUnloadACK(
590a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      render_view_host_->unload_ack_is_for_cross_site_transition_, proceed,
591a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      before_unload_end_time);
592a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
593a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If canceled, notify the delegate to cancel its pending navigation entry.
594a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!proceed)
595a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    render_view_host_->GetDelegate()->DidCancelLoading();
596a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
597a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnSwapOutACK() {
5995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  OnSwappedOut(false);
6005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
6015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnSwappedOut(bool timed_out) {
603effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // For now, we only need to update the RVH state machine for top-level swaps.
604effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Subframe swaps (in --site-per-process) can just continue via RFHM.
605effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!GetParent())
606effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    render_view_host_->OnSwappedOut(timed_out);
607effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  else
608effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    frame_tree_node_->render_manager()->SwappedOut(this);
6095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
6105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::OnContextMenu(const ContextMenuParams& params) {
6125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Validate the URLs in |params|.  If the renderer can't request the URLs
6135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // directly, don't show them in the context menu.
6145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ContextMenuParams validated_params(params);
6155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RenderProcessHost* process = GetProcess();
6165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // We don't validate |unfiltered_link_url| so that this field can be used
6185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // when users want to copy the original link URL.
6195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.link_url);
6205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.src_url);
6215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(false, &validated_params.page_url);
6225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  process->FilterURL(true, &validated_params.frame_url);
6235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  delegate_->ShowContextMenu(this, validated_params);
6255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
6265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
627a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void RenderFrameHostImpl::OnJavaScriptExecuteResponse(
628a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int id, const base::ListValue& result) {
629a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const base::Value* result_value;
630a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!result.Get(0, &result_value)) {
631a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Programming error or rogue renderer.
632a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    NOTREACHED() << "Got bad arguments for OnJavaScriptExecuteResponse";
633a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
634a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
635a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
636a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::map<int, JavaScriptResultCallback>::iterator it =
637a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      javascript_callbacks_.find(id);
638a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (it != javascript_callbacks_.end()) {
639a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    it->second.Run(result_value);
640a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    javascript_callbacks_.erase(it);
641a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else {
642a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    NOTREACHED() << "Received script response for unknown request";
643a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
644a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
645a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
646c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::OnRunJavaScriptMessage(
647c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& message,
648c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& default_prompt,
649c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const GURL& frame_url,
650c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    JavaScriptMessageType type,
651c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC::Message* reply_msg) {
652c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // While a JS message dialog is showing, tabs in the same process shouldn't
653c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // process input events.
654c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GetProcess()->SetIgnoreInputEvents(true);
655c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  render_view_host_->StopHangMonitorTimeout();
656c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  delegate_->RunJavaScriptMessage(this, message, default_prompt,
657c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                  frame_url, type, reply_msg);
658c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
659c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
660c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::OnRunBeforeUnloadConfirm(
661c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const GURL& frame_url,
662c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& message,
663c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    bool is_reload,
664c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC::Message* reply_msg) {
665c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // While a JS before unload dialog is showing, tabs in the same process
666c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // shouldn't process input events.
667c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GetProcess()->SetIgnoreInputEvents(true);
668c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  render_view_host_->StopHangMonitorTimeout();
669c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  delegate_->RunBeforeUnloadConfirm(this, message, is_reload, reply_msg);
670c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
671c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
6720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::OnRequestDesktopNotificationPermission(
6730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const GURL& source_origin, int callback_context) {
6740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::Closure done_callback = base::Bind(
6750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      &RenderFrameHostImpl::DesktopNotificationPermissionRequestDone,
6760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      weak_ptr_factory_.GetWeakPtr(), callback_context);
6770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  GetContentClient()->browser()->RequestDesktopNotificationPermission(
6780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      source_origin, this, done_callback);
6790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
6800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::OnShowDesktopNotification(
6820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int notification_id,
6830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const ShowDesktopNotificationHostMsgParams& params) {
6840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  base::Closure cancel_callback;
6850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  GetContentClient()->browser()->ShowDesktopNotification(
6860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      params, this,
6870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      new DesktopNotificationDelegateImpl(this, notification_id),
6880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      &cancel_callback);
6890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cancel_notification_callbacks_[notification_id] = cancel_callback;
6900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
6910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
6920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::OnCancelDesktopNotification(int notification_id) {
6930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!cancel_notification_callbacks_.count(notification_id)) {
6940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    NOTREACHED();
6950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
6960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
6970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cancel_notification_callbacks_[notification_id].Run();
6980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cancel_notification_callbacks_.erase(notification_id);
6990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
7000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
7015c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid RenderFrameHostImpl::OnDidAccessInitialDocument() {
7025c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  delegate_->DidAccessInitialDocument();
7035c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
7045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
705010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void RenderFrameHostImpl::OnDidDisownOpener() {
706010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // This message is only sent for top-level frames. TODO(avi): when frame tree
707010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // mirroring works correctly, add a check here to enforce it.
708010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  delegate_->DidDisownOpener(this);
709010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
710010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
711cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RenderFrameHostImpl::OnUpdateTitle(
712cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    int32 page_id,
713cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::string16& title,
714cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    blink::WebTextDirection title_direction) {
715cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This message is only sent for top-level frames. TODO(avi): when frame tree
716cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // mirroring works correctly, add a check here to enforce it.
717cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (title.length() > kMaxTitleChars) {
718cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    NOTREACHED() << "Renderer sent too many characters in title.";
719cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
720cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
721cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
722cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  delegate_->UpdateTitle(this, page_id, title,
723cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                         WebTextDirectionToChromeTextDirection(
724cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                             title_direction));
725cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
726cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
727cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RenderFrameHostImpl::OnUpdateEncoding(const std::string& encoding_name) {
728cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This message is only sent for top-level frames. TODO(avi): when frame tree
729cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // mirroring works correctly, add a check here to enforce it.
730cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  delegate_->UpdateEncoding(this, encoding_name);
731cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
732cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::SetPendingShutdown(const base::Closure& on_swap_out) {
7345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  render_view_host_->SetPendingShutdown(on_swap_out);
7355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool RenderFrameHostImpl::CanCommitURL(const GURL& url) {
7385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(creis): We should also check for WebUI pages here.  Also, when the
7395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // out-of-process iframes implementation is ready, we should check for
7405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // cross-site URLs that are not allowed to commit in this process.
7415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Give the client a chance to disallow URLs from committing.
7435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return GetContentClient()->browser()->CanCommitURL(GetProcess(), url);
7445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::Navigate(const FrameMsg_Navigate_Params& params) {
7475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TRACE_EVENT0("frame_host", "RenderFrameHostImpl::Navigate");
7485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Browser plugin guests are not allowed to navigate outside web-safe schemes,
7495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // so do not grant them the ability to request additional URLs.
750cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!GetProcess()->IsIsolatedGuest()) {
7515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ChildProcessSecurityPolicyImpl::GetInstance()->GrantRequestURL(
7525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        GetProcess()->GetID(), params.url);
753cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (params.url.SchemeIs(url::kDataScheme) &&
754cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        params.base_url_for_data_url.SchemeIs(url::kFileScheme)) {
7555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // If 'data:' is used, and we have a 'file:' base url, grant access to
7565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // local files.
7575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ChildProcessSecurityPolicyImpl::GetInstance()->GrantRequestURL(
7585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          GetProcess()->GetID(), params.base_url_for_data_url);
7595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
7605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Only send the message if we aren't suspended at the start of a cross-site
7635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // request.
7645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (render_view_host_->navigations_suspended_) {
7655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Shouldn't be possible to have a second navigation while suspended, since
7665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // navigations will only be suspended during a cross-site request.  If a
7675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // second navigation occurs, RenderFrameHostManager will cancel this pending
7685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // RFH and create a new pending RFH.
7695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK(!render_view_host_->suspended_nav_params_.get());
7705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    render_view_host_->suspended_nav_params_.reset(
7715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new FrameMsg_Navigate_Params(params));
7725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
7735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Get back to a clean state, in case we start a new navigation without
7745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // completing a RVH swap or unload handler.
7755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    render_view_host_->SetState(RenderViewHostImpl::STATE_DEFAULT);
7765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
777a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Send(new FrameMsg_Navigate(routing_id_, params));
7785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Force the throbber to start. We do this because Blink's "started
7815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // loading" message will be received asynchronously from the UI of the
7825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // browser. But we want to keep the throbber in sync with what's happening
7835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // in the UI. For example, we want to start throbbing immediately when the
7845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // user naivgates even if the renderer is delayed. There is also an issue
7855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // with the throbber starting because the WebUI (which controls whether the
7865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // favicon is displayed) happens synchronously. If the start loading
7875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // messages was asynchronous, then the default favicon would flash in.
7885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
7895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Blink doesn't send throb notifications for JavaScript URLs, so we
7905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // don't want to either.
791cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!params.url.SchemeIs(url::kJavaScriptScheme))
792effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    delegate_->DidStartLoading(this, true);
7935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void RenderFrameHostImpl::NavigateToURL(const GURL& url) {
7965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FrameMsg_Navigate_Params params;
7975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.page_id = -1;
7985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.pending_history_list_offset = -1;
7995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.current_history_list_offset = -1;
8005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.current_history_list_length = 0;
8015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.url = url;
8025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.transition = PAGE_TRANSITION_LINK;
8035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.navigation_type = FrameMsg_Navigate_Type::NORMAL;
8045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Navigate(params);
8055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
8065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
807c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::DispatchBeforeUnload(bool for_cross_site_transition) {
808c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // TODO(creis): Support subframes.
8090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!render_view_host_->IsRenderViewLive() || GetParent()) {
810c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // We don't have a live renderer, so just skip running beforeunload.
811c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->is_waiting_for_beforeunload_ack_ = true;
812c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->unload_ack_is_for_cross_site_transition_ =
813c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        for_cross_site_transition;
814c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    base::TimeTicks now = base::TimeTicks::Now();
815c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    OnBeforeUnloadACK(true, now, now);
816c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    return;
817c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
818c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
819c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // This may be called more than once (if the user clicks the tab close button
820c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // several times, or if she clicks the tab close button then the browser close
821c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // button), and we only send the message once.
822c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (render_view_host_->is_waiting_for_beforeunload_ack_) {
823c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Some of our close messages could be for the tab, others for cross-site
824c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // transitions. We always want to think it's for closing the tab if any
825c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // of the messages were, since otherwise it might be impossible to close
826c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // (if there was a cross-site "close" request pending when the user clicked
827c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // the close button). We want to keep the "for cross site" flag only if
828c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // both the old and the new ones are also for cross site.
829c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->unload_ack_is_for_cross_site_transition_ =
830c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        render_view_host_->unload_ack_is_for_cross_site_transition_ &&
831c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        for_cross_site_transition;
832c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  } else {
833c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Start the hang monitor in case the renderer hangs in the beforeunload
834c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // handler.
835c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->is_waiting_for_beforeunload_ack_ = true;
836c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->unload_ack_is_for_cross_site_transition_ =
837c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        for_cross_site_transition;
838c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Increment the in-flight event count, to ensure that input events won't
839c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // cancel the timeout timer.
840c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->increment_in_flight_event_count();
841c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->StartHangMonitorTimeout(
842c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        TimeDelta::FromMilliseconds(RenderViewHostImpl::kUnloadTimeoutMS));
843c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    send_before_unload_start_time_ = base::TimeTicks::Now();
844c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    Send(new FrameMsg_BeforeUnload(routing_id_));
845c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
846effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
847effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
848effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid RenderFrameHostImpl::ExtendSelectionAndDelete(size_t before,
849effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                   size_t after) {
850effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  Send(new FrameMsg_ExtendSelectionAndDelete(routing_id_, before, after));
851effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
852effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
853c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid RenderFrameHostImpl::JavaScriptDialogClosed(
854c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC::Message* reply_msg,
855c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    bool success,
856c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    const base::string16& user_input,
857c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    bool dialog_was_suppressed) {
858c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  GetProcess()->SetIgnoreInputEvents(false);
859c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool is_waiting = render_view_host_->is_waiting_for_beforeunload_ack() ||
860c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                    render_view_host_->IsWaitingForUnloadACK();
861c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
862c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // If we are executing as part of (before)unload event handling, we don't
863c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // want to use the regular hung_renderer_delay_ms_ if the user has agreed to
864c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // leave the current page. In this case, use the regular timeout value used
865c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // during the (before)unload handling.
866c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (is_waiting) {
867c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(
868c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        success ? RenderViewHostImpl::kUnloadTimeoutMS
869c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                : render_view_host_->hung_renderer_delay_ms_));
870c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
871c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
872c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  FrameHostMsg_RunJavaScriptMessage::WriteReplyParams(reply_msg,
873c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                      success, user_input);
874c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  Send(reply_msg);
875c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
876c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // If we are waiting for an unload or beforeunload ack and the user has
877c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // suppressed messages, kill the tab immediately; a page that's spamming
878c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // alerts in onbeforeunload is presumably malicious, so there's no point in
879c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // continuing to run its script and dragging out the process.
880c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // This must be done after sending the reply since RenderView can't close
881c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // correctly while waiting for a response.
882c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  if (is_waiting && dialog_was_suppressed)
883c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    render_view_host_->delegate_->RendererUnresponsive(
884c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        render_view_host_,
885c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        render_view_host_->is_waiting_for_beforeunload_ack(),
886c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        render_view_host_->IsWaitingForUnloadACK());
887c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
888c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
8890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::NotificationClosed(int notification_id) {
8900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  cancel_notification_callbacks_.erase(notification_id);
8910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
8920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
8930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid RenderFrameHostImpl::DesktopNotificationPermissionRequestDone(
8940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int callback_context) {
8950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  Send(new DesktopNotificationMsg_PermissionRequestDone(
8960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      routing_id_, callback_context));
8970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
8980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
899868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace content
900