Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / content / browser / frame_host / interstitial_page_impl.cc
blob71c70564b8d8a4ceb0ca3713a58bf2175aa6a167
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/message_loop/message_loop.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/threading/thread.h"
15 #include "content/browser/dom_storage/dom_storage_context_wrapper.h"
16 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
17 #include "content/browser/frame_host/interstitial_page_navigator_impl.h"
18 #include "content/browser/frame_host/navigation_controller_impl.h"
19 #include "content/browser/frame_host/navigation_entry_impl.h"
20 #include "content/browser/loader/resource_dispatcher_host_impl.h"
21 #include "content/browser/renderer_host/render_process_host_impl.h"
22 #include "content/browser/renderer_host/render_view_host_delegate_view.h"
23 #include "content/browser/renderer_host/render_view_host_factory.h"
24 #include "content/browser/renderer_host/render_view_host_impl.h"
25 #include "content/browser/renderer_host/render_widget_host_view_base.h"
26 #include "content/browser/site_instance_impl.h"
27 #include "content/browser/web_contents/web_contents_impl.h"
28 #include "content/browser/web_contents/web_contents_view.h"
29 #include "content/common/frame_messages.h"
30 #include "content/common/view_messages.h"
31 #include "content/public/browser/browser_context.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/content_browser_client.h"
34 #include "content/public/browser/dom_operation_notification_details.h"
35 #include "content/public/browser/interstitial_page_delegate.h"
36 #include "content/public/browser/invalidate_type.h"
37 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/notification_source.h"
39 #include "content/public/browser/storage_partition.h"
40 #include "content/public/browser/user_metrics.h"
41 #include "content/public/browser/web_contents_delegate.h"
42 #include "content/public/common/bindings_policy.h"
43 #include "content/public/common/page_transition_types.h"
44 #include "net/base/escape.h"
45 #include "net/url_request/url_request_context_getter.h"
47 using blink::WebDragOperation;
48 using blink::WebDragOperationsMask;
50 namespace content {
51 namespace {
53 void ResourceRequestHelper(ResourceDispatcherHostImpl* rdh,
54 int process_id,
55 int render_view_host_id,
56 ResourceRequestAction action) {
57 switch (action) {
58 case BLOCK:
59 rdh->BlockRequestsForRoute(process_id, render_view_host_id);
60 break;
61 case RESUME:
62 rdh->ResumeBlockedRequestsForRoute(process_id, render_view_host_id);
63 break;
64 case CANCEL:
65 rdh->CancelBlockedRequestsForRoute(process_id, render_view_host_id);
66 break;
67 default:
68 NOTREACHED();
72 } // namespace
74 class InterstitialPageImpl::InterstitialPageRVHDelegateView
75 : public RenderViewHostDelegateView {
76 public:
77 explicit InterstitialPageRVHDelegateView(InterstitialPageImpl* page);
79 // RenderViewHostDelegateView implementation:
80 #if defined(OS_MACOSX) || defined(OS_ANDROID)
81 virtual void ShowPopupMenu(const gfx::Rect& bounds,
82 int item_height,
83 double item_font_size,
84 int selected_item,
85 const std::vector<MenuItem>& items,
86 bool right_aligned,
87 bool allow_multiple_selection) OVERRIDE;
88 virtual void HidePopupMenu() OVERRIDE;
89 #endif
90 virtual void StartDragging(const DropData& drop_data,
91 WebDragOperationsMask operations_allowed,
92 const gfx::ImageSkia& image,
93 const gfx::Vector2d& image_offset,
94 const DragEventSourceInfo& event_info) OVERRIDE;
95 virtual void UpdateDragCursor(WebDragOperation operation) OVERRIDE;
96 virtual void GotFocus() OVERRIDE;
97 virtual void TakeFocus(bool reverse) OVERRIDE;
98 virtual void OnFindReply(int request_id,
99 int number_of_matches,
100 const gfx::Rect& selection_rect,
101 int active_match_ordinal,
102 bool final_update);
104 private:
105 InterstitialPageImpl* interstitial_page_;
107 DISALLOW_COPY_AND_ASSIGN(InterstitialPageRVHDelegateView);
111 // We keep a map of the various blocking pages shown as the UI tests need to
112 // be able to retrieve them.
113 typedef std::map<WebContents*, InterstitialPageImpl*> InterstitialPageMap;
114 static InterstitialPageMap* g_web_contents_to_interstitial_page;
116 // Initializes g_web_contents_to_interstitial_page in a thread-safe manner.
117 // Should be called before accessing g_web_contents_to_interstitial_page.
118 static void InitInterstitialPageMap() {
119 if (!g_web_contents_to_interstitial_page)
120 g_web_contents_to_interstitial_page = new InterstitialPageMap;
123 InterstitialPage* InterstitialPage::Create(WebContents* web_contents,
124 bool new_navigation,
125 const GURL& url,
126 InterstitialPageDelegate* delegate) {
127 return new InterstitialPageImpl(
128 web_contents,
129 static_cast<RenderWidgetHostDelegate*>(
130 static_cast<WebContentsImpl*>(web_contents)),
131 new_navigation, url, delegate);
134 InterstitialPage* InterstitialPage::GetInterstitialPage(
135 WebContents* web_contents) {
136 InitInterstitialPageMap();
137 InterstitialPageMap::const_iterator iter =
138 g_web_contents_to_interstitial_page->find(web_contents);
139 if (iter == g_web_contents_to_interstitial_page->end())
140 return NULL;
142 return iter->second;
145 InterstitialPageImpl::InterstitialPageImpl(
146 WebContents* web_contents,
147 RenderWidgetHostDelegate* render_widget_host_delegate,
148 bool new_navigation,
149 const GURL& url,
150 InterstitialPageDelegate* delegate)
151 : WebContentsObserver(web_contents),
152 web_contents_(web_contents),
153 controller_(static_cast<NavigationControllerImpl*>(
154 &web_contents->GetController())),
155 render_widget_host_delegate_(render_widget_host_delegate),
156 url_(url),
157 new_navigation_(new_navigation),
158 should_discard_pending_nav_entry_(new_navigation),
159 reload_on_dont_proceed_(false),
160 enabled_(true),
161 action_taken_(NO_ACTION),
162 render_view_host_(NULL),
163 // TODO(nasko): The InterstitialPageImpl will need to provide its own
164 // NavigationControllerImpl to the Navigator, which is separate from
165 // the WebContents one, so we can enforce no navigation policy here.
166 // While we get the code to a point to do this, pass NULL for it.
167 // TODO(creis): We will also need to pass delegates for the RVHM as we
168 // start to use it.
169 frame_tree_(new InterstitialPageNavigatorImpl(this, controller_),
170 this, this, this,
171 static_cast<WebContentsImpl*>(web_contents)),
172 original_child_id_(web_contents->GetRenderProcessHost()->GetID()),
173 original_rvh_id_(web_contents->GetRenderViewHost()->GetRoutingID()),
174 should_revert_web_contents_title_(false),
175 web_contents_was_loading_(false),
176 resource_dispatcher_host_notified_(false),
177 rvh_delegate_view_(new InterstitialPageRVHDelegateView(this)),
178 create_view_(true),
179 delegate_(delegate),
180 weak_ptr_factory_(this) {
181 InitInterstitialPageMap();
182 // It would be inconsistent to create an interstitial with no new navigation
183 // (which is the case when the interstitial was triggered by a sub-resource on
184 // a page) when we have a pending entry (in the process of loading a new top
185 // frame).
186 DCHECK(new_navigation || !web_contents->GetController().GetPendingEntry());
189 InterstitialPageImpl::~InterstitialPageImpl() {
192 void InterstitialPageImpl::Show() {
193 if (!enabled())
194 return;
196 // If an interstitial is already showing or about to be shown, close it before
197 // showing the new one.
198 // Be careful not to take an action on the old interstitial more than once.
199 InterstitialPageMap::const_iterator iter =
200 g_web_contents_to_interstitial_page->find(web_contents_);
201 if (iter != g_web_contents_to_interstitial_page->end()) {
202 InterstitialPageImpl* interstitial = iter->second;
203 if (interstitial->action_taken_ != NO_ACTION) {
204 interstitial->Hide();
205 } else {
206 // If we are currently showing an interstitial page for which we created
207 // a transient entry and a new interstitial is shown as the result of a
208 // new browser initiated navigation, then that transient entry has already
209 // been discarded and a new pending navigation entry created.
210 // So we should not discard that new pending navigation entry.
211 // See http://crbug.com/9791
212 if (new_navigation_ && interstitial->new_navigation_)
213 interstitial->should_discard_pending_nav_entry_= false;
214 interstitial->DontProceed();
218 // Block the resource requests for the render view host while it is hidden.
219 TakeActionOnResourceDispatcher(BLOCK);
220 // We need to be notified when the RenderViewHost is destroyed so we can
221 // cancel the blocked requests. We cannot do that on
222 // NOTIFY_WEB_CONTENTS_DESTROYED as at that point the RenderViewHost has
223 // already been destroyed.
224 notification_registrar_.Add(
225 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
226 Source<RenderWidgetHost>(controller_->delegate()->GetRenderViewHost()));
228 // Update the g_web_contents_to_interstitial_page map.
229 iter = g_web_contents_to_interstitial_page->find(web_contents_);
230 DCHECK(iter == g_web_contents_to_interstitial_page->end());
231 (*g_web_contents_to_interstitial_page)[web_contents_] = this;
233 if (new_navigation_) {
234 NavigationEntryImpl* entry = new NavigationEntryImpl;
235 entry->SetURL(url_);
236 entry->SetVirtualURL(url_);
237 entry->set_page_type(PAGE_TYPE_INTERSTITIAL);
239 // Give delegates a chance to set some states on the navigation entry.
240 delegate_->OverrideEntry(entry);
242 controller_->SetTransientEntry(entry);
245 DCHECK(!render_view_host_);
246 render_view_host_ = static_cast<RenderViewHostImpl*>(CreateRenderViewHost());
247 render_view_host_->AttachToFrameTree();
248 CreateWebContentsView();
250 std::string data_url = "data:text/html;charset=utf-8," +
251 net::EscapePath(delegate_->GetHTMLContents());
252 render_view_host_->NavigateToURL(GURL(data_url));
254 notification_registrar_.Add(this, NOTIFICATION_NAV_ENTRY_PENDING,
255 Source<NavigationController>(controller_));
258 void InterstitialPageImpl::Hide() {
259 // We may have already been hidden, and are just waiting to be deleted.
260 // We can't check for enabled() here, because some callers have already
261 // called Disable.
262 if (!render_view_host_)
263 return;
265 Disable();
267 RenderWidgetHostView* old_view =
268 controller_->delegate()->GetRenderViewHost()->GetView();
269 if (controller_->delegate()->GetInterstitialPage() == this &&
270 old_view &&
271 !old_view->IsShowing() &&
272 !controller_->delegate()->IsHidden()) {
273 // Show the original RVH since we're going away. Note it might not exist if
274 // the renderer crashed while the interstitial was showing.
275 // Note that it is important that we don't call Show() if the view is
276 // already showing. That would result in bad things (unparented HWND on
277 // Windows for example) happening.
278 old_view->Show();
281 // If the focus was on the interstitial, let's keep it to the page.
282 // (Note that in unit-tests the RVH may not have a view).
283 if (render_view_host_->GetView() &&
284 render_view_host_->GetView()->HasFocus() &&
285 controller_->delegate()->GetRenderViewHost()->GetView()) {
286 controller_->delegate()->GetRenderViewHost()->Focus();
289 // Delete this and call Shutdown on the RVH asynchronously, as we may have
290 // been called from a RVH delegate method, and we can't delete the RVH out
291 // from under itself.
292 base::MessageLoop::current()->PostNonNestableTask(
293 FROM_HERE,
294 base::Bind(&InterstitialPageImpl::Shutdown,
295 weak_ptr_factory_.GetWeakPtr()));
296 render_view_host_ = NULL;
297 frame_tree_.ResetForMainFrameSwap();
298 controller_->delegate()->DetachInterstitialPage();
299 // Let's revert to the original title if necessary.
300 NavigationEntry* entry = controller_->GetVisibleEntry();
301 if (!new_navigation_ && should_revert_web_contents_title_) {
302 entry->SetTitle(original_web_contents_title_);
303 controller_->delegate()->NotifyNavigationStateChanged(
304 INVALIDATE_TYPE_TITLE);
307 InterstitialPageMap::iterator iter =
308 g_web_contents_to_interstitial_page->find(web_contents_);
309 DCHECK(iter != g_web_contents_to_interstitial_page->end());
310 if (iter != g_web_contents_to_interstitial_page->end())
311 g_web_contents_to_interstitial_page->erase(iter);
313 // Clear the WebContents pointer, because it may now be deleted.
314 // This signifies that we are in the process of shutting down.
315 web_contents_ = NULL;
318 void InterstitialPageImpl::Observe(
319 int type,
320 const NotificationSource& source,
321 const NotificationDetails& details) {
322 switch (type) {
323 case NOTIFICATION_NAV_ENTRY_PENDING:
324 // We are navigating away from the interstitial (the user has typed a URL
325 // in the location bar or clicked a bookmark). Make sure clicking on the
326 // interstitial will have no effect. Also cancel any blocked requests
327 // on the ResourceDispatcherHost. Note that when we get this notification
328 // the RenderViewHost has not yet navigated so we'll unblock the
329 // RenderViewHost before the resource request for the new page we are
330 // navigating arrives in the ResourceDispatcherHost. This ensures that
331 // request won't be blocked if the same RenderViewHost was used for the
332 // new navigation.
333 Disable();
334 TakeActionOnResourceDispatcher(CANCEL);
335 break;
336 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED:
337 if (action_taken_ == NO_ACTION) {
338 // The RenderViewHost is being destroyed (as part of the tab being
339 // closed); make sure we clear the blocked requests.
340 RenderViewHost* rvh = static_cast<RenderViewHost*>(
341 static_cast<RenderViewHostImpl*>(
342 RenderWidgetHostImpl::From(
343 Source<RenderWidgetHost>(source).ptr())));
344 DCHECK(rvh->GetProcess()->GetID() == original_child_id_ &&
345 rvh->GetRoutingID() == original_rvh_id_);
346 TakeActionOnResourceDispatcher(CANCEL);
348 break;
349 default:
350 NOTREACHED();
354 void InterstitialPageImpl::NavigationEntryCommitted(
355 const LoadCommittedDetails& load_details) {
356 OnNavigatingAwayOrTabClosing();
359 void InterstitialPageImpl::WebContentsWillBeDestroyed() {
360 OnNavigatingAwayOrTabClosing();
363 void InterstitialPageImpl::WebContentsDestroyed(WebContents* web_contents) {
364 // WebContentsImpl will only call WebContentsWillBeDestroyed for interstitial
365 // pages that it knows about, pages that called
366 // WebContentsImpl::AttachInterstitialPage. But it's possible to have an
367 // interstitial page that never progressed that far. In that case, ensure that
368 // this interstitial page is destroyed. (And if it was attached, and
369 // OnNavigatingAwayOrTabClosing was called, it's safe to call
370 // OnNavigatingAwayOrTabClosing twice.)
371 OnNavigatingAwayOrTabClosing();
374 bool InterstitialPageImpl::OnMessageReceived(RenderFrameHost* render_frame_host,
375 const IPC::Message& message) {
376 return OnMessageReceived(message);
379 bool InterstitialPageImpl::OnMessageReceived(RenderViewHost* render_view_host,
380 const IPC::Message& message) {
381 return OnMessageReceived(message);
384 bool InterstitialPageImpl::OnMessageReceived(const IPC::Message& message) {
386 bool handled = true;
387 bool message_is_ok = true;
388 IPC_BEGIN_MESSAGE_MAP_EX(InterstitialPageImpl, message, message_is_ok)
389 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
390 OnDomOperationResponse)
391 IPC_MESSAGE_UNHANDLED(handled = false)
392 IPC_END_MESSAGE_MAP_EX()
394 if (!message_is_ok) {
395 RecordAction(base::UserMetricsAction("BadMessageTerminate_RVD"));
396 web_contents()->GetRenderProcessHost()->ReceivedBadMessage();
399 return handled;
402 void InterstitialPageImpl::RenderFrameCreated(
403 RenderFrameHost* render_frame_host) {
404 // Note this is only for subframes in the interstitial, the notification for
405 // the main frame happens in RenderViewCreated.
406 controller_->delegate()->RenderFrameForInterstitialPageCreated(
407 render_frame_host);
410 RenderViewHostDelegateView* InterstitialPageImpl::GetDelegateView() {
411 return rvh_delegate_view_.get();
414 const GURL& InterstitialPageImpl::GetMainFrameLastCommittedURL() const {
415 return url_;
418 void InterstitialPageImpl::RenderViewTerminated(
419 RenderViewHost* render_view_host,
420 base::TerminationStatus status,
421 int error_code) {
422 // Our renderer died. This should not happen in normal cases.
423 // If we haven't already started shutdown, just dismiss the interstitial.
424 // We cannot check for enabled() here, because we may have called Disable
425 // without calling Hide.
426 if (render_view_host_)
427 DontProceed();
430 void InterstitialPageImpl::DidNavigate(
431 RenderViewHost* render_view_host,
432 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
433 // A fast user could have navigated away from the page that triggered the
434 // interstitial while the interstitial was loading, that would have disabled
435 // us. In that case we can dismiss ourselves.
436 if (!enabled()) {
437 DontProceed();
438 return;
440 if (PageTransitionCoreTypeIs(params.transition,
441 PAGE_TRANSITION_AUTO_SUBFRAME)) {
442 // No need to handle navigate message from iframe in the interstitial page.
443 return;
446 // The RenderViewHost has loaded its contents, we can show it now.
447 if (!controller_->delegate()->IsHidden())
448 render_view_host_->GetView()->Show();
449 controller_->delegate()->AttachInterstitialPage(this);
451 RenderWidgetHostView* rwh_view =
452 controller_->delegate()->GetRenderViewHost()->GetView();
454 // The RenderViewHost may already have crashed before we even get here.
455 if (rwh_view) {
456 // If the page has focus, focus the interstitial.
457 if (rwh_view->HasFocus())
458 Focus();
460 // Hide the original RVH since we're showing the interstitial instead.
461 rwh_view->Hide();
464 // Notify the tab we are not loading so the throbber is stopped. It also
465 // causes a WebContentsObserver::DidStopLoading callback that the
466 // AutomationProvider (used by the UI tests) expects to consider a navigation
467 // as complete. Without this, navigating in a UI test to a URL that triggers
468 // an interstitial would hang.
469 web_contents_was_loading_ = controller_->delegate()->IsLoading();
470 controller_->delegate()->SetIsLoading(
471 controller_->delegate()->GetRenderViewHost(), false, true, NULL);
474 void InterstitialPageImpl::UpdateTitle(
475 RenderViewHost* render_view_host,
476 int32 page_id,
477 const base::string16& title,
478 base::i18n::TextDirection title_direction) {
479 if (!enabled())
480 return;
482 DCHECK(render_view_host == render_view_host_);
483 NavigationEntry* entry = controller_->GetVisibleEntry();
484 if (!entry) {
485 // Crash reports from the field indicate this can be NULL.
486 // This is unexpected as InterstitialPages constructed with the
487 // new_navigation flag set to true create a transient navigation entry
488 // (that is returned as the active entry). And the only case so far of
489 // interstitial created with that flag set to false is with the
490 // SafeBrowsingBlockingPage, when the resource triggering the interstitial
491 // is a sub-resource, meaning the main page has already been loaded and a
492 // navigation entry should have been created.
493 NOTREACHED();
494 return;
497 // If this interstitial is shown on an existing navigation entry, we'll need
498 // to remember its title so we can revert to it when hidden.
499 if (!new_navigation_ && !should_revert_web_contents_title_) {
500 original_web_contents_title_ = entry->GetTitle();
501 should_revert_web_contents_title_ = true;
503 // TODO(evan): make use of title_direction.
504 // http://code.google.com/p/chromium/issues/detail?id=27094
505 entry->SetTitle(title);
506 controller_->delegate()->NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
509 RendererPreferences InterstitialPageImpl::GetRendererPrefs(
510 BrowserContext* browser_context) const {
511 delegate_->OverrideRendererPrefs(&renderer_preferences_);
512 return renderer_preferences_;
515 WebPreferences InterstitialPageImpl::GetWebkitPrefs() {
516 if (!enabled())
517 return WebPreferences();
519 return render_view_host_->GetWebkitPrefs(url_);
522 void InterstitialPageImpl::RenderWidgetDeleted(
523 RenderWidgetHostImpl* render_widget_host) {
524 // TODO(creis): Remove this method once we verify the shutdown path is sane.
525 CHECK(!web_contents_);
528 bool InterstitialPageImpl::PreHandleKeyboardEvent(
529 const NativeWebKeyboardEvent& event,
530 bool* is_keyboard_shortcut) {
531 if (!enabled())
532 return false;
533 return render_widget_host_delegate_->PreHandleKeyboardEvent(
534 event, is_keyboard_shortcut);
537 void InterstitialPageImpl::HandleKeyboardEvent(
538 const NativeWebKeyboardEvent& event) {
539 if (enabled())
540 render_widget_host_delegate_->HandleKeyboardEvent(event);
543 #if defined(OS_WIN)
544 gfx::NativeViewAccessible
545 InterstitialPageImpl::GetParentNativeViewAccessible() {
546 return render_widget_host_delegate_->GetParentNativeViewAccessible();
548 #endif
550 WebContents* InterstitialPageImpl::web_contents() const {
551 return web_contents_;
554 RenderViewHost* InterstitialPageImpl::CreateRenderViewHost() {
555 if (!enabled())
556 return NULL;
558 // Interstitial pages don't want to share the session storage so we mint a
559 // new one.
560 BrowserContext* browser_context = web_contents()->GetBrowserContext();
561 scoped_refptr<SiteInstance> site_instance =
562 SiteInstance::Create(browser_context);
563 DOMStorageContextWrapper* dom_storage_context =
564 static_cast<DOMStorageContextWrapper*>(
565 BrowserContext::GetStoragePartition(
566 browser_context, site_instance.get())->GetDOMStorageContext());
567 session_storage_namespace_ =
568 new SessionStorageNamespaceImpl(dom_storage_context);
570 // Use the RenderViewHost from our FrameTree.
571 frame_tree_.root()->render_manager()->Init(
572 browser_context, site_instance.get(), MSG_ROUTING_NONE, MSG_ROUTING_NONE);
573 return frame_tree_.root()->current_frame_host()->render_view_host();
576 WebContentsView* InterstitialPageImpl::CreateWebContentsView() {
577 if (!enabled() || !create_view_)
578 return NULL;
579 WebContentsView* wcv =
580 static_cast<WebContentsImpl*>(web_contents())->GetView();
581 RenderWidgetHostViewBase* view =
582 wcv->CreateViewForWidget(render_view_host_);
583 render_view_host_->SetView(view);
584 render_view_host_->AllowBindings(BINDINGS_POLICY_DOM_AUTOMATION);
586 int32 max_page_id = web_contents()->
587 GetMaxPageIDForSiteInstance(render_view_host_->GetSiteInstance());
588 render_view_host_->CreateRenderView(base::string16(),
589 MSG_ROUTING_NONE,
590 max_page_id,
591 false);
592 controller_->delegate()->RenderFrameForInterstitialPageCreated(
593 frame_tree_.root()->current_frame_host());
594 view->SetSize(web_contents()->GetContainerBounds().size());
595 // Don't show the interstitial until we have navigated to it.
596 view->Hide();
597 return wcv;
600 void InterstitialPageImpl::Proceed() {
601 // Don't repeat this if we are already shutting down. We cannot check for
602 // enabled() here, because we may have called Disable without calling Hide.
603 if (!render_view_host_)
604 return;
606 if (action_taken_ != NO_ACTION) {
607 NOTREACHED();
608 return;
610 Disable();
611 action_taken_ = PROCEED_ACTION;
613 // Resumes the throbber, if applicable.
614 if (web_contents_was_loading_)
615 controller_->delegate()->SetIsLoading(
616 controller_->delegate()->GetRenderViewHost(), true, true, NULL);
618 // If this is a new navigation, the old page is going away, so we cancel any
619 // blocked requests for it. If it is not a new navigation, then it means the
620 // interstitial was shown as a result of a resource loading in the page.
621 // Since the user wants to proceed, we'll let any blocked request go through.
622 if (new_navigation_)
623 TakeActionOnResourceDispatcher(CANCEL);
624 else
625 TakeActionOnResourceDispatcher(RESUME);
627 // No need to hide if we are a new navigation, we'll get hidden when the
628 // navigation is committed.
629 if (!new_navigation_) {
630 Hide();
631 delegate_->OnProceed();
632 return;
635 delegate_->OnProceed();
638 void InterstitialPageImpl::DontProceed() {
639 // Don't repeat this if we are already shutting down. We cannot check for
640 // enabled() here, because we may have called Disable without calling Hide.
641 if (!render_view_host_)
642 return;
643 DCHECK(action_taken_ != DONT_PROCEED_ACTION);
645 Disable();
646 action_taken_ = DONT_PROCEED_ACTION;
648 // If this is a new navigation, we are returning to the original page, so we
649 // resume blocked requests for it. If it is not a new navigation, then it
650 // means the interstitial was shown as a result of a resource loading in the
651 // page and we won't return to the original page, so we cancel blocked
652 // requests in that case.
653 if (new_navigation_)
654 TakeActionOnResourceDispatcher(RESUME);
655 else
656 TakeActionOnResourceDispatcher(CANCEL);
658 if (should_discard_pending_nav_entry_) {
659 // Since no navigation happens we have to discard the transient entry
660 // explicitely. Note that by calling DiscardNonCommittedEntries() we also
661 // discard the pending entry, which is what we want, since the navigation is
662 // cancelled.
663 controller_->DiscardNonCommittedEntries();
666 if (reload_on_dont_proceed_)
667 controller_->Reload(true);
669 Hide();
670 delegate_->OnDontProceed();
673 void InterstitialPageImpl::CancelForNavigation() {
674 // The user is trying to navigate away. We should unblock the renderer and
675 // disable the interstitial, but keep it visible until the navigation
676 // completes.
677 Disable();
678 // If this interstitial was shown for a new navigation, allow any navigations
679 // on the original page to resume (e.g., subresource requests, XHRs, etc).
680 // Otherwise, cancel the pending, possibly dangerous navigations.
681 if (new_navigation_)
682 TakeActionOnResourceDispatcher(RESUME);
683 else
684 TakeActionOnResourceDispatcher(CANCEL);
687 void InterstitialPageImpl::SetSize(const gfx::Size& size) {
688 if (!enabled())
689 return;
690 #if !defined(OS_MACOSX)
691 // When a tab is closed, we might be resized after our view was NULLed
692 // (typically if there was an info-bar).
693 if (render_view_host_->GetView())
694 render_view_host_->GetView()->SetSize(size);
695 #else
696 // TODO(port): Does Mac need to SetSize?
697 NOTIMPLEMENTED();
698 #endif
701 void InterstitialPageImpl::Focus() {
702 // Focus the native window.
703 if (!enabled())
704 return;
705 render_view_host_->Focus();
708 void InterstitialPageImpl::FocusThroughTabTraversal(bool reverse) {
709 if (!enabled())
710 return;
711 render_view_host_->SetInitialFocus(reverse);
714 RenderWidgetHostView* InterstitialPageImpl::GetView() {
715 return render_view_host_->GetView();
718 RenderViewHost* InterstitialPageImpl::GetRenderViewHostForTesting() const {
719 return render_view_host_;
722 #if defined(OS_ANDROID)
723 RenderViewHost* InterstitialPageImpl::GetRenderViewHost() const {
724 return render_view_host_;
726 #endif
728 InterstitialPageDelegate* InterstitialPageImpl::GetDelegateForTesting() {
729 return delegate_.get();
732 void InterstitialPageImpl::DontCreateViewForTesting() {
733 create_view_ = false;
736 gfx::Rect InterstitialPageImpl::GetRootWindowResizerRect() const {
737 return gfx::Rect();
740 void InterstitialPageImpl::CreateNewWindow(
741 int render_process_id,
742 int route_id,
743 int main_frame_route_id,
744 const ViewHostMsg_CreateWindow_Params& params,
745 SessionStorageNamespace* session_storage_namespace) {
746 NOTREACHED() << "InterstitialPage does not support showing popups yet.";
749 void InterstitialPageImpl::CreateNewWidget(int render_process_id,
750 int route_id,
751 blink::WebPopupType popup_type) {
752 NOTREACHED() << "InterstitialPage does not support showing drop-downs yet.";
755 void InterstitialPageImpl::CreateNewFullscreenWidget(int render_process_id,
756 int route_id) {
757 NOTREACHED()
758 << "InterstitialPage does not support showing full screen popups.";
761 void InterstitialPageImpl::ShowCreatedWindow(int route_id,
762 WindowOpenDisposition disposition,
763 const gfx::Rect& initial_pos,
764 bool user_gesture) {
765 NOTREACHED() << "InterstitialPage does not support showing popups yet.";
768 void InterstitialPageImpl::ShowCreatedWidget(int route_id,
769 const gfx::Rect& initial_pos) {
770 NOTREACHED() << "InterstitialPage does not support showing drop-downs yet.";
773 void InterstitialPageImpl::ShowCreatedFullscreenWidget(int route_id) {
774 NOTREACHED()
775 << "InterstitialPage does not support showing full screen popups.";
778 SessionStorageNamespace* InterstitialPageImpl::GetSessionStorageNamespace(
779 SiteInstance* instance) {
780 return session_storage_namespace_.get();
783 FrameTree* InterstitialPageImpl::GetFrameTree() {
784 return &frame_tree_;
787 void InterstitialPageImpl::Disable() {
788 enabled_ = false;
791 void InterstitialPageImpl::Shutdown() {
792 delete this;
795 void InterstitialPageImpl::OnNavigatingAwayOrTabClosing() {
796 if (action_taken_ == NO_ACTION) {
797 // We are navigating away from the interstitial or closing a tab with an
798 // interstitial. Default to DontProceed(). We don't just call Hide as
799 // subclasses will almost certainly override DontProceed to do some work
800 // (ex: close pending connections).
801 DontProceed();
802 } else {
803 // User decided to proceed and either the navigation was committed or
804 // the tab was closed before that.
805 Hide();
809 void InterstitialPageImpl::TakeActionOnResourceDispatcher(
810 ResourceRequestAction action) {
811 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)) <<
812 "TakeActionOnResourceDispatcher should be called on the main thread.";
814 if (action == CANCEL || action == RESUME) {
815 if (resource_dispatcher_host_notified_)
816 return;
817 resource_dispatcher_host_notified_ = true;
820 // The tab might not have a render_view_host if it was closed (in which case,
821 // we have taken care of the blocked requests when processing
822 // NOTIFY_RENDER_WIDGET_HOST_DESTROYED.
823 // Also we need to test there is a ResourceDispatcherHostImpl, as when unit-
824 // tests we don't have one.
825 RenderViewHostImpl* rvh = RenderViewHostImpl::FromID(original_child_id_,
826 original_rvh_id_);
827 if (!rvh || !ResourceDispatcherHostImpl::Get())
828 return;
830 BrowserThread::PostTask(
831 BrowserThread::IO,
832 FROM_HERE,
833 base::Bind(
834 &ResourceRequestHelper,
835 ResourceDispatcherHostImpl::Get(),
836 original_child_id_,
837 original_rvh_id_,
838 action));
841 void InterstitialPageImpl::OnDomOperationResponse(
842 const std::string& json_string,
843 int automation_id) {
844 // Needed by test code.
845 DomOperationNotificationDetails details(json_string, automation_id);
846 NotificationService::current()->Notify(
847 NOTIFICATION_DOM_OPERATION_RESPONSE,
848 Source<WebContents>(web_contents()),
849 Details<DomOperationNotificationDetails>(&details));
851 if (!enabled())
852 return;
853 delegate_->CommandReceived(details.json);
857 InterstitialPageImpl::InterstitialPageRVHDelegateView::
858 InterstitialPageRVHDelegateView(InterstitialPageImpl* page)
859 : interstitial_page_(page) {
862 #if defined(OS_MACOSX) || defined(OS_ANDROID)
863 void InterstitialPageImpl::InterstitialPageRVHDelegateView::ShowPopupMenu(
864 const gfx::Rect& bounds,
865 int item_height,
866 double item_font_size,
867 int selected_item,
868 const std::vector<MenuItem>& items,
869 bool right_aligned,
870 bool allow_multiple_selection) {
871 NOTREACHED() << "InterstitialPage does not support showing popup menus.";
874 void InterstitialPageImpl::InterstitialPageRVHDelegateView::HidePopupMenu() {
875 NOTREACHED() << "InterstitialPage does not support showing popup menus.";
877 #endif
879 void InterstitialPageImpl::InterstitialPageRVHDelegateView::StartDragging(
880 const DropData& drop_data,
881 WebDragOperationsMask allowed_operations,
882 const gfx::ImageSkia& image,
883 const gfx::Vector2d& image_offset,
884 const DragEventSourceInfo& event_info) {
885 interstitial_page_->render_view_host_->DragSourceSystemDragEnded();
886 DVLOG(1) << "InterstitialPage does not support dragging yet.";
889 void InterstitialPageImpl::InterstitialPageRVHDelegateView::UpdateDragCursor(
890 WebDragOperation) {
891 NOTREACHED() << "InterstitialPage does not support dragging yet.";
894 void InterstitialPageImpl::InterstitialPageRVHDelegateView::GotFocus() {
895 WebContents* web_contents = interstitial_page_->web_contents();
896 if (web_contents && web_contents->GetDelegate())
897 web_contents->GetDelegate()->WebContentsFocused(web_contents);
900 void InterstitialPageImpl::InterstitialPageRVHDelegateView::TakeFocus(
901 bool reverse) {
902 if (!interstitial_page_->web_contents())
903 return;
904 WebContentsImpl* web_contents =
905 static_cast<WebContentsImpl*>(interstitial_page_->web_contents());
906 if (!web_contents->GetDelegateView())
907 return;
909 web_contents->GetDelegateView()->TakeFocus(reverse);
912 void InterstitialPageImpl::InterstitialPageRVHDelegateView::OnFindReply(
913 int request_id, int number_of_matches, const gfx::Rect& selection_rect,
914 int active_match_ordinal, bool final_update) {
917 } // namespace content