Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / browser_navigator.cc
blob810c299c7f7fb1d678dabe31f75445691d3f8d41
1 // Copyright (c) 2012 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 "chrome/browser/ui/browser_navigator.h"
7 #include <algorithm>
9 #include "base/command_line.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_about_handler.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/prefs/incognito_mode_prefs.h"
16 #include "chrome/browser/prerender/prerender_manager.h"
17 #include "chrome/browser/prerender/prerender_manager_factory.h"
18 #include "chrome/browser/prerender/prerender_util.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/tab_contents/tab_util.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_finder.h"
23 #include "chrome/browser/ui/browser_instant_controller.h"
24 #include "chrome/browser/ui/browser_window.h"
25 #include "chrome/browser/ui/host_desktop.h"
26 #include "chrome/browser/ui/location_bar/location_bar.h"
27 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
28 #include "chrome/browser/ui/singleton_tabs.h"
29 #include "chrome/browser/ui/status_bubble.h"
30 #include "chrome/browser/ui/tab_helpers.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/pref_names.h"
33 #include "chrome/common/url_constants.h"
34 #include "components/google/core/browser/google_url_tracker.h"
35 #include "content/public/browser/browser_url_handler.h"
36 #include "content/public/browser/navigation_entry.h"
37 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/render_view_host.h"
39 #include "content/public/browser/web_contents.h"
41 #if defined(USE_ASH)
42 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
43 #endif
45 #if defined(USE_AURA)
46 #include "ui/aura/window.h"
47 #endif
49 #if defined(ENABLE_EXTENSIONS)
50 #include "chrome/browser/extensions/tab_helper.h"
51 #include "chrome/browser/web_applications/web_app.h"
52 #include "extensions/browser/extension_registry.h"
53 #include "extensions/common/extension.h"
54 #include "extensions/common/extension_set.h"
55 #endif
57 using content::GlobalRequestID;
58 using content::NavigationController;
59 using content::WebContents;
61 class BrowserNavigatorWebContentsAdoption {
62 public:
63 static void AttachTabHelpers(content::WebContents* contents) {
64 TabHelpers::AttachTabHelpers(contents);
68 namespace {
70 // Returns true if the specified Browser can open tabs. Not all Browsers support
71 // multiple tabs, such as app frames and popups. This function returns false for
72 // those types of Browser.
73 bool WindowCanOpenTabs(Browser* browser) {
74 return browser->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP) ||
75 browser->tab_strip_model()->empty();
78 // Finds an existing Browser compatible with |profile|, making a new one if no
79 // such Browser is located.
80 Browser* GetOrCreateBrowser(Profile* profile,
81 chrome::HostDesktopType host_desktop_type) {
82 Browser* browser = chrome::FindTabbedBrowser(profile, false,
83 host_desktop_type);
84 return browser ? browser : new Browser(
85 Browser::CreateParams(profile, host_desktop_type));
88 // Change some of the navigation parameters based on the particular URL.
89 // Currently this applies to some chrome:// pages which we always want to open
90 // in a non-incognito window. Note that even though a ChromeOS guest session is
91 // technically an incognito window, these URLs are allowed.
92 // Returns true on success. Otherwise, if changing params leads the browser into
93 // an erroneous state, returns false.
94 bool AdjustNavigateParamsForURL(chrome::NavigateParams* params) {
95 if (params->target_contents != NULL ||
96 chrome::IsURLAllowedInIncognito(params->url,
97 params->initiating_profile) ||
98 params->initiating_profile->IsGuestSession()) {
99 return true;
102 Profile* profile = params->initiating_profile;
104 if (profile->IsOffTheRecord() || params->disposition == OFF_THE_RECORD) {
105 profile = profile->GetOriginalProfile();
107 // If incognito is forced, we punt.
108 PrefService* prefs = profile->GetPrefs();
109 if (prefs && IncognitoModePrefs::GetAvailability(prefs) ==
110 IncognitoModePrefs::FORCED) {
111 return false;
114 params->disposition = SINGLETON_TAB;
115 params->browser = GetOrCreateBrowser(profile, params->host_desktop_type);
116 params->window_action = chrome::NavigateParams::SHOW_WINDOW;
119 return true;
122 // Returns a Browser that can host the navigation or tab addition specified in
123 // |params|. This might just return the same Browser specified in |params|, or
124 // some other if that Browser is deemed incompatible.
125 Browser* GetBrowserForDisposition(chrome::NavigateParams* params) {
126 // If no source WebContents was specified, we use the selected one from
127 // the target browser. This must happen first, before
128 // GetBrowserForDisposition() has a chance to replace |params->browser| with
129 // another one.
130 if (!params->source_contents && params->browser) {
131 params->source_contents =
132 params->browser->tab_strip_model()->GetActiveWebContents();
135 Profile* profile = params->initiating_profile;
137 switch (params->disposition) {
138 case CURRENT_TAB:
139 if (params->browser)
140 return params->browser;
141 // Find a compatible window and re-execute this command in it. Otherwise
142 // re-run with NEW_WINDOW.
143 return GetOrCreateBrowser(profile, params->host_desktop_type);
144 case SINGLETON_TAB:
145 case NEW_FOREGROUND_TAB:
146 case NEW_BACKGROUND_TAB:
147 // See if we can open the tab in the window this navigator is bound to.
148 if (params->browser && WindowCanOpenTabs(params->browser))
149 return params->browser;
150 // Find a compatible window and re-execute this command in it. Otherwise
151 // re-run with NEW_WINDOW.
152 return GetOrCreateBrowser(profile, params->host_desktop_type);
153 case NEW_POPUP: {
154 // Make a new popup window.
155 // Coerce app-style if |source| represents an app.
156 std::string app_name;
157 #if defined(ENABLE_EXTENSIONS)
158 if (!params->extension_app_id.empty()) {
159 app_name = web_app::GenerateApplicationNameFromExtensionId(
160 params->extension_app_id);
161 } else if (params->browser && !params->browser->app_name().empty()) {
162 app_name = params->browser->app_name();
163 } else if (params->source_contents) {
164 extensions::TabHelper* extensions_tab_helper =
165 extensions::TabHelper::FromWebContents(params->source_contents);
166 if (extensions_tab_helper && extensions_tab_helper->is_app()) {
167 app_name = web_app::GenerateApplicationNameFromExtensionId(
168 extensions_tab_helper->extension_app()->id());
171 #endif
172 if (app_name.empty()) {
173 Browser::CreateParams browser_params(
174 Browser::TYPE_POPUP, profile, params->host_desktop_type);
175 browser_params.trusted_source = params->trusted_source;
176 browser_params.initial_bounds = params->window_bounds;
177 return new Browser(browser_params);
180 return new Browser(Browser::CreateParams::CreateForApp(
181 app_name,
182 params->trusted_source,
183 params->window_bounds,
184 profile,
185 params->host_desktop_type));
187 case NEW_WINDOW: {
188 // Make a new normal browser window.
189 return new Browser(Browser::CreateParams(profile,
190 params->host_desktop_type));
192 case OFF_THE_RECORD:
193 // Make or find an incognito window.
194 return GetOrCreateBrowser(profile->GetOffTheRecordProfile(),
195 params->host_desktop_type);
196 // The following types all result in no navigation.
197 case SUPPRESS_OPEN:
198 case SAVE_TO_DISK:
199 case IGNORE_ACTION:
200 return NULL;
201 default:
202 NOTREACHED();
204 return NULL;
207 // Fix disposition and other parameter values depending on prevailing
208 // conditions.
209 void NormalizeDisposition(chrome::NavigateParams* params) {
210 // Calculate the WindowOpenDisposition if necessary.
211 if (params->browser->tab_strip_model()->empty() &&
212 (params->disposition == NEW_BACKGROUND_TAB ||
213 params->disposition == CURRENT_TAB ||
214 params->disposition == SINGLETON_TAB)) {
215 params->disposition = NEW_FOREGROUND_TAB;
217 if (params->browser->profile()->IsOffTheRecord() &&
218 params->disposition == OFF_THE_RECORD) {
219 params->disposition = NEW_FOREGROUND_TAB;
221 if (!params->source_contents && params->disposition == CURRENT_TAB)
222 params->disposition = NEW_FOREGROUND_TAB;
224 switch (params->disposition) {
225 case NEW_BACKGROUND_TAB:
226 // Disposition trumps add types. ADD_ACTIVE is a default, so we need to
227 // remove it if disposition implies the tab is going to open in the
228 // background.
229 params->tabstrip_add_types &= ~TabStripModel::ADD_ACTIVE;
230 break;
232 case NEW_WINDOW:
233 case NEW_POPUP:
234 // Code that wants to open a new window typically expects it to be shown
235 // automatically.
236 if (params->window_action == chrome::NavigateParams::NO_ACTION)
237 params->window_action = chrome::NavigateParams::SHOW_WINDOW;
238 // Fall-through.
239 case NEW_FOREGROUND_TAB:
240 case SINGLETON_TAB:
241 params->tabstrip_add_types |= TabStripModel::ADD_ACTIVE;
242 break;
244 default:
245 break;
249 // Obtain the profile used by the code that originated the Navigate() request.
250 Profile* GetSourceProfile(chrome::NavigateParams* params) {
251 if (params->source_contents) {
252 return Profile::FromBrowserContext(
253 params->source_contents->GetBrowserContext());
256 return params->initiating_profile;
259 void LoadURLInContents(WebContents* target_contents,
260 const GURL& url,
261 chrome::NavigateParams* params) {
262 NavigationController::LoadURLParams load_url_params(url);
263 load_url_params.referrer = params->referrer;
264 load_url_params.frame_tree_node_id = params->frame_tree_node_id;
265 load_url_params.redirect_chain = params->redirect_chain;
266 load_url_params.transition_type = params->transition;
267 load_url_params.extra_headers = params->extra_headers;
268 load_url_params.should_replace_current_entry =
269 params->should_replace_current_entry;
271 if (params->transferred_global_request_id != GlobalRequestID()) {
272 load_url_params.is_renderer_initiated = params->is_renderer_initiated;
273 load_url_params.transferred_global_request_id =
274 params->transferred_global_request_id;
275 } else if (params->is_renderer_initiated) {
276 load_url_params.is_renderer_initiated = true;
279 // Only allows the browser-initiated navigation to use POST.
280 if (params->uses_post && !params->is_renderer_initiated) {
281 load_url_params.load_type =
282 NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
283 load_url_params.browser_initiated_post_data =
284 params->browser_initiated_post_data;
286 target_contents->GetController().LoadURLWithParams(load_url_params);
289 // This class makes sure the Browser object held in |params| is made visible
290 // by the time it goes out of scope, provided |params| wants it to be shown.
291 class ScopedBrowserShower {
292 public:
293 explicit ScopedBrowserShower(chrome::NavigateParams* params)
294 : params_(params) {
296 ~ScopedBrowserShower() {
297 if (params_->window_action ==
298 chrome::NavigateParams::SHOW_WINDOW_INACTIVE) {
299 params_->browser->window()->ShowInactive();
300 } else if (params_->window_action == chrome::NavigateParams::SHOW_WINDOW) {
301 params_->browser->window()->Show();
302 // If a user gesture opened a popup window, focus the contents.
303 if (params_->user_gesture && params_->disposition == NEW_POPUP &&
304 params_->target_contents) {
305 params_->target_contents->Focus();
310 private:
311 chrome::NavigateParams* params_;
312 DISALLOW_COPY_AND_ASSIGN(ScopedBrowserShower);
315 // This class manages the lifetime of a WebContents created by the
316 // Navigate() function. When Navigate() creates a WebContents for a URL,
317 // an instance of this class takes ownership of it via TakeOwnership() until the
318 // WebContents is added to a tab strip at which time ownership is
319 // relinquished via ReleaseOwnership(). If this object goes out of scope without
320 // being added to a tab strip, the created WebContents is deleted to
321 // avoid a leak and the params->target_contents field is set to NULL.
322 class ScopedTargetContentsOwner {
323 public:
324 explicit ScopedTargetContentsOwner(chrome::NavigateParams* params)
325 : params_(params) {
327 ~ScopedTargetContentsOwner() {
328 if (target_contents_owner_.get())
329 params_->target_contents = NULL;
332 // Assumes ownership of |params_|' target_contents until ReleaseOwnership
333 // is called.
334 void TakeOwnership() {
335 target_contents_owner_.reset(params_->target_contents);
338 // Relinquishes ownership of |params_|' target_contents.
339 WebContents* ReleaseOwnership() {
340 return target_contents_owner_.release();
343 private:
344 chrome::NavigateParams* params_;
345 scoped_ptr<WebContents> target_contents_owner_;
346 DISALLOW_COPY_AND_ASSIGN(ScopedTargetContentsOwner);
349 content::WebContents* CreateTargetContents(const chrome::NavigateParams& params,
350 const GURL& url) {
351 WebContents::CreateParams create_params(
352 params.browser->profile(),
353 tab_util::GetSiteInstanceForNewTab(params.browser->profile(), url));
354 if (params.source_contents) {
355 create_params.initial_size =
356 params.source_contents->GetContainerBounds().size();
357 if (params.should_set_opener)
358 create_params.opener = params.source_contents;
360 if (params.disposition == NEW_BACKGROUND_TAB)
361 create_params.initially_hidden = true;
363 #if defined(USE_AURA)
364 if (params.browser->window() &&
365 params.browser->window()->GetNativeWindow()) {
366 create_params.context =
367 params.browser->window()->GetNativeWindow();
369 #endif
371 WebContents* target_contents = WebContents::Create(create_params);
373 // New tabs can have WebUI URLs that will make calls back to arbitrary
374 // tab helpers, so the entire set of tab helpers needs to be set up
375 // immediately.
376 BrowserNavigatorWebContentsAdoption::AttachTabHelpers(target_contents);
377 #if defined(ENABLE_EXTENSIONS)
378 extensions::TabHelper::FromWebContents(target_contents)->
379 SetExtensionAppById(params.extension_app_id);
380 #endif
381 return target_contents;
384 // If a prerendered page exists for |url|, replace the page at
385 // |params->target_contents| with it and update to point to the swapped-in
386 // WebContents.
387 bool SwapInPrerender(const GURL& url, chrome::NavigateParams* params) {
388 Profile* profile =
389 Profile::FromBrowserContext(params->target_contents->GetBrowserContext());
390 InstantSearchPrerenderer* prerenderer =
391 InstantSearchPrerenderer::GetForProfile(profile);
392 if (prerenderer && prerenderer->UsePrerenderedPage(url, params))
393 return true;
395 prerender::PrerenderManager* prerender_manager =
396 prerender::PrerenderManagerFactory::GetForProfile(profile);
397 return prerender_manager &&
398 prerender_manager->MaybeUsePrerenderedPage(url, params);
401 chrome::HostDesktopType GetHostDesktop(Browser* browser) {
402 if (browser)
403 return browser->host_desktop_type();
404 return chrome::GetActiveDesktop();
407 } // namespace
409 namespace chrome {
411 NavigateParams::NavigateParams(Browser* a_browser,
412 const GURL& a_url,
413 ui::PageTransition a_transition)
414 : url(a_url),
415 frame_tree_node_id(-1),
416 uses_post(false),
417 target_contents(NULL),
418 source_contents(NULL),
419 disposition(CURRENT_TAB),
420 trusted_source(false),
421 transition(a_transition),
422 is_renderer_initiated(false),
423 tabstrip_index(-1),
424 tabstrip_add_types(TabStripModel::ADD_ACTIVE),
425 window_action(NO_ACTION),
426 user_gesture(true),
427 path_behavior(RESPECT),
428 ref_behavior(IGNORE_REF),
429 browser(a_browser),
430 initiating_profile(NULL),
431 host_desktop_type(GetHostDesktop(a_browser)),
432 should_replace_current_entry(false),
433 should_set_opener(false) {
436 NavigateParams::NavigateParams(Browser* a_browser,
437 WebContents* a_target_contents)
438 : frame_tree_node_id(-1),
439 uses_post(false),
440 target_contents(a_target_contents),
441 source_contents(NULL),
442 disposition(CURRENT_TAB),
443 trusted_source(false),
444 transition(ui::PAGE_TRANSITION_LINK),
445 is_renderer_initiated(false),
446 tabstrip_index(-1),
447 tabstrip_add_types(TabStripModel::ADD_ACTIVE),
448 window_action(NO_ACTION),
449 user_gesture(true),
450 path_behavior(RESPECT),
451 ref_behavior(IGNORE_REF),
452 browser(a_browser),
453 initiating_profile(NULL),
454 host_desktop_type(GetHostDesktop(a_browser)),
455 should_replace_current_entry(false),
456 should_set_opener(false) {
459 NavigateParams::NavigateParams(Profile* a_profile,
460 const GURL& a_url,
461 ui::PageTransition a_transition)
462 : url(a_url),
463 frame_tree_node_id(-1),
464 uses_post(false),
465 target_contents(NULL),
466 source_contents(NULL),
467 disposition(NEW_FOREGROUND_TAB),
468 trusted_source(false),
469 transition(a_transition),
470 is_renderer_initiated(false),
471 tabstrip_index(-1),
472 tabstrip_add_types(TabStripModel::ADD_ACTIVE),
473 window_action(SHOW_WINDOW),
474 user_gesture(true),
475 path_behavior(RESPECT),
476 ref_behavior(IGNORE_REF),
477 browser(NULL),
478 initiating_profile(a_profile),
479 host_desktop_type(chrome::GetActiveDesktop()),
480 should_replace_current_entry(false),
481 should_set_opener(false) {
484 NavigateParams::~NavigateParams() {}
486 void FillNavigateParamsFromOpenURLParams(chrome::NavigateParams* nav_params,
487 const content::OpenURLParams& params) {
488 nav_params->referrer = params.referrer;
489 nav_params->frame_tree_node_id = params.frame_tree_node_id;
490 nav_params->redirect_chain = params.redirect_chain;
491 nav_params->extra_headers = params.extra_headers;
492 nav_params->disposition = params.disposition;
493 nav_params->trusted_source = false;
494 nav_params->is_renderer_initiated = params.is_renderer_initiated;
495 nav_params->transferred_global_request_id =
496 params.transferred_global_request_id;
497 nav_params->should_replace_current_entry =
498 params.should_replace_current_entry;
499 nav_params->uses_post = params.uses_post;
500 nav_params->browser_initiated_post_data = params.browser_initiated_post_data;
503 void Navigate(NavigateParams* params) {
504 Browser* source_browser = params->browser;
505 if (source_browser)
506 params->initiating_profile = source_browser->profile();
507 DCHECK(params->initiating_profile);
509 if (!AdjustNavigateParamsForURL(params))
510 return;
512 #if defined(ENABLE_EXTENSIONS)
513 const extensions::Extension* extension =
514 extensions::ExtensionRegistry::Get(params->initiating_profile)->
515 enabled_extensions().GetExtensionOrAppByURL(params->url);
516 // Platform apps cannot navigate. Block the request.
517 if (extension && extension->is_platform_app())
518 params->url = GURL(chrome::kExtensionInvalidRequestURL);
519 #endif
521 // The browser window may want to adjust the disposition.
522 if (params->disposition == NEW_POPUP &&
523 source_browser &&
524 source_browser->window()) {
525 params->disposition =
526 source_browser->window()->GetDispositionForPopupBounds(
527 params->window_bounds);
530 params->browser = GetBrowserForDisposition(params);
531 if (!params->browser)
532 return;
534 #if defined(USE_ASH)
535 if (source_browser && source_browser != params->browser) {
536 // When the newly created browser was spawned by a browser which visits
537 // another user's desktop, it should be shown on the same desktop as the
538 // originating one. (This is part of the desktop separation per profile).
539 MultiUserWindowManager* manager = MultiUserWindowManager::GetInstance();
540 // Some unit tests have no manager instantiated.
541 if (manager) {
542 aura::Window* src_window = source_browser->window()->GetNativeWindow();
543 aura::Window* new_window = params->browser->window()->GetNativeWindow();
544 const std::string& src_user =
545 manager->GetUserPresentingWindow(src_window);
546 if (src_user != manager->GetUserPresentingWindow(new_window)) {
547 // Once the window gets presented, it should be shown on the same
548 // desktop as the desktop of the creating browser. Note that this
549 // command will not show the window if it wasn't shown yet by the
550 // browser creation.
551 manager->ShowWindowForUser(new_window, src_user);
555 #endif
557 // Navigate() must not return early after this point.
559 if (GetSourceProfile(params) != params->browser->profile()) {
560 // A tab is being opened from a link from a different profile, we must reset
561 // source information that may cause state to be shared.
562 params->source_contents = NULL;
563 params->referrer = content::Referrer();
566 // Make sure the Browser is shown if params call for it.
567 ScopedBrowserShower shower(params);
569 // Makes sure any WebContents created by this function is destroyed if
570 // not properly added to a tab strip.
571 ScopedTargetContentsOwner target_contents_owner(params);
573 // Some dispositions need coercion to base types.
574 NormalizeDisposition(params);
576 // If a new window has been created, it needs to be shown.
577 if (params->window_action == NavigateParams::NO_ACTION &&
578 source_browser != params->browser &&
579 params->browser->tab_strip_model()->empty()) {
580 params->window_action = NavigateParams::SHOW_WINDOW;
583 // If we create a popup window from a non user-gesture, don't activate it.
584 if (params->window_action == NavigateParams::SHOW_WINDOW &&
585 params->disposition == NEW_POPUP &&
586 params->user_gesture == false) {
587 params->window_action = NavigateParams::SHOW_WINDOW_INACTIVE;
590 // Determine if the navigation was user initiated. If it was, we need to
591 // inform the target WebContents, and we may need to update the UI.
592 ui::PageTransition base_transition =
593 ui::PageTransitionStripQualifier(params->transition);
594 bool user_initiated =
595 params->transition & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR ||
596 base_transition == ui::PAGE_TRANSITION_TYPED ||
597 base_transition == ui::PAGE_TRANSITION_AUTO_BOOKMARK ||
598 base_transition == ui::PAGE_TRANSITION_GENERATED ||
599 base_transition == ui::PAGE_TRANSITION_AUTO_TOPLEVEL ||
600 base_transition == ui::PAGE_TRANSITION_RELOAD ||
601 base_transition == ui::PAGE_TRANSITION_KEYWORD;
603 // Check if this is a singleton tab that already exists
604 int singleton_index = chrome::GetIndexOfSingletonTab(params);
606 // Did we use a prerender?
607 bool swapped_in_prerender = false;
609 // If no target WebContents was specified, we need to construct one if
610 // we are supposed to target a new tab; unless it's a singleton that already
611 // exists.
612 if (!params->target_contents && singleton_index < 0) {
613 DCHECK(!params->url.is_empty());
614 if (params->disposition != CURRENT_TAB) {
615 params->target_contents = CreateTargetContents(*params, params->url);
617 // This function takes ownership of |params->target_contents| until it
618 // is added to a TabStripModel.
619 target_contents_owner.TakeOwnership();
620 } else {
621 // ... otherwise if we're loading in the current tab, the target is the
622 // same as the source.
623 DCHECK(params->source_contents);
624 params->target_contents = params->source_contents;
627 // Note: at this point, if |params->disposition| is not CURRENT_TAB,
628 // |params->target_contents| has not been attached to a Browser yet. (That
629 // happens later in this function.) However, in that case, the
630 // sessionStorage namespace could not match, so prerender will use the
631 // asynchronous codepath and still swap.
632 DCHECK(params->target_contents);
633 swapped_in_prerender = SwapInPrerender(params->url, params);
635 if (user_initiated)
636 params->target_contents->UserGestureDone();
638 if (!swapped_in_prerender) {
639 // Try to handle non-navigational URLs that popup dialogs and such, these
640 // should not actually navigate.
641 if (!HandleNonNavigationAboutURL(params->url)) {
642 // Perform the actual navigation, tracking whether it came from the
643 // renderer.
645 LoadURLInContents(params->target_contents, params->url, params);
646 // For prerender bookkeeping purposes, record that this pending navigate
647 // originated from chrome::Navigate.
648 content::NavigationEntry* entry =
649 params->target_contents->GetController().GetPendingEntry();
650 if (entry)
651 entry->SetExtraData(prerender::kChromeNavigateExtraDataKey,
652 base::string16());
655 } else {
656 // |target_contents| was specified non-NULL, and so we assume it has already
657 // been navigated appropriately. We need to do nothing more other than
658 // add it to the appropriate tabstrip.
661 // If the user navigated from the omnibox, and the selected tab is going to
662 // lose focus, then make sure the focus for the source tab goes away from the
663 // omnibox.
664 if (params->source_contents &&
665 (params->disposition == NEW_FOREGROUND_TAB ||
666 params->disposition == NEW_WINDOW) &&
667 (params->tabstrip_add_types & TabStripModel::ADD_INHERIT_OPENER))
668 params->source_contents->Focus();
670 if (params->source_contents == params->target_contents ||
671 (swapped_in_prerender && params->disposition == CURRENT_TAB)) {
672 // The navigation occurred in the source tab.
673 params->browser->UpdateUIForNavigationInTab(params->target_contents,
674 params->transition,
675 user_initiated);
676 } else if (singleton_index == -1) {
677 // If some non-default value is set for the index, we should tell the
678 // TabStripModel to respect it.
679 if (params->tabstrip_index != -1)
680 params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX;
682 // The navigation should insert a new tab into the target Browser.
683 params->browser->tab_strip_model()->AddWebContents(
684 params->target_contents,
685 params->tabstrip_index,
686 params->transition,
687 params->tabstrip_add_types);
688 // Now that the |params->target_contents| is safely owned by the target
689 // Browser's TabStripModel, we can release ownership.
690 target_contents_owner.ReleaseOwnership();
693 if (singleton_index >= 0) {
694 WebContents* target =
695 params->browser->tab_strip_model()->GetWebContentsAt(singleton_index);
697 if (target->IsCrashed()) {
698 target->GetController().Reload(true);
699 } else if (params->path_behavior == NavigateParams::IGNORE_AND_NAVIGATE &&
700 target->GetURL() != params->url) {
701 LoadURLInContents(target, params->url, params);
702 // For prerender bookkeeping purposes, record that this pending navigate
703 // originated from chrome::Navigate.
704 content::NavigationEntry* entry =
705 target->GetController().GetPendingEntry();
706 if (entry)
707 entry->SetExtraData(prerender::kChromeNavigateExtraDataKey,
708 base::string16());
711 // If the singleton tab isn't already selected, select it.
712 if (params->source_contents != params->target_contents) {
713 params->browser->tab_strip_model()->ActivateTabAt(singleton_index,
714 user_initiated);
718 if (params->disposition != CURRENT_TAB) {
719 content::NotificationService::current()->Notify(
720 chrome::NOTIFICATION_TAB_ADDED,
721 content::Source<content::WebContentsDelegate>(params->browser),
722 content::Details<WebContents>(params->target_contents));
726 bool IsURLAllowedInIncognito(const GURL& url,
727 content::BrowserContext* browser_context) {
728 if (url.scheme() == content::kViewSourceScheme) {
729 // A view-source URL is allowed in incognito mode only if the URL itself
730 // is allowed in incognito mode. Remove the "view-source:" from the start
731 // of the URL and validate the rest.
732 std::string stripped_spec = url.spec();
733 DCHECK_GT(stripped_spec.size(), strlen(content::kViewSourceScheme));
734 stripped_spec.erase(0, strlen(content::kViewSourceScheme) + 1);
735 GURL stripped_url(stripped_spec);
736 return stripped_url.is_valid() &&
737 IsURLAllowedInIncognito(stripped_url, browser_context);
739 // Most URLs are allowed in incognito; the following are exceptions.
740 // chrome://extensions is on the list because it redirects to
741 // chrome://settings.
742 if (url.scheme() == content::kChromeUIScheme &&
743 (url.host() == chrome::kChromeUISettingsHost ||
744 url.host() == chrome::kChromeUISettingsFrameHost ||
745 url.host() == chrome::kChromeUIExtensionsHost ||
746 url.host() == chrome::kChromeUIBookmarksHost ||
747 #if !defined(OS_CHROMEOS)
748 url.host() == chrome::kChromeUIChromeSigninHost ||
749 #endif
750 url.host() == chrome::kChromeUIUberHost ||
751 url.host() == chrome::kChromeUIThumbnailHost ||
752 url.host() == chrome::kChromeUIThumbnailHost2 ||
753 url.host() == chrome::kChromeUIThumbnailListHost ||
754 url.host() == chrome::kChromeUISuggestionsHost ||
755 url.host() == chrome::kChromeUIDevicesHost ||
756 url.host() == chrome::kChromeUIVoiceSearchHost)) {
757 return false;
760 if (url.scheme() == chrome::kChromeSearchScheme &&
761 (url.host() == chrome::kChromeUIThumbnailHost ||
762 url.host() == chrome::kChromeUIThumbnailHost2 ||
763 url.host() == chrome::kChromeUIThumbnailListHost ||
764 url.host() == chrome::kChromeUISuggestionsHost)) {
765 return false;
768 GURL rewritten_url = url;
769 bool reverse_on_redirect = false;
770 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
771 &rewritten_url, browser_context, &reverse_on_redirect);
773 // Some URLs are mapped to uber subpages. Do not allow them in incognito.
774 return !(rewritten_url.scheme() == content::kChromeUIScheme &&
775 rewritten_url.host() == chrome::kChromeUIUberHost);
778 } // namespace chrome