Add ICU message format support
[chromium-blink-merge.git] / content / browser / frame_host / interstitial_page_impl.cc
blobd769c4d9bea19a225ec13cbf47e5223283423462
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/frame_host/interstitial_page_impl.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/threading/thread.h"
17 #include "content/browser/dom_storage/dom_storage_context_wrapper.h"
18 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
19 #include "content/browser/frame_host/interstitial_page_navigator_impl.h"
20 #include "content/browser/frame_host/navigation_controller_impl.h"
21 #include "content/browser/frame_host/navigation_entry_impl.h"
22 #include "content/browser/loader/resource_dispatcher_host_impl.h"
23 #include "content/browser/renderer_host/render_process_host_impl.h"
24 #include "content/browser/renderer_host/render_view_host_delegate_view.h"
25 #include "content/browser/renderer_host/render_view_host_factory.h"
26 #include "content/browser/renderer_host/render_view_host_impl.h"
27 #include "content/browser/renderer_host/render_widget_host_view_base.h"
28 #include "content/browser/site_instance_impl.h"
29 #include "content/browser/web_contents/web_contents_impl.h"
30 #include "content/browser/web_contents/web_contents_view.h"
31 #include "content/common/frame_messages.h"
32 #include "content/common/input_messages.h"
33 #include "content/common/view_messages.h"
34 #include "content/public/browser/browser_context.h"
35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/content_browser_client.h"
37 #include "content/public/browser/dom_operation_notification_details.h"
38 #include "content/public/browser/interstitial_page_delegate.h"
39 #include "content/public/browser/invalidate_type.h"
40 #include "content/public/browser/notification_service.h"
41 #include "content/public/browser/notification_source.h"
42 #include "content/public/browser/storage_partition.h"
43 #include "content/public/browser/user_metrics.h"
44 #include "content/public/browser/web_contents_delegate.h"
45 #include "content/public/common/bindings_policy.h"
46 #include "net/base/escape.h"
47 #include "net/url_request/url_request_context_getter.h"
48 #include "ui/base/page_transition_types.h"
50 using blink::WebDragOperation;
51 using blink::WebDragOperationsMask;
53 namespace content {
54 namespace {
56 void ResourceRequestHelper(ResourceDispatcherHostImpl* rdh,
57 int process_id,
58 int render_view_host_id,
59 ResourceRequestAction action) {
60 switch (action) {
61 case BLOCK:
62 rdh->BlockRequestsForRoute(process_id, render_view_host_id);
63 break;
64 case RESUME:
65 rdh->ResumeBlockedRequestsForRoute(process_id, render_view_host_id);
66 break;
67 case CANCEL:
68 rdh->CancelBlockedRequestsForRoute(process_id, render_view_host_id);
69 break;
70 default:
71 NOTREACHED();
75 } // namespace
77 class InterstitialPageImpl::InterstitialPageRVHDelegateView
78 : public RenderViewHostDelegateView {
79 public:
80 explicit InterstitialPageRVHDelegateView(InterstitialPageImpl* page);
82 // RenderViewHostDelegateView implementation:
83 #if defined(OS_MACOSX) || defined(OS_ANDROID)
84 void ShowPopupMenu(RenderFrameHost* render_frame_host,
85 const gfx::Rect& bounds,
86 int item_height,
87 double item_font_size,
88 int selected_item,
89 const std::vector<MenuItem>& items,
90 bool right_aligned,
91 bool allow_multiple_selection) override;
92 void HidePopupMenu() override;
93 #endif
94 void StartDragging(const DropData& drop_data,
95 WebDragOperationsMask operations_allowed,
96 const gfx::ImageSkia& image,
97 const gfx::Vector2d& image_offset,
98 const DragEventSourceInfo& event_info) override;
99 void UpdateDragCursor(WebDragOperation operation) override;
100 void GotFocus() override;
101 void TakeFocus(bool reverse) override;
102 virtual void OnFindReply(int request_id,
103 int number_of_matches,
104 const gfx::Rect& selection_rect,
105 int active_match_ordinal,
106 bool final_update);
108 private:
109 InterstitialPageImpl* interstitial_page_;
111 DISALLOW_COPY_AND_ASSIGN(InterstitialPageRVHDelegateView);
115 // We keep a map of the various blocking pages shown as the UI tests need to
116 // be able to retrieve them.
117 typedef std::map<WebContents*, InterstitialPageImpl*> InterstitialPageMap;
118 static InterstitialPageMap* g_web_contents_to_interstitial_page;
120 // Initializes g_web_contents_to_interstitial_page in a thread-safe manner.
121 // Should be called before accessing g_web_contents_to_interstitial_page.
122 static void InitInterstitialPageMap() {
123 if (!g_web_contents_to_interstitial_page)
124 g_web_contents_to_interstitial_page = new InterstitialPageMap;
127 InterstitialPage* InterstitialPage::Create(WebContents* web_contents,
128 bool new_navigation,
129 const GURL& url,
130 InterstitialPageDelegate* delegate) {
131 return new InterstitialPageImpl(
132 web_contents,
133 static_cast<RenderWidgetHostDelegate*>(
134 static_cast<WebContentsImpl*>(web_contents)),
135 new_navigation, url, delegate);
138 InterstitialPage* InterstitialPage::GetInterstitialPage(
139 WebContents* web_contents) {
140 InitInterstitialPageMap();
141 InterstitialPageMap::const_iterator iter =
142 g_web_contents_to_interstitial_page->find(web_contents);
143 if (iter == g_web_contents_to_interstitial_page->end())
144 return NULL;
146 return iter->second;
149 InterstitialPageImpl::InterstitialPageImpl(
150 WebContents* web_contents,
151 RenderWidgetHostDelegate* render_widget_host_delegate,
152 bool new_navigation,
153 const GURL& url,
154 InterstitialPageDelegate* delegate)
155 : underlying_content_observer_(web_contents, this),
156 web_contents_(web_contents),
157 controller_(static_cast<NavigationControllerImpl*>(
158 &web_contents->GetController())),
159 render_widget_host_delegate_(render_widget_host_delegate),
160 url_(url),
161 new_navigation_(new_navigation),
162 should_discard_pending_nav_entry_(new_navigation),
163 reload_on_dont_proceed_(false),
164 enabled_(true),
165 action_taken_(NO_ACTION),
166 render_view_host_(NULL),
167 // TODO(nasko): The InterstitialPageImpl will need to provide its own
168 // NavigationControllerImpl to the Navigator, which is separate from
169 // the WebContents one, so we can enforce no navigation policy here.
170 // While we get the code to a point to do this, pass NULL for it.
171 // TODO(creis): We will also need to pass delegates for the RVHM as we
172 // start to use it.
173 frame_tree_(new InterstitialPageNavigatorImpl(this, controller_),
174 this, this, this,
175 static_cast<WebContentsImpl*>(web_contents)),
176 original_child_id_(web_contents->GetRenderProcessHost()->GetID()),
177 original_rvh_id_(web_contents->GetRenderViewHost()->GetRoutingID()),
178 should_revert_web_contents_title_(false),
179 web_contents_was_loading_(false),
180 resource_dispatcher_host_notified_(false),
181 rvh_delegate_view_(new InterstitialPageRVHDelegateView(this)),
182 create_view_(true),
183 delegate_(delegate),
184 weak_ptr_factory_(this) {
185 InitInterstitialPageMap();
186 // It would be inconsistent to create an interstitial with no new navigation
187 // (which is the case when the interstitial was triggered by a sub-resource on
188 // a page) when we have a pending entry (in the process of loading a new top
189 // frame).
190 DCHECK(new_navigation || !web_contents->GetController().GetPendingEntry());
193 InterstitialPageImpl::~InterstitialPageImpl() {
196 void InterstitialPageImpl::Show() {
197 if (!enabled())
198 return;
200 // If an interstitial is already showing or about to be shown, close it before
201 // showing the new one.
202 // Be careful not to take an action on the old interstitial more than once.
203 InterstitialPageMap::const_iterator iter =
204 g_web_contents_to_interstitial_page->find(web_contents_);
205 if (iter != g_web_contents_to_interstitial_page->end()) {
206 InterstitialPageImpl* interstitial = iter->second;
207 if (interstitial->action_taken_ != NO_ACTION) {
208 interstitial->Hide();
209 } else {
210 // If we are currently showing an interstitial page for which we created
211 // a transient entry and a new interstitial is shown as the result of a
212 // new browser initiated navigation, then that transient entry has already
213 // been discarded and a new pending navigation entry created.
214 // So we should not discard that new pending navigation entry.
215 // See http://crbug.com/9791
216 if (new_navigation_ && interstitial->new_navigation_)
217 interstitial->should_discard_pending_nav_entry_= false;
218 interstitial->DontProceed();
222 // Block the resource requests for the render view host while it is hidden.
223 TakeActionOnResourceDispatcher(BLOCK);
224 // We need to be notified when the RenderViewHost is destroyed so we can
225 // cancel the blocked requests. We cannot do that on
226 // NOTIFY_WEB_CONTENTS_DESTROYED as at that point the RenderViewHost has
227 // already been destroyed.
228 notification_registrar_.Add(
229 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
230 Source<RenderWidgetHost>(controller_->delegate()->GetRenderViewHost()));
232 // Update the g_web_contents_to_interstitial_page map.
233 iter = g_web_contents_to_interstitial_page->find(web_contents_);
234 DCHECK(iter == g_web_contents_to_interstitial_page->end());
235 (*g_web_contents_to_interstitial_page)[web_contents_] = this;
237 if (new_navigation_) {
238 scoped_ptr<NavigationEntryImpl> entry =
239 make_scoped_ptr(new NavigationEntryImpl);
240 entry->SetURL(url_);
241 entry->SetVirtualURL(url_);
242 entry->set_page_type(PAGE_TYPE_INTERSTITIAL);
244 // Give delegates a chance to set some states on the navigation entry.
245 delegate_->OverrideEntry(entry.get());
247 controller_->SetTransientEntry(entry.Pass());
249 static_cast<WebContentsImpl*>(web_contents_)->DidChangeVisibleSSLState();
252 DCHECK(!render_view_host_);
253 render_view_host_ = CreateRenderViewHost();
254 CreateWebContentsView();
256 std::string data_url = "data:text/html;charset=utf-8," +
257 net::EscapePath(delegate_->GetHTMLContents());
258 frame_tree_.root()->current_frame_host()->NavigateToURL(GURL(data_url));
259 frame_tree_.root()->current_frame_host()->SetAccessibilityMode(
260 GetAccessibilityMode());
262 notification_registrar_.Add(this, NOTIFICATION_NAV_ENTRY_PENDING,
263 Source<NavigationController>(controller_));
266 void InterstitialPageImpl::Hide() {
267 // We may have already been hidden, and are just waiting to be deleted.
268 // We can't check for enabled() here, because some callers have already
269 // called Disable.
270 if (!render_view_host_)
271 return;
273 Disable();
275 RenderWidgetHostView* old_view =
276 controller_->delegate()->GetRenderViewHost()->GetView();
277 if (controller_->delegate()->GetInterstitialPage() == this &&
278 old_view &&
279 !old_view->IsShowing() &&
280 !controller_->delegate()->IsHidden()) {
281 // Show the original RVH since we're going away. Note it might not exist if
282 // the renderer crashed while the interstitial was showing.
283 // Note that it is important that we don't call Show() if the view is
284 // already showing. That would result in bad things (unparented HWND on
285 // Windows for example) happening.
286 old_view->Show();
289 // If the focus was on the interstitial, let's keep it to the page.
290 // (Note that in unit-tests the RVH may not have a view).
291 if (render_view_host_->GetView() &&
292 render_view_host_->GetView()->HasFocus() &&
293 controller_->delegate()->GetRenderViewHost()->GetView()) {
294 controller_->delegate()->GetRenderViewHost()->GetView()->Focus();
297 // Delete this and call Shutdown on the RVH asynchronously, as we may have
298 // been called from a RVH delegate method, and we can't delete the RVH out
299 // from under itself.
300 base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
301 FROM_HERE, base::Bind(&InterstitialPageImpl::Shutdown,
302 weak_ptr_factory_.GetWeakPtr()));
303 render_view_host_ = NULL;
304 frame_tree_.root()->ResetForNewProcess();
305 controller_->delegate()->DetachInterstitialPage();
306 // Let's revert to the original title if necessary.
307 NavigationEntry* entry = controller_->GetVisibleEntry();
308 if (entry && !new_navigation_ && should_revert_web_contents_title_) {
309 entry->SetTitle(original_web_contents_title_);
310 controller_->delegate()->NotifyNavigationStateChanged(
311 INVALIDATE_TYPE_TITLE);
314 InterstitialPageMap::iterator iter =
315 g_web_contents_to_interstitial_page->find(web_contents_);
316 DCHECK(iter != g_web_contents_to_interstitial_page->end());
317 if (iter != g_web_contents_to_interstitial_page->end())
318 g_web_contents_to_interstitial_page->erase(iter);
320 // Clear the WebContents pointer, because it may now be deleted.
321 // This signifies that we are in the process of shutting down.
322 web_contents_ = NULL;
325 void InterstitialPageImpl::Observe(
326 int type,
327 const NotificationSource& source,
328 const NotificationDetails& details) {
329 switch (type) {
330 case NOTIFICATION_NAV_ENTRY_PENDING:
331 // We are navigating away from the interstitial (the user has typed a URL
332 // in the location bar or clicked a bookmark). Make sure clicking on the
333 // interstitial will have no effect. Also cancel any blocked requests
334 // on the ResourceDispatcherHost. Note that when we get this notification
335 // the RenderViewHost has not yet navigated so we'll unblock the
336 // RenderViewHost before the resource request for the new page we are
337 // navigating arrives in the ResourceDispatcherHost. This ensures that
338 // request won't be blocked if the same RenderViewHost was used for the
339 // new navigation.
340 Disable();
341 TakeActionOnResourceDispatcher(CANCEL);
342 break;
343 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED:
344 if (action_taken_ == NO_ACTION) {
345 // The RenderViewHost is being destroyed (as part of the tab being
346 // closed); make sure we clear the blocked requests.
347 RenderViewHost* rvh = static_cast<RenderViewHost*>(
348 static_cast<RenderViewHostImpl*>(
349 RenderWidgetHostImpl::From(
350 Source<RenderWidgetHost>(source).ptr())));
351 DCHECK(rvh->GetProcess()->GetID() == original_child_id_ &&
352 rvh->GetRoutingID() == original_rvh_id_);
353 TakeActionOnResourceDispatcher(CANCEL);
355 break;
356 default:
357 NOTREACHED();
361 bool InterstitialPageImpl::OnMessageReceived(RenderFrameHost* render_frame_host,
362 const IPC::Message& message) {
363 if (render_frame_host->GetRenderViewHost() != render_view_host_) {
364 DCHECK(!render_view_host_)
365 << "We expect an interstitial page to have only a single RVH";
366 return false;
369 bool handled = true;
370 IPC_BEGIN_MESSAGE_MAP(InterstitialPageImpl, message)
371 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
372 OnDomOperationResponse)
373 IPC_MESSAGE_UNHANDLED(handled = false)
374 IPC_END_MESSAGE_MAP()
376 return handled;
379 bool InterstitialPageImpl::OnMessageReceived(RenderViewHost* render_view_host,
380 const IPC::Message& message) {
381 return false;
384 void InterstitialPageImpl::RenderFrameCreated(
385 RenderFrameHost* render_frame_host) {
386 // Note this is only for subframes in the interstitial, the notification for
387 // the main frame happens in RenderViewCreated.
388 controller_->delegate()->RenderFrameForInterstitialPageCreated(
389 render_frame_host);
392 void InterstitialPageImpl::UpdateTitle(
393 RenderFrameHost* render_frame_host,
394 int32 page_id,
395 const base::string16& title,
396 base::i18n::TextDirection title_direction) {
397 if (!enabled())
398 return;
400 RenderViewHost* render_view_host = render_frame_host->GetRenderViewHost();
401 DCHECK(render_view_host == render_view_host_);
402 NavigationEntry* entry = controller_->GetVisibleEntry();
403 if (!entry) {
404 // There may be no visible entry if no URL has committed (e.g., after
405 // window.open("")). InterstitialPages with the new_navigation flag create
406 // a transient NavigationEntry and thus have a visible entry. However,
407 // interstitials can still be created when there is no visible entry. For
408 // example, the opener window may inject content into the initial blank
409 // page, which might trigger a SafeBrowsingBlockingPage.
410 return;
413 // If this interstitial is shown on an existing navigation entry, we'll need
414 // to remember its title so we can revert to it when hidden.
415 if (!new_navigation_ && !should_revert_web_contents_title_) {
416 original_web_contents_title_ = entry->GetTitle();
417 should_revert_web_contents_title_ = true;
419 // TODO(evan): make use of title_direction.
420 // http://code.google.com/p/chromium/issues/detail?id=27094
421 entry->SetTitle(title);
422 controller_->delegate()->NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
425 AccessibilityMode InterstitialPageImpl::GetAccessibilityMode() const {
426 if (web_contents_)
427 return static_cast<WebContentsImpl*>(web_contents_)->GetAccessibilityMode();
428 else
429 return AccessibilityModeOff;
432 void InterstitialPageImpl::Cut() {
433 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
434 if (!focused_node)
435 return;
437 focused_node->current_frame_host()->Send(
438 new InputMsg_Cut(focused_node->current_frame_host()->GetRoutingID()));
439 RecordAction(base::UserMetricsAction("Cut"));
442 void InterstitialPageImpl::Copy() {
443 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
444 if (!focused_node)
445 return;
447 focused_node->current_frame_host()->Send(
448 new InputMsg_Copy(focused_node->current_frame_host()->GetRoutingID()));
449 RecordAction(base::UserMetricsAction("Copy"));
452 void InterstitialPageImpl::Paste() {
453 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
454 if (!focused_node)
455 return;
457 focused_node->current_frame_host()->Send(
458 new InputMsg_Paste(focused_node->current_frame_host()->GetRoutingID()));
459 RecordAction(base::UserMetricsAction("Paste"));
462 void InterstitialPageImpl::SelectAll() {
463 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
464 if (!focused_node)
465 return;
467 focused_node->current_frame_host()->Send(new InputMsg_SelectAll(
468 focused_node->current_frame_host()->GetRoutingID()));
469 RecordAction(base::UserMetricsAction("SelectAll"));
472 RenderViewHostDelegateView* InterstitialPageImpl::GetDelegateView() {
473 return rvh_delegate_view_.get();
476 const GURL& InterstitialPageImpl::GetMainFrameLastCommittedURL() const {
477 return url_;
480 void InterstitialPageImpl::RenderViewTerminated(
481 RenderViewHost* render_view_host,
482 base::TerminationStatus status,
483 int error_code) {
484 // Our renderer died. This should not happen in normal cases.
485 // If we haven't already started shutdown, just dismiss the interstitial.
486 // We cannot check for enabled() here, because we may have called Disable
487 // without calling Hide.
488 if (render_view_host_)
489 DontProceed();
492 void InterstitialPageImpl::DidNavigate(
493 RenderViewHost* render_view_host,
494 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
495 // A fast user could have navigated away from the page that triggered the
496 // interstitial while the interstitial was loading, that would have disabled
497 // us. In that case we can dismiss ourselves.
498 if (!enabled()) {
499 DontProceed();
500 return;
502 if (ui::PageTransitionCoreTypeIs(params.transition,
503 ui::PAGE_TRANSITION_AUTO_SUBFRAME)) {
504 // No need to handle navigate message from iframe in the interstitial page.
505 return;
508 // The RenderViewHost has loaded its contents, we can show it now.
509 if (!controller_->delegate()->IsHidden())
510 render_view_host_->GetView()->Show();
511 controller_->delegate()->AttachInterstitialPage(this);
513 RenderWidgetHostView* rwh_view =
514 controller_->delegate()->GetRenderViewHost()->GetView();
516 // The RenderViewHost may already have crashed before we even get here.
517 if (rwh_view) {
518 // If the page has focus, focus the interstitial.
519 if (rwh_view->HasFocus())
520 Focus();
522 // Hide the original RVH since we're showing the interstitial instead.
523 rwh_view->Hide();
526 // Notify the tab we are not loading so the throbber is stopped. It also
527 // causes a WebContentsObserver::DidStopLoading callback that the
528 // AutomationProvider (used by the UI tests) expects to consider a navigation
529 // as complete. Without this, navigating in a UI test to a URL that triggers
530 // an interstitial would hang.
531 web_contents_was_loading_ = controller_->delegate()->IsLoading();
532 controller_->delegate()->SetIsLoading(false, true, NULL);
535 RendererPreferences InterstitialPageImpl::GetRendererPrefs(
536 BrowserContext* browser_context) const {
537 delegate_->OverrideRendererPrefs(&renderer_preferences_);
538 return renderer_preferences_;
541 void InterstitialPageImpl::RenderWidgetDeleted(
542 RenderWidgetHostImpl* render_widget_host) {
543 // TODO(creis): Remove this method once we verify the shutdown path is sane.
544 CHECK(!web_contents_);
547 bool InterstitialPageImpl::PreHandleKeyboardEvent(
548 const NativeWebKeyboardEvent& event,
549 bool* is_keyboard_shortcut) {
550 if (!enabled())
551 return false;
552 return render_widget_host_delegate_->PreHandleKeyboardEvent(
553 event, is_keyboard_shortcut);
556 void InterstitialPageImpl::HandleKeyboardEvent(
557 const NativeWebKeyboardEvent& event) {
558 if (enabled())
559 render_widget_host_delegate_->HandleKeyboardEvent(event);
562 #if defined(OS_WIN)
563 gfx::NativeViewAccessible
564 InterstitialPageImpl::GetParentNativeViewAccessible() {
565 if (web_contents_) {
566 WebContentsImpl* wci = static_cast<WebContentsImpl*>(web_contents_);
567 return wci->GetParentNativeViewAccessible();
569 return NULL;
571 #endif
573 WebContents* InterstitialPageImpl::web_contents() const {
574 return web_contents_;
577 RenderViewHostImpl* InterstitialPageImpl::CreateRenderViewHost() {
578 if (!enabled())
579 return NULL;
581 // Interstitial pages don't want to share the session storage so we mint a
582 // new one.
583 BrowserContext* browser_context = web_contents()->GetBrowserContext();
584 scoped_refptr<SiteInstance> site_instance =
585 SiteInstance::Create(browser_context);
586 DOMStorageContextWrapper* dom_storage_context =
587 static_cast<DOMStorageContextWrapper*>(
588 BrowserContext::GetStoragePartition(
589 browser_context, site_instance.get())->GetDOMStorageContext());
590 session_storage_namespace_ =
591 new SessionStorageNamespaceImpl(dom_storage_context);
593 // Use the RenderViewHost from our FrameTree.
594 frame_tree_.root()->render_manager()->Init(
595 browser_context, site_instance.get(), MSG_ROUTING_NONE, MSG_ROUTING_NONE);
596 return frame_tree_.root()->current_frame_host()->render_view_host();
599 WebContentsView* InterstitialPageImpl::CreateWebContentsView() {
600 if (!enabled() || !create_view_)
601 return NULL;
602 WebContentsView* wcv =
603 static_cast<WebContentsImpl*>(web_contents())->GetView();
604 RenderWidgetHostViewBase* view =
605 wcv->CreateViewForWidget(render_view_host_, false);
606 render_view_host_->SetView(view);
607 render_view_host_->AllowBindings(BINDINGS_POLICY_DOM_AUTOMATION);
609 int32 max_page_id = web_contents()->
610 GetMaxPageIDForSiteInstance(render_view_host_->GetSiteInstance());
611 render_view_host_->CreateRenderView(MSG_ROUTING_NONE,
612 MSG_ROUTING_NONE,
613 max_page_id,
614 FrameReplicationState(),
615 false);
616 controller_->delegate()->RenderFrameForInterstitialPageCreated(
617 frame_tree_.root()->current_frame_host());
618 view->SetSize(web_contents()->GetContainerBounds().size());
619 // Don't show the interstitial until we have navigated to it.
620 view->Hide();
621 return wcv;
624 void InterstitialPageImpl::Proceed() {
625 // Don't repeat this if we are already shutting down. We cannot check for
626 // enabled() here, because we may have called Disable without calling Hide.
627 if (!render_view_host_)
628 return;
630 if (action_taken_ != NO_ACTION) {
631 NOTREACHED();
632 return;
634 Disable();
635 action_taken_ = PROCEED_ACTION;
637 // Resumes the throbber, if applicable.
638 if (web_contents_was_loading_)
639 controller_->delegate()->SetIsLoading(true, true, NULL);
641 // If this is a new navigation, the old page is going away, so we cancel any
642 // blocked requests for it. If it is not a new navigation, then it means the
643 // interstitial was shown as a result of a resource loading in the page.
644 // Since the user wants to proceed, we'll let any blocked request go through.
645 if (new_navigation_)
646 TakeActionOnResourceDispatcher(CANCEL);
647 else
648 TakeActionOnResourceDispatcher(RESUME);
650 // No need to hide if we are a new navigation, we'll get hidden when the
651 // navigation is committed.
652 if (!new_navigation_) {
653 Hide();
654 delegate_->OnProceed();
655 return;
658 delegate_->OnProceed();
661 void InterstitialPageImpl::DontProceed() {
662 // Don't repeat this if we are already shutting down. We cannot check for
663 // enabled() here, because we may have called Disable without calling Hide.
664 if (!render_view_host_)
665 return;
666 DCHECK(action_taken_ != DONT_PROCEED_ACTION);
668 Disable();
669 action_taken_ = DONT_PROCEED_ACTION;
671 // If this is a new navigation, we are returning to the original page, so we
672 // resume blocked requests for it. If it is not a new navigation, then it
673 // means the interstitial was shown as a result of a resource loading in the
674 // page and we won't return to the original page, so we cancel blocked
675 // requests in that case.
676 if (new_navigation_)
677 TakeActionOnResourceDispatcher(RESUME);
678 else
679 TakeActionOnResourceDispatcher(CANCEL);
681 if (should_discard_pending_nav_entry_) {
682 // Since no navigation happens we have to discard the transient entry
683 // explicitely. Note that by calling DiscardNonCommittedEntries() we also
684 // discard the pending entry, which is what we want, since the navigation is
685 // cancelled.
686 controller_->DiscardNonCommittedEntries();
689 if (reload_on_dont_proceed_)
690 controller_->Reload(true);
692 Hide();
693 delegate_->OnDontProceed();
696 void InterstitialPageImpl::CancelForNavigation() {
697 // The user is trying to navigate away. We should unblock the renderer and
698 // disable the interstitial, but keep it visible until the navigation
699 // completes.
700 Disable();
701 // If this interstitial was shown for a new navigation, allow any navigations
702 // on the original page to resume (e.g., subresource requests, XHRs, etc).
703 // Otherwise, cancel the pending, possibly dangerous navigations.
704 if (new_navigation_)
705 TakeActionOnResourceDispatcher(RESUME);
706 else
707 TakeActionOnResourceDispatcher(CANCEL);
710 void InterstitialPageImpl::SetSize(const gfx::Size& size) {
711 if (!enabled())
712 return;
713 #if !defined(OS_MACOSX)
714 // When a tab is closed, we might be resized after our view was NULLed
715 // (typically if there was an info-bar).
716 if (render_view_host_->GetView())
717 render_view_host_->GetView()->SetSize(size);
718 #else
719 // TODO(port): Does Mac need to SetSize?
720 NOTIMPLEMENTED();
721 #endif
724 void InterstitialPageImpl::Focus() {
725 // Focus the native window.
726 if (!enabled())
727 return;
728 render_view_host_->GetView()->Focus();
731 void InterstitialPageImpl::FocusThroughTabTraversal(bool reverse) {
732 if (!enabled())
733 return;
734 render_view_host_->SetInitialFocus(reverse);
737 RenderWidgetHostView* InterstitialPageImpl::GetView() {
738 return render_view_host_->GetView();
741 RenderFrameHost* InterstitialPageImpl::GetMainFrame() const {
742 return render_view_host_->GetMainFrame();
745 InterstitialPageDelegate* InterstitialPageImpl::GetDelegateForTesting() {
746 return delegate_.get();
749 void InterstitialPageImpl::DontCreateViewForTesting() {
750 create_view_ = false;
753 gfx::Rect InterstitialPageImpl::GetRootWindowResizerRect() const {
754 return gfx::Rect();
757 void InterstitialPageImpl::CreateNewWindow(
758 SiteInstance* source_site_instance,
759 int route_id,
760 int main_frame_route_id,
761 const ViewHostMsg_CreateWindow_Params& params,
762 SessionStorageNamespace* session_storage_namespace) {
763 NOTREACHED() << "InterstitialPage does not support showing popups yet.";
766 void InterstitialPageImpl::CreateNewWidget(int render_process_id,
767 int route_id,
768 blink::WebPopupType popup_type) {
769 NOTREACHED() << "InterstitialPage does not support showing drop-downs yet.";
772 void InterstitialPageImpl::CreateNewFullscreenWidget(int render_process_id,
773 int route_id) {
774 NOTREACHED()
775 << "InterstitialPage does not support showing full screen popups.";
778 void InterstitialPageImpl::ShowCreatedWindow(int route_id,
779 WindowOpenDisposition disposition,
780 const gfx::Rect& initial_rect,
781 bool user_gesture) {
782 NOTREACHED() << "InterstitialPage does not support showing popups yet.";
785 void InterstitialPageImpl::ShowCreatedWidget(int route_id,
786 const gfx::Rect& initial_rect) {
787 NOTREACHED() << "InterstitialPage does not support showing drop-downs yet.";
790 void InterstitialPageImpl::ShowCreatedFullscreenWidget(int route_id) {
791 NOTREACHED()
792 << "InterstitialPage does not support showing full screen popups.";
795 SessionStorageNamespace* InterstitialPageImpl::GetSessionStorageNamespace(
796 SiteInstance* instance) {
797 return session_storage_namespace_.get();
800 FrameTree* InterstitialPageImpl::GetFrameTree() {
801 return &frame_tree_;
804 void InterstitialPageImpl::Disable() {
805 enabled_ = false;
808 void InterstitialPageImpl::Shutdown() {
809 delete this;
812 void InterstitialPageImpl::OnNavigatingAwayOrTabClosing() {
813 if (action_taken_ == NO_ACTION) {
814 // We are navigating away from the interstitial or closing a tab with an
815 // interstitial. Default to DontProceed(). We don't just call Hide as
816 // subclasses will almost certainly override DontProceed to do some work
817 // (ex: close pending connections).
818 DontProceed();
819 } else {
820 // User decided to proceed and either the navigation was committed or
821 // the tab was closed before that.
822 Hide();
826 void InterstitialPageImpl::TakeActionOnResourceDispatcher(
827 ResourceRequestAction action) {
828 DCHECK_CURRENTLY_ON(BrowserThread::UI);
830 if (action == CANCEL || action == RESUME) {
831 if (resource_dispatcher_host_notified_)
832 return;
833 resource_dispatcher_host_notified_ = true;
836 // The tab might not have a render_view_host if it was closed (in which case,
837 // we have taken care of the blocked requests when processing
838 // NOTIFY_RENDER_WIDGET_HOST_DESTROYED.
839 // Also we need to test there is a ResourceDispatcherHostImpl, as when unit-
840 // tests we don't have one.
841 RenderViewHostImpl* rvh = RenderViewHostImpl::FromID(original_child_id_,
842 original_rvh_id_);
843 if (!rvh || !ResourceDispatcherHostImpl::Get())
844 return;
846 BrowserThread::PostTask(
847 BrowserThread::IO,
848 FROM_HERE,
849 base::Bind(
850 &ResourceRequestHelper,
851 ResourceDispatcherHostImpl::Get(),
852 original_child_id_,
853 original_rvh_id_,
854 action));
857 void InterstitialPageImpl::OnDomOperationResponse(
858 const std::string& json_string,
859 int automation_id) {
860 // Needed by test code.
861 DomOperationNotificationDetails details(json_string, automation_id);
862 NotificationService::current()->Notify(
863 NOTIFICATION_DOM_OPERATION_RESPONSE,
864 Source<WebContents>(web_contents()),
865 Details<DomOperationNotificationDetails>(&details));
867 if (!enabled())
868 return;
869 delegate_->CommandReceived(details.json);
873 InterstitialPageImpl::InterstitialPageRVHDelegateView::
874 InterstitialPageRVHDelegateView(InterstitialPageImpl* page)
875 : interstitial_page_(page) {
878 #if defined(OS_MACOSX) || defined(OS_ANDROID)
879 void InterstitialPageImpl::InterstitialPageRVHDelegateView::ShowPopupMenu(
880 RenderFrameHost* render_frame_host,
881 const gfx::Rect& bounds,
882 int item_height,
883 double item_font_size,
884 int selected_item,
885 const std::vector<MenuItem>& items,
886 bool right_aligned,
887 bool allow_multiple_selection) {
888 NOTREACHED() << "InterstitialPage does not support showing popup menus.";
891 void InterstitialPageImpl::InterstitialPageRVHDelegateView::HidePopupMenu() {
892 NOTREACHED() << "InterstitialPage does not support showing popup menus.";
894 #endif
896 void InterstitialPageImpl::InterstitialPageRVHDelegateView::StartDragging(
897 const DropData& drop_data,
898 WebDragOperationsMask allowed_operations,
899 const gfx::ImageSkia& image,
900 const gfx::Vector2d& image_offset,
901 const DragEventSourceInfo& event_info) {
902 interstitial_page_->render_view_host_->DragSourceSystemDragEnded();
903 DVLOG(1) << "InterstitialPage does not support dragging yet.";
906 void InterstitialPageImpl::InterstitialPageRVHDelegateView::UpdateDragCursor(
907 WebDragOperation) {
908 NOTREACHED() << "InterstitialPage does not support dragging yet.";
911 void InterstitialPageImpl::InterstitialPageRVHDelegateView::GotFocus() {
912 WebContents* web_contents = interstitial_page_->web_contents();
913 if (web_contents)
914 static_cast<WebContentsImpl*>(web_contents)->NotifyWebContentsFocused();
917 void InterstitialPageImpl::InterstitialPageRVHDelegateView::TakeFocus(
918 bool reverse) {
919 if (!interstitial_page_->web_contents())
920 return;
921 WebContentsImpl* web_contents =
922 static_cast<WebContentsImpl*>(interstitial_page_->web_contents());
923 if (!web_contents->GetDelegateView())
924 return;
926 web_contents->GetDelegateView()->TakeFocus(reverse);
929 void InterstitialPageImpl::InterstitialPageRVHDelegateView::OnFindReply(
930 int request_id, int number_of_matches, const gfx::Rect& selection_rect,
931 int active_match_ordinal, bool final_update) {
934 InterstitialPageImpl::UnderlyingContentObserver::UnderlyingContentObserver(
935 WebContents* web_contents,
936 InterstitialPageImpl* interstitial)
937 : WebContentsObserver(web_contents), interstitial_(interstitial) {
940 void InterstitialPageImpl::UnderlyingContentObserver::NavigationEntryCommitted(
941 const LoadCommittedDetails& load_details) {
942 interstitial_->OnNavigatingAwayOrTabClosing();
945 void InterstitialPageImpl::UnderlyingContentObserver::WebContentsDestroyed() {
946 interstitial_->OnNavigatingAwayOrTabClosing();
949 } // namespace content