chrome/browser/extensions: Remove use of MessageLoopProxy and deprecated MessageLoop...
[chromium-blink-merge.git] / content / browser / frame_host / interstitial_page_impl.cc
blob88d720fcf8b866593c68f598d8be0acba3c8b11a
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/view_messages.h"
33 #include "content/public/browser/browser_context.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/browser/content_browser_client.h"
36 #include "content/public/browser/dom_operation_notification_details.h"
37 #include "content/public/browser/interstitial_page_delegate.h"
38 #include "content/public/browser/invalidate_type.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/notification_source.h"
41 #include "content/public/browser/storage_partition.h"
42 #include "content/public/browser/user_metrics.h"
43 #include "content/public/browser/web_contents_delegate.h"
44 #include "content/public/common/bindings_policy.h"
45 #include "net/base/escape.h"
46 #include "net/url_request/url_request_context_getter.h"
47 #include "ui/base/page_transition_types.h"
49 using blink::WebDragOperation;
50 using blink::WebDragOperationsMask;
52 namespace content {
53 namespace {
55 void ResourceRequestHelper(ResourceDispatcherHostImpl* rdh,
56 int process_id,
57 int render_view_host_id,
58 ResourceRequestAction action) {
59 switch (action) {
60 case BLOCK:
61 rdh->BlockRequestsForRoute(process_id, render_view_host_id);
62 break;
63 case RESUME:
64 rdh->ResumeBlockedRequestsForRoute(process_id, render_view_host_id);
65 break;
66 case CANCEL:
67 rdh->CancelBlockedRequestsForRoute(process_id, render_view_host_id);
68 break;
69 default:
70 NOTREACHED();
74 } // namespace
76 class InterstitialPageImpl::InterstitialPageRVHDelegateView
77 : public RenderViewHostDelegateView {
78 public:
79 explicit InterstitialPageRVHDelegateView(InterstitialPageImpl* page);
81 // RenderViewHostDelegateView implementation:
82 #if defined(OS_MACOSX) || defined(OS_ANDROID)
83 void ShowPopupMenu(RenderFrameHost* render_frame_host,
84 const gfx::Rect& bounds,
85 int item_height,
86 double item_font_size,
87 int selected_item,
88 const std::vector<MenuItem>& items,
89 bool right_aligned,
90 bool allow_multiple_selection) override;
91 void HidePopupMenu() override;
92 #endif
93 void StartDragging(const DropData& drop_data,
94 WebDragOperationsMask operations_allowed,
95 const gfx::ImageSkia& image,
96 const gfx::Vector2d& image_offset,
97 const DragEventSourceInfo& event_info) override;
98 void UpdateDragCursor(WebDragOperation operation) override;
99 void GotFocus() override;
100 void TakeFocus(bool reverse) override;
101 virtual void OnFindReply(int request_id,
102 int number_of_matches,
103 const gfx::Rect& selection_rect,
104 int active_match_ordinal,
105 bool final_update);
107 private:
108 InterstitialPageImpl* interstitial_page_;
110 DISALLOW_COPY_AND_ASSIGN(InterstitialPageRVHDelegateView);
114 // We keep a map of the various blocking pages shown as the UI tests need to
115 // be able to retrieve them.
116 typedef std::map<WebContents*, InterstitialPageImpl*> InterstitialPageMap;
117 static InterstitialPageMap* g_web_contents_to_interstitial_page;
119 // Initializes g_web_contents_to_interstitial_page in a thread-safe manner.
120 // Should be called before accessing g_web_contents_to_interstitial_page.
121 static void InitInterstitialPageMap() {
122 if (!g_web_contents_to_interstitial_page)
123 g_web_contents_to_interstitial_page = new InterstitialPageMap;
126 InterstitialPage* InterstitialPage::Create(WebContents* web_contents,
127 bool new_navigation,
128 const GURL& url,
129 InterstitialPageDelegate* delegate) {
130 return new InterstitialPageImpl(
131 web_contents,
132 static_cast<RenderWidgetHostDelegate*>(
133 static_cast<WebContentsImpl*>(web_contents)),
134 new_navigation, url, delegate);
137 InterstitialPage* InterstitialPage::GetInterstitialPage(
138 WebContents* web_contents) {
139 InitInterstitialPageMap();
140 InterstitialPageMap::const_iterator iter =
141 g_web_contents_to_interstitial_page->find(web_contents);
142 if (iter == g_web_contents_to_interstitial_page->end())
143 return NULL;
145 return iter->second;
148 InterstitialPageImpl::InterstitialPageImpl(
149 WebContents* web_contents,
150 RenderWidgetHostDelegate* render_widget_host_delegate,
151 bool new_navigation,
152 const GURL& url,
153 InterstitialPageDelegate* delegate)
154 : underlying_content_observer_(web_contents, this),
155 web_contents_(web_contents),
156 controller_(static_cast<NavigationControllerImpl*>(
157 &web_contents->GetController())),
158 render_widget_host_delegate_(render_widget_host_delegate),
159 url_(url),
160 new_navigation_(new_navigation),
161 should_discard_pending_nav_entry_(new_navigation),
162 reload_on_dont_proceed_(false),
163 enabled_(true),
164 action_taken_(NO_ACTION),
165 render_view_host_(NULL),
166 // TODO(nasko): The InterstitialPageImpl will need to provide its own
167 // NavigationControllerImpl to the Navigator, which is separate from
168 // the WebContents one, so we can enforce no navigation policy here.
169 // While we get the code to a point to do this, pass NULL for it.
170 // TODO(creis): We will also need to pass delegates for the RVHM as we
171 // start to use it.
172 frame_tree_(new InterstitialPageNavigatorImpl(this, controller_),
173 this, this, this,
174 static_cast<WebContentsImpl*>(web_contents)),
175 original_child_id_(web_contents->GetRenderProcessHost()->GetID()),
176 original_rvh_id_(web_contents->GetRenderViewHost()->GetRoutingID()),
177 should_revert_web_contents_title_(false),
178 web_contents_was_loading_(false),
179 resource_dispatcher_host_notified_(false),
180 rvh_delegate_view_(new InterstitialPageRVHDelegateView(this)),
181 create_view_(true),
182 delegate_(delegate),
183 weak_ptr_factory_(this) {
184 InitInterstitialPageMap();
185 // It would be inconsistent to create an interstitial with no new navigation
186 // (which is the case when the interstitial was triggered by a sub-resource on
187 // a page) when we have a pending entry (in the process of loading a new top
188 // frame).
189 DCHECK(new_navigation || !web_contents->GetController().GetPendingEntry());
192 InterstitialPageImpl::~InterstitialPageImpl() {
195 void InterstitialPageImpl::Show() {
196 if (!enabled())
197 return;
199 // If an interstitial is already showing or about to be shown, close it before
200 // showing the new one.
201 // Be careful not to take an action on the old interstitial more than once.
202 InterstitialPageMap::const_iterator iter =
203 g_web_contents_to_interstitial_page->find(web_contents_);
204 if (iter != g_web_contents_to_interstitial_page->end()) {
205 InterstitialPageImpl* interstitial = iter->second;
206 if (interstitial->action_taken_ != NO_ACTION) {
207 interstitial->Hide();
208 } else {
209 // If we are currently showing an interstitial page for which we created
210 // a transient entry and a new interstitial is shown as the result of a
211 // new browser initiated navigation, then that transient entry has already
212 // been discarded and a new pending navigation entry created.
213 // So we should not discard that new pending navigation entry.
214 // See http://crbug.com/9791
215 if (new_navigation_ && interstitial->new_navigation_)
216 interstitial->should_discard_pending_nav_entry_= false;
217 interstitial->DontProceed();
221 // Block the resource requests for the render view host while it is hidden.
222 TakeActionOnResourceDispatcher(BLOCK);
223 // We need to be notified when the RenderViewHost is destroyed so we can
224 // cancel the blocked requests. We cannot do that on
225 // NOTIFY_WEB_CONTENTS_DESTROYED as at that point the RenderViewHost has
226 // already been destroyed.
227 notification_registrar_.Add(
228 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
229 Source<RenderWidgetHost>(controller_->delegate()->GetRenderViewHost()));
231 // Update the g_web_contents_to_interstitial_page map.
232 iter = g_web_contents_to_interstitial_page->find(web_contents_);
233 DCHECK(iter == g_web_contents_to_interstitial_page->end());
234 (*g_web_contents_to_interstitial_page)[web_contents_] = this;
236 if (new_navigation_) {
237 NavigationEntryImpl* entry = new NavigationEntryImpl;
238 entry->SetURL(url_);
239 entry->SetVirtualURL(url_);
240 entry->set_page_type(PAGE_TYPE_INTERSTITIAL);
242 // Give delegates a chance to set some states on the navigation entry.
243 delegate_->OverrideEntry(entry);
245 controller_->SetTransientEntry(entry);
248 DCHECK(!render_view_host_);
249 render_view_host_ = CreateRenderViewHost();
250 CreateWebContentsView();
252 std::string data_url = "data:text/html;charset=utf-8," +
253 net::EscapePath(delegate_->GetHTMLContents());
254 frame_tree_.root()->current_frame_host()->NavigateToURL(GURL(data_url));
256 notification_registrar_.Add(this, NOTIFICATION_NAV_ENTRY_PENDING,
257 Source<NavigationController>(controller_));
260 void InterstitialPageImpl::Hide() {
261 // We may have already been hidden, and are just waiting to be deleted.
262 // We can't check for enabled() here, because some callers have already
263 // called Disable.
264 if (!render_view_host_)
265 return;
267 Disable();
269 RenderWidgetHostView* old_view =
270 controller_->delegate()->GetRenderViewHost()->GetView();
271 if (controller_->delegate()->GetInterstitialPage() == this &&
272 old_view &&
273 !old_view->IsShowing() &&
274 !controller_->delegate()->IsHidden()) {
275 // Show the original RVH since we're going away. Note it might not exist if
276 // the renderer crashed while the interstitial was showing.
277 // Note that it is important that we don't call Show() if the view is
278 // already showing. That would result in bad things (unparented HWND on
279 // Windows for example) happening.
280 old_view->Show();
283 // If the focus was on the interstitial, let's keep it to the page.
284 // (Note that in unit-tests the RVH may not have a view).
285 if (render_view_host_->GetView() &&
286 render_view_host_->GetView()->HasFocus() &&
287 controller_->delegate()->GetRenderViewHost()->GetView()) {
288 controller_->delegate()->GetRenderViewHost()->GetView()->Focus();
291 // Delete this and call Shutdown on the RVH asynchronously, as we may have
292 // been called from a RVH delegate method, and we can't delete the RVH out
293 // from under itself.
294 base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
295 FROM_HERE, base::Bind(&InterstitialPageImpl::Shutdown,
296 weak_ptr_factory_.GetWeakPtr()));
297 render_view_host_ = NULL;
298 frame_tree_.root()->ResetForNewProcess();
299 controller_->delegate()->DetachInterstitialPage();
300 // Let's revert to the original title if necessary.
301 NavigationEntry* entry = controller_->GetVisibleEntry();
302 if (entry && !new_navigation_ && should_revert_web_contents_title_) {
303 entry->SetTitle(original_web_contents_title_);
304 controller_->delegate()->NotifyNavigationStateChanged(
305 INVALIDATE_TYPE_TITLE);
308 InterstitialPageMap::iterator iter =
309 g_web_contents_to_interstitial_page->find(web_contents_);
310 DCHECK(iter != g_web_contents_to_interstitial_page->end());
311 if (iter != g_web_contents_to_interstitial_page->end())
312 g_web_contents_to_interstitial_page->erase(iter);
314 // Clear the WebContents pointer, because it may now be deleted.
315 // This signifies that we are in the process of shutting down.
316 web_contents_ = NULL;
319 void InterstitialPageImpl::Observe(
320 int type,
321 const NotificationSource& source,
322 const NotificationDetails& details) {
323 switch (type) {
324 case NOTIFICATION_NAV_ENTRY_PENDING:
325 // We are navigating away from the interstitial (the user has typed a URL
326 // in the location bar or clicked a bookmark). Make sure clicking on the
327 // interstitial will have no effect. Also cancel any blocked requests
328 // on the ResourceDispatcherHost. Note that when we get this notification
329 // the RenderViewHost has not yet navigated so we'll unblock the
330 // RenderViewHost before the resource request for the new page we are
331 // navigating arrives in the ResourceDispatcherHost. This ensures that
332 // request won't be blocked if the same RenderViewHost was used for the
333 // new navigation.
334 Disable();
335 TakeActionOnResourceDispatcher(CANCEL);
336 break;
337 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED:
338 if (action_taken_ == NO_ACTION) {
339 // The RenderViewHost is being destroyed (as part of the tab being
340 // closed); make sure we clear the blocked requests.
341 RenderViewHost* rvh = static_cast<RenderViewHost*>(
342 static_cast<RenderViewHostImpl*>(
343 RenderWidgetHostImpl::From(
344 Source<RenderWidgetHost>(source).ptr())));
345 DCHECK(rvh->GetProcess()->GetID() == original_child_id_ &&
346 rvh->GetRoutingID() == original_rvh_id_);
347 TakeActionOnResourceDispatcher(CANCEL);
349 break;
350 default:
351 NOTREACHED();
355 bool InterstitialPageImpl::OnMessageReceived(RenderFrameHost* render_frame_host,
356 const IPC::Message& message) {
357 if (render_frame_host->GetRenderViewHost() != render_view_host_) {
358 DCHECK(!render_view_host_)
359 << "We expect an interstitial page to have only a single RVH";
360 return false;
363 bool handled = true;
364 IPC_BEGIN_MESSAGE_MAP(InterstitialPageImpl, message)
365 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
366 OnDomOperationResponse)
367 IPC_MESSAGE_UNHANDLED(handled = false)
368 IPC_END_MESSAGE_MAP()
370 return handled;
373 bool InterstitialPageImpl::OnMessageReceived(RenderViewHost* render_view_host,
374 const IPC::Message& message) {
375 return false;
378 void InterstitialPageImpl::RenderFrameCreated(
379 RenderFrameHost* render_frame_host) {
380 // Note this is only for subframes in the interstitial, the notification for
381 // the main frame happens in RenderViewCreated.
382 controller_->delegate()->RenderFrameForInterstitialPageCreated(
383 render_frame_host);
386 void InterstitialPageImpl::UpdateTitle(
387 RenderFrameHost* render_frame_host,
388 int32 page_id,
389 const base::string16& title,
390 base::i18n::TextDirection title_direction) {
391 if (!enabled())
392 return;
394 RenderViewHost* render_view_host = render_frame_host->GetRenderViewHost();
395 DCHECK(render_view_host == render_view_host_);
396 NavigationEntry* entry = controller_->GetVisibleEntry();
397 if (!entry) {
398 // There may be no visible entry if no URL has committed (e.g., after
399 // window.open("")). InterstitialPages with the new_navigation flag create
400 // a transient NavigationEntry and thus have a visible entry. However,
401 // interstitials can still be created when there is no visible entry. For
402 // example, the opener window may inject content into the initial blank
403 // page, which might trigger a SafeBrowsingBlockingPage.
404 return;
407 // If this interstitial is shown on an existing navigation entry, we'll need
408 // to remember its title so we can revert to it when hidden.
409 if (!new_navigation_ && !should_revert_web_contents_title_) {
410 original_web_contents_title_ = entry->GetTitle();
411 should_revert_web_contents_title_ = true;
413 // TODO(evan): make use of title_direction.
414 // http://code.google.com/p/chromium/issues/detail?id=27094
415 entry->SetTitle(title);
416 controller_->delegate()->NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
419 AccessibilityMode InterstitialPageImpl::GetAccessibilityMode() const {
420 if (web_contents_)
421 return static_cast<WebContentsImpl*>(web_contents_)->GetAccessibilityMode();
422 else
423 return AccessibilityModeOff;
426 RenderViewHostDelegateView* InterstitialPageImpl::GetDelegateView() {
427 return rvh_delegate_view_.get();
430 const GURL& InterstitialPageImpl::GetMainFrameLastCommittedURL() const {
431 return url_;
434 void InterstitialPageImpl::RenderViewTerminated(
435 RenderViewHost* render_view_host,
436 base::TerminationStatus status,
437 int error_code) {
438 // Our renderer died. This should not happen in normal cases.
439 // If we haven't already started shutdown, just dismiss the interstitial.
440 // We cannot check for enabled() here, because we may have called Disable
441 // without calling Hide.
442 if (render_view_host_)
443 DontProceed();
446 void InterstitialPageImpl::DidNavigate(
447 RenderViewHost* render_view_host,
448 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
449 // A fast user could have navigated away from the page that triggered the
450 // interstitial while the interstitial was loading, that would have disabled
451 // us. In that case we can dismiss ourselves.
452 if (!enabled()) {
453 DontProceed();
454 return;
456 if (ui::PageTransitionCoreTypeIs(params.transition,
457 ui::PAGE_TRANSITION_AUTO_SUBFRAME)) {
458 // No need to handle navigate message from iframe in the interstitial page.
459 return;
462 // The RenderViewHost has loaded its contents, we can show it now.
463 if (!controller_->delegate()->IsHidden())
464 render_view_host_->GetView()->Show();
465 controller_->delegate()->AttachInterstitialPage(this);
467 RenderWidgetHostView* rwh_view =
468 controller_->delegate()->GetRenderViewHost()->GetView();
470 // The RenderViewHost may already have crashed before we even get here.
471 if (rwh_view) {
472 // If the page has focus, focus the interstitial.
473 if (rwh_view->HasFocus())
474 Focus();
476 // Hide the original RVH since we're showing the interstitial instead.
477 rwh_view->Hide();
480 // Notify the tab we are not loading so the throbber is stopped. It also
481 // causes a WebContentsObserver::DidStopLoading callback that the
482 // AutomationProvider (used by the UI tests) expects to consider a navigation
483 // as complete. Without this, navigating in a UI test to a URL that triggers
484 // an interstitial would hang.
485 web_contents_was_loading_ = controller_->delegate()->IsLoading();
486 controller_->delegate()->SetIsLoading(false, true, NULL);
489 RendererPreferences InterstitialPageImpl::GetRendererPrefs(
490 BrowserContext* browser_context) const {
491 delegate_->OverrideRendererPrefs(&renderer_preferences_);
492 return renderer_preferences_;
495 void InterstitialPageImpl::RenderWidgetDeleted(
496 RenderWidgetHostImpl* render_widget_host) {
497 // TODO(creis): Remove this method once we verify the shutdown path is sane.
498 CHECK(!web_contents_);
501 bool InterstitialPageImpl::PreHandleKeyboardEvent(
502 const NativeWebKeyboardEvent& event,
503 bool* is_keyboard_shortcut) {
504 if (!enabled())
505 return false;
506 return render_widget_host_delegate_->PreHandleKeyboardEvent(
507 event, is_keyboard_shortcut);
510 void InterstitialPageImpl::HandleKeyboardEvent(
511 const NativeWebKeyboardEvent& event) {
512 if (enabled())
513 render_widget_host_delegate_->HandleKeyboardEvent(event);
516 #if defined(OS_WIN)
517 gfx::NativeViewAccessible
518 InterstitialPageImpl::GetParentNativeViewAccessible() {
519 if (web_contents_) {
520 WebContentsImpl* wci = static_cast<WebContentsImpl*>(web_contents_);
521 return wci->GetParentNativeViewAccessible();
523 return NULL;
525 #endif
527 WebContents* InterstitialPageImpl::web_contents() const {
528 return web_contents_;
531 RenderViewHostImpl* InterstitialPageImpl::CreateRenderViewHost() {
532 if (!enabled())
533 return NULL;
535 // Interstitial pages don't want to share the session storage so we mint a
536 // new one.
537 BrowserContext* browser_context = web_contents()->GetBrowserContext();
538 scoped_refptr<SiteInstance> site_instance =
539 SiteInstance::Create(browser_context);
540 DOMStorageContextWrapper* dom_storage_context =
541 static_cast<DOMStorageContextWrapper*>(
542 BrowserContext::GetStoragePartition(
543 browser_context, site_instance.get())->GetDOMStorageContext());
544 session_storage_namespace_ =
545 new SessionStorageNamespaceImpl(dom_storage_context);
547 // Use the RenderViewHost from our FrameTree.
548 frame_tree_.root()->render_manager()->Init(
549 browser_context, site_instance.get(), MSG_ROUTING_NONE, MSG_ROUTING_NONE);
550 return frame_tree_.root()->current_frame_host()->render_view_host();
553 WebContentsView* InterstitialPageImpl::CreateWebContentsView() {
554 if (!enabled() || !create_view_)
555 return NULL;
556 WebContentsView* wcv =
557 static_cast<WebContentsImpl*>(web_contents())->GetView();
558 RenderWidgetHostViewBase* view =
559 wcv->CreateViewForWidget(render_view_host_, false);
560 render_view_host_->SetView(view);
561 render_view_host_->AllowBindings(BINDINGS_POLICY_DOM_AUTOMATION);
563 int32 max_page_id = web_contents()->
564 GetMaxPageIDForSiteInstance(render_view_host_->GetSiteInstance());
565 render_view_host_->CreateRenderView(base::string16(),
566 MSG_ROUTING_NONE,
567 MSG_ROUTING_NONE,
568 max_page_id,
569 FrameReplicationState(),
570 false);
571 controller_->delegate()->RenderFrameForInterstitialPageCreated(
572 frame_tree_.root()->current_frame_host());
573 view->SetSize(web_contents()->GetContainerBounds().size());
574 // Don't show the interstitial until we have navigated to it.
575 view->Hide();
576 return wcv;
579 void InterstitialPageImpl::Proceed() {
580 // Don't repeat this if we are already shutting down. We cannot check for
581 // enabled() here, because we may have called Disable without calling Hide.
582 if (!render_view_host_)
583 return;
585 if (action_taken_ != NO_ACTION) {
586 NOTREACHED();
587 return;
589 Disable();
590 action_taken_ = PROCEED_ACTION;
592 // Resumes the throbber, if applicable.
593 if (web_contents_was_loading_)
594 controller_->delegate()->SetIsLoading(true, true, NULL);
596 // If this is a new navigation, the old page is going away, so we cancel any
597 // blocked requests for it. If it is not a new navigation, then it means the
598 // interstitial was shown as a result of a resource loading in the page.
599 // Since the user wants to proceed, we'll let any blocked request go through.
600 if (new_navigation_)
601 TakeActionOnResourceDispatcher(CANCEL);
602 else
603 TakeActionOnResourceDispatcher(RESUME);
605 // No need to hide if we are a new navigation, we'll get hidden when the
606 // navigation is committed.
607 if (!new_navigation_) {
608 Hide();
609 delegate_->OnProceed();
610 return;
613 delegate_->OnProceed();
616 void InterstitialPageImpl::DontProceed() {
617 // Don't repeat this if we are already shutting down. We cannot check for
618 // enabled() here, because we may have called Disable without calling Hide.
619 if (!render_view_host_)
620 return;
621 DCHECK(action_taken_ != DONT_PROCEED_ACTION);
623 Disable();
624 action_taken_ = DONT_PROCEED_ACTION;
626 // If this is a new navigation, we are returning to the original page, so we
627 // resume blocked requests for it. If it is not a new navigation, then it
628 // means the interstitial was shown as a result of a resource loading in the
629 // page and we won't return to the original page, so we cancel blocked
630 // requests in that case.
631 if (new_navigation_)
632 TakeActionOnResourceDispatcher(RESUME);
633 else
634 TakeActionOnResourceDispatcher(CANCEL);
636 if (should_discard_pending_nav_entry_) {
637 // Since no navigation happens we have to discard the transient entry
638 // explicitely. Note that by calling DiscardNonCommittedEntries() we also
639 // discard the pending entry, which is what we want, since the navigation is
640 // cancelled.
641 controller_->DiscardNonCommittedEntries();
644 if (reload_on_dont_proceed_)
645 controller_->Reload(true);
647 Hide();
648 delegate_->OnDontProceed();
651 void InterstitialPageImpl::CancelForNavigation() {
652 // The user is trying to navigate away. We should unblock the renderer and
653 // disable the interstitial, but keep it visible until the navigation
654 // completes.
655 Disable();
656 // If this interstitial was shown for a new navigation, allow any navigations
657 // on the original page to resume (e.g., subresource requests, XHRs, etc).
658 // Otherwise, cancel the pending, possibly dangerous navigations.
659 if (new_navigation_)
660 TakeActionOnResourceDispatcher(RESUME);
661 else
662 TakeActionOnResourceDispatcher(CANCEL);
665 void InterstitialPageImpl::SetSize(const gfx::Size& size) {
666 if (!enabled())
667 return;
668 #if !defined(OS_MACOSX)
669 // When a tab is closed, we might be resized after our view was NULLed
670 // (typically if there was an info-bar).
671 if (render_view_host_->GetView())
672 render_view_host_->GetView()->SetSize(size);
673 #else
674 // TODO(port): Does Mac need to SetSize?
675 NOTIMPLEMENTED();
676 #endif
679 void InterstitialPageImpl::Focus() {
680 // Focus the native window.
681 if (!enabled())
682 return;
683 render_view_host_->GetView()->Focus();
686 void InterstitialPageImpl::FocusThroughTabTraversal(bool reverse) {
687 if (!enabled())
688 return;
689 render_view_host_->SetInitialFocus(reverse);
692 RenderWidgetHostView* InterstitialPageImpl::GetView() {
693 return render_view_host_->GetView();
696 RenderFrameHost* InterstitialPageImpl::GetMainFrame() const {
697 return render_view_host_->GetMainFrame();
700 InterstitialPageDelegate* InterstitialPageImpl::GetDelegateForTesting() {
701 return delegate_.get();
704 void InterstitialPageImpl::DontCreateViewForTesting() {
705 create_view_ = false;
708 gfx::Rect InterstitialPageImpl::GetRootWindowResizerRect() const {
709 return gfx::Rect();
712 void InterstitialPageImpl::CreateNewWindow(
713 int render_process_id,
714 int route_id,
715 int main_frame_route_id,
716 const ViewHostMsg_CreateWindow_Params& params,
717 SessionStorageNamespace* session_storage_namespace) {
718 NOTREACHED() << "InterstitialPage does not support showing popups yet.";
721 void InterstitialPageImpl::CreateNewWidget(int render_process_id,
722 int route_id,
723 blink::WebPopupType popup_type) {
724 NOTREACHED() << "InterstitialPage does not support showing drop-downs yet.";
727 void InterstitialPageImpl::CreateNewFullscreenWidget(int render_process_id,
728 int route_id) {
729 NOTREACHED()
730 << "InterstitialPage does not support showing full screen popups.";
733 void InterstitialPageImpl::ShowCreatedWindow(int route_id,
734 WindowOpenDisposition disposition,
735 const gfx::Rect& initial_rect,
736 bool user_gesture) {
737 NOTREACHED() << "InterstitialPage does not support showing popups yet.";
740 void InterstitialPageImpl::ShowCreatedWidget(int route_id,
741 const gfx::Rect& initial_rect) {
742 NOTREACHED() << "InterstitialPage does not support showing drop-downs yet.";
745 void InterstitialPageImpl::ShowCreatedFullscreenWidget(int route_id) {
746 NOTREACHED()
747 << "InterstitialPage does not support showing full screen popups.";
750 SessionStorageNamespace* InterstitialPageImpl::GetSessionStorageNamespace(
751 SiteInstance* instance) {
752 return session_storage_namespace_.get();
755 FrameTree* InterstitialPageImpl::GetFrameTree() {
756 return &frame_tree_;
759 void InterstitialPageImpl::Disable() {
760 enabled_ = false;
763 void InterstitialPageImpl::Shutdown() {
764 delete this;
767 void InterstitialPageImpl::OnNavigatingAwayOrTabClosing() {
768 if (action_taken_ == NO_ACTION) {
769 // We are navigating away from the interstitial or closing a tab with an
770 // interstitial. Default to DontProceed(). We don't just call Hide as
771 // subclasses will almost certainly override DontProceed to do some work
772 // (ex: close pending connections).
773 DontProceed();
774 } else {
775 // User decided to proceed and either the navigation was committed or
776 // the tab was closed before that.
777 Hide();
781 void InterstitialPageImpl::TakeActionOnResourceDispatcher(
782 ResourceRequestAction action) {
783 DCHECK_CURRENTLY_ON(BrowserThread::UI);
785 if (action == CANCEL || action == RESUME) {
786 if (resource_dispatcher_host_notified_)
787 return;
788 resource_dispatcher_host_notified_ = true;
791 // The tab might not have a render_view_host if it was closed (in which case,
792 // we have taken care of the blocked requests when processing
793 // NOTIFY_RENDER_WIDGET_HOST_DESTROYED.
794 // Also we need to test there is a ResourceDispatcherHostImpl, as when unit-
795 // tests we don't have one.
796 RenderViewHostImpl* rvh = RenderViewHostImpl::FromID(original_child_id_,
797 original_rvh_id_);
798 if (!rvh || !ResourceDispatcherHostImpl::Get())
799 return;
801 BrowserThread::PostTask(
802 BrowserThread::IO,
803 FROM_HERE,
804 base::Bind(
805 &ResourceRequestHelper,
806 ResourceDispatcherHostImpl::Get(),
807 original_child_id_,
808 original_rvh_id_,
809 action));
812 void InterstitialPageImpl::OnDomOperationResponse(
813 const std::string& json_string,
814 int automation_id) {
815 // Needed by test code.
816 DomOperationNotificationDetails details(json_string, automation_id);
817 NotificationService::current()->Notify(
818 NOTIFICATION_DOM_OPERATION_RESPONSE,
819 Source<WebContents>(web_contents()),
820 Details<DomOperationNotificationDetails>(&details));
822 if (!enabled())
823 return;
824 delegate_->CommandReceived(details.json);
828 InterstitialPageImpl::InterstitialPageRVHDelegateView::
829 InterstitialPageRVHDelegateView(InterstitialPageImpl* page)
830 : interstitial_page_(page) {
833 #if defined(OS_MACOSX) || defined(OS_ANDROID)
834 void InterstitialPageImpl::InterstitialPageRVHDelegateView::ShowPopupMenu(
835 RenderFrameHost* render_frame_host,
836 const gfx::Rect& bounds,
837 int item_height,
838 double item_font_size,
839 int selected_item,
840 const std::vector<MenuItem>& items,
841 bool right_aligned,
842 bool allow_multiple_selection) {
843 NOTREACHED() << "InterstitialPage does not support showing popup menus.";
846 void InterstitialPageImpl::InterstitialPageRVHDelegateView::HidePopupMenu() {
847 NOTREACHED() << "InterstitialPage does not support showing popup menus.";
849 #endif
851 void InterstitialPageImpl::InterstitialPageRVHDelegateView::StartDragging(
852 const DropData& drop_data,
853 WebDragOperationsMask allowed_operations,
854 const gfx::ImageSkia& image,
855 const gfx::Vector2d& image_offset,
856 const DragEventSourceInfo& event_info) {
857 interstitial_page_->render_view_host_->DragSourceSystemDragEnded();
858 DVLOG(1) << "InterstitialPage does not support dragging yet.";
861 void InterstitialPageImpl::InterstitialPageRVHDelegateView::UpdateDragCursor(
862 WebDragOperation) {
863 NOTREACHED() << "InterstitialPage does not support dragging yet.";
866 void InterstitialPageImpl::InterstitialPageRVHDelegateView::GotFocus() {
867 WebContents* web_contents = interstitial_page_->web_contents();
868 if (web_contents)
869 static_cast<WebContentsImpl*>(web_contents)->NotifyWebContentsFocused();
872 void InterstitialPageImpl::InterstitialPageRVHDelegateView::TakeFocus(
873 bool reverse) {
874 if (!interstitial_page_->web_contents())
875 return;
876 WebContentsImpl* web_contents =
877 static_cast<WebContentsImpl*>(interstitial_page_->web_contents());
878 if (!web_contents->GetDelegateView())
879 return;
881 web_contents->GetDelegateView()->TakeFocus(reverse);
884 void InterstitialPageImpl::InterstitialPageRVHDelegateView::OnFindReply(
885 int request_id, int number_of_matches, const gfx::Rect& selection_rect,
886 int active_match_ordinal, bool final_update) {
889 InterstitialPageImpl::UnderlyingContentObserver::UnderlyingContentObserver(
890 WebContents* web_contents,
891 InterstitialPageImpl* interstitial)
892 : WebContentsObserver(web_contents), interstitial_(interstitial) {
895 void InterstitialPageImpl::UnderlyingContentObserver::NavigationEntryCommitted(
896 const LoadCommittedDetails& load_details) {
897 interstitial_->OnNavigatingAwayOrTabClosing();
900 void InterstitialPageImpl::UnderlyingContentObserver::WebContentsDestroyed() {
901 interstitial_->OnNavigatingAwayOrTabClosing();
904 } // namespace content