Refactor views app list services to allow more code sharing
[chromium-blink-merge.git] / chrome / browser / ui / browser_navigator.cc
blob2d65644d9c921d2d1955c7308b7f605b96820010
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/extensions/tab_helper.h"
16 #include "chrome/browser/google/google_url_tracker.h"
17 #include "chrome/browser/prefs/incognito_mode_prefs.h"
18 #include "chrome/browser/prerender/prerender_manager.h"
19 #include "chrome/browser/prerender/prerender_manager_factory.h"
20 #include "chrome/browser/prerender/prerender_util.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/tab_contents/tab_util.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/browser/ui/browser_instant_controller.h"
26 #include "chrome/browser/ui/browser_window.h"
27 #include "chrome/browser/ui/host_desktop.h"
28 #include "chrome/browser/ui/omnibox/location_bar.h"
29 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
30 #include "chrome/browser/ui/singleton_tabs.h"
31 #include "chrome/browser/ui/status_bubble.h"
32 #include "chrome/browser/ui/tab_helpers.h"
33 #include "chrome/browser/ui/tabs/tab_strip_model.h"
34 #include "chrome/browser/web_applications/web_app.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/url_constants.h"
37 #include "content/public/browser/browser_url_handler.h"
38 #include "content/public/browser/navigation_entry.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/browser/web_contents_view.h"
43 #include "extensions/browser/extension_registry.h"
44 #include "extensions/common/extension.h"
45 #include "extensions/common/extension_set.h"
47 #if defined(USE_ASH)
48 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
49 #endif
50 #if defined(USE_AURA)
51 #include "ui/aura/window.h"
52 #endif
54 using content::GlobalRequestID;
55 using content::NavigationController;
56 using content::WebContents;
58 class BrowserNavigatorWebContentsAdoption {
59 public:
60 static void AttachTabHelpers(content::WebContents* contents) {
61 TabHelpers::AttachTabHelpers(contents);
65 namespace {
67 // Returns true if the specified Browser can open tabs. Not all Browsers support
68 // multiple tabs, such as app frames and popups. This function returns false for
69 // those types of Browser.
70 bool WindowCanOpenTabs(Browser* browser) {
71 return browser->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP) ||
72 browser->tab_strip_model()->empty();
75 // Finds an existing Browser compatible with |profile|, making a new one if no
76 // such Browser is located.
77 Browser* GetOrCreateBrowser(Profile* profile,
78 chrome::HostDesktopType host_desktop_type) {
79 Browser* browser = chrome::FindTabbedBrowser(profile, false,
80 host_desktop_type);
81 return browser ? browser : new Browser(
82 Browser::CreateParams(profile, host_desktop_type));
85 // Change some of the navigation parameters based on the particular URL.
86 // Currently this applies to some chrome:// pages which we always want to open
87 // in a non-incognito window. Note that even though a ChromeOS guest session is
88 // technically an incognito window, these URLs are allowed.
89 // Returns true on success. Otherwise, if changing params leads the browser into
90 // an erroneous state, returns false.
91 bool AdjustNavigateParamsForURL(chrome::NavigateParams* params) {
92 if (params->target_contents != NULL ||
93 chrome::IsURLAllowedInIncognito(params->url,
94 params->initiating_profile) ||
95 params->initiating_profile->IsGuestSession()) {
96 return true;
99 Profile* profile = params->initiating_profile;
101 if (profile->IsOffTheRecord() || params->disposition == OFF_THE_RECORD) {
102 profile = profile->GetOriginalProfile();
104 // If incognito is forced, we punt.
105 PrefService* prefs = profile->GetPrefs();
106 if (prefs && IncognitoModePrefs::GetAvailability(prefs) ==
107 IncognitoModePrefs::FORCED) {
108 return false;
111 params->disposition = SINGLETON_TAB;
112 params->browser = GetOrCreateBrowser(profile, params->host_desktop_type);
113 params->window_action = chrome::NavigateParams::SHOW_WINDOW;
116 return true;
119 // Returns a Browser that can host the navigation or tab addition specified in
120 // |params|. This might just return the same Browser specified in |params|, or
121 // some other if that Browser is deemed incompatible.
122 Browser* GetBrowserForDisposition(chrome::NavigateParams* params) {
123 // If no source WebContents was specified, we use the selected one from
124 // the target browser. This must happen first, before
125 // GetBrowserForDisposition() has a chance to replace |params->browser| with
126 // another one.
127 if (!params->source_contents && params->browser) {
128 params->source_contents =
129 params->browser->tab_strip_model()->GetActiveWebContents();
132 Profile* profile = params->initiating_profile;
134 switch (params->disposition) {
135 case CURRENT_TAB:
136 if (params->browser)
137 return params->browser;
138 // Find a compatible window and re-execute this command in it. Otherwise
139 // re-run with NEW_WINDOW.
140 return GetOrCreateBrowser(profile, params->host_desktop_type);
141 case SINGLETON_TAB:
142 case NEW_FOREGROUND_TAB:
143 case NEW_BACKGROUND_TAB:
144 // See if we can open the tab in the window this navigator is bound to.
145 if (params->browser && WindowCanOpenTabs(params->browser))
146 return params->browser;
147 // Find a compatible window and re-execute this command in it. Otherwise
148 // re-run with NEW_WINDOW.
149 return GetOrCreateBrowser(profile, params->host_desktop_type);
150 case NEW_POPUP: {
151 // Make a new popup window.
152 // Coerce app-style if |source| represents an app.
153 std::string app_name;
154 if (!params->extension_app_id.empty()) {
155 app_name = web_app::GenerateApplicationNameFromExtensionId(
156 params->extension_app_id);
157 } else if (params->browser && !params->browser->app_name().empty()) {
158 app_name = params->browser->app_name();
159 } else if (params->source_contents) {
160 extensions::TabHelper* extensions_tab_helper =
161 extensions::TabHelper::FromWebContents(params->source_contents);
162 if (extensions_tab_helper && extensions_tab_helper->is_app()) {
163 app_name = web_app::GenerateApplicationNameFromExtensionId(
164 extensions_tab_helper->extension_app()->id());
167 if (app_name.empty()) {
168 Browser::CreateParams browser_params(
169 Browser::TYPE_POPUP, profile, params->host_desktop_type);
170 browser_params.trusted_source = params->trusted_source;
171 browser_params.initial_bounds = params->window_bounds;
172 return new Browser(browser_params);
175 return new Browser(Browser::CreateParams::CreateForApp(
176 app_name,
177 params->trusted_source,
178 params->window_bounds,
179 profile,
180 params->host_desktop_type));
182 case NEW_WINDOW: {
183 // Make a new normal browser window.
184 return new Browser(Browser::CreateParams(profile,
185 params->host_desktop_type));
187 case OFF_THE_RECORD:
188 // Make or find an incognito window.
189 return GetOrCreateBrowser(profile->GetOffTheRecordProfile(),
190 params->host_desktop_type);
191 // The following types all result in no navigation.
192 case SUPPRESS_OPEN:
193 case SAVE_TO_DISK:
194 case IGNORE_ACTION:
195 return NULL;
196 default:
197 NOTREACHED();
199 return NULL;
202 // Fix disposition and other parameter values depending on prevailing
203 // conditions.
204 void NormalizeDisposition(chrome::NavigateParams* params) {
205 // Calculate the WindowOpenDisposition if necessary.
206 if (params->browser->tab_strip_model()->empty() &&
207 (params->disposition == NEW_BACKGROUND_TAB ||
208 params->disposition == CURRENT_TAB ||
209 params->disposition == SINGLETON_TAB)) {
210 params->disposition = NEW_FOREGROUND_TAB;
212 if (params->browser->profile()->IsOffTheRecord() &&
213 params->disposition == OFF_THE_RECORD) {
214 params->disposition = NEW_FOREGROUND_TAB;
216 if (!params->source_contents && params->disposition == CURRENT_TAB)
217 params->disposition = NEW_FOREGROUND_TAB;
219 switch (params->disposition) {
220 case NEW_BACKGROUND_TAB:
221 // Disposition trumps add types. ADD_ACTIVE is a default, so we need to
222 // remove it if disposition implies the tab is going to open in the
223 // background.
224 params->tabstrip_add_types &= ~TabStripModel::ADD_ACTIVE;
225 break;
227 case NEW_WINDOW:
228 case NEW_POPUP:
229 // Code that wants to open a new window typically expects it to be shown
230 // automatically.
231 if (params->window_action == chrome::NavigateParams::NO_ACTION)
232 params->window_action = chrome::NavigateParams::SHOW_WINDOW;
233 // Fall-through.
234 case NEW_FOREGROUND_TAB:
235 case SINGLETON_TAB:
236 params->tabstrip_add_types |= TabStripModel::ADD_ACTIVE;
237 break;
239 default:
240 break;
244 // Obtain the profile used by the code that originated the Navigate() request.
245 Profile* GetSourceProfile(chrome::NavigateParams* params) {
246 if (params->source_contents) {
247 return Profile::FromBrowserContext(
248 params->source_contents->GetBrowserContext());
251 return params->initiating_profile;
254 void LoadURLInContents(WebContents* target_contents,
255 const GURL& url,
256 chrome::NavigateParams* params) {
257 NavigationController::LoadURLParams load_url_params(url);
258 load_url_params.referrer = params->referrer;
259 load_url_params.frame_tree_node_id = params->frame_tree_node_id;
260 load_url_params.redirect_chain = params->redirect_chain;
261 load_url_params.transition_type = params->transition;
262 load_url_params.extra_headers = params->extra_headers;
263 load_url_params.should_replace_current_entry =
264 params->should_replace_current_entry;
266 if (params->transferred_global_request_id != GlobalRequestID()) {
267 load_url_params.is_renderer_initiated = params->is_renderer_initiated;
268 load_url_params.transferred_global_request_id =
269 params->transferred_global_request_id;
270 } else if (params->is_renderer_initiated) {
271 load_url_params.is_renderer_initiated = true;
274 // Only allows the browser-initiated navigation to use POST.
275 if (params->uses_post && !params->is_renderer_initiated) {
276 load_url_params.load_type =
277 NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
278 load_url_params.browser_initiated_post_data =
279 params->browser_initiated_post_data;
281 target_contents->GetController().LoadURLWithParams(load_url_params);
284 // This class makes sure the Browser object held in |params| is made visible
285 // by the time it goes out of scope, provided |params| wants it to be shown.
286 class ScopedBrowserShower {
287 public:
288 explicit ScopedBrowserShower(chrome::NavigateParams* params)
289 : params_(params) {
291 ~ScopedBrowserShower() {
292 if (params_->window_action == chrome::NavigateParams::SHOW_WINDOW_INACTIVE)
293 params_->browser->window()->ShowInactive();
294 else if (params_->window_action == chrome::NavigateParams::SHOW_WINDOW)
295 params_->browser->window()->Show();
297 private:
298 chrome::NavigateParams* params_;
299 DISALLOW_COPY_AND_ASSIGN(ScopedBrowserShower);
302 // This class manages the lifetime of a WebContents created by the
303 // Navigate() function. When Navigate() creates a WebContents for a URL,
304 // an instance of this class takes ownership of it via TakeOwnership() until the
305 // WebContents is added to a tab strip at which time ownership is
306 // relinquished via ReleaseOwnership(). If this object goes out of scope without
307 // being added to a tab strip, the created WebContents is deleted to
308 // avoid a leak and the params->target_contents field is set to NULL.
309 class ScopedTargetContentsOwner {
310 public:
311 explicit ScopedTargetContentsOwner(chrome::NavigateParams* params)
312 : params_(params) {
314 ~ScopedTargetContentsOwner() {
315 if (target_contents_owner_.get())
316 params_->target_contents = NULL;
319 // Assumes ownership of |params_|' target_contents until ReleaseOwnership
320 // is called.
321 void TakeOwnership() {
322 target_contents_owner_.reset(params_->target_contents);
325 // Relinquishes ownership of |params_|' target_contents.
326 WebContents* ReleaseOwnership() {
327 return target_contents_owner_.release();
330 private:
331 chrome::NavigateParams* params_;
332 scoped_ptr<WebContents> target_contents_owner_;
333 DISALLOW_COPY_AND_ASSIGN(ScopedTargetContentsOwner);
336 content::WebContents* CreateTargetContents(const chrome::NavigateParams& params,
337 const GURL& url) {
338 WebContents::CreateParams create_params(
339 params.browser->profile(),
340 tab_util::GetSiteInstanceForNewTab(params.browser->profile(), url));
341 if (params.source_contents) {
342 create_params.initial_size =
343 params.source_contents->GetView()->GetContainerSize();
344 if (params.should_set_opener)
345 create_params.opener = params.source_contents;
347 if (params.disposition == NEW_BACKGROUND_TAB)
348 create_params.initially_hidden = true;
350 #if defined(USE_AURA)
351 if (params.browser->window() &&
352 params.browser->window()->GetNativeWindow()) {
353 create_params.context =
354 params.browser->window()->GetNativeWindow();
356 #endif
358 WebContents* target_contents = WebContents::Create(create_params);
360 // New tabs can have WebUI URLs that will make calls back to arbitrary
361 // tab helpers, so the entire set of tab helpers needs to be set up
362 // immediately.
363 BrowserNavigatorWebContentsAdoption::AttachTabHelpers(target_contents);
364 extensions::TabHelper::FromWebContents(target_contents)->
365 SetExtensionAppById(params.extension_app_id);
366 return target_contents;
369 // If a prerendered page exists for |url|, replace the page at
370 // |params->target_contents| with it and update to point to the swapped-in
371 // WebContents.
372 bool SwapInPrerender(const GURL& url, chrome::NavigateParams* params) {
373 Profile* profile =
374 Profile::FromBrowserContext(params->target_contents->GetBrowserContext());
375 InstantSearchPrerenderer* prerenderer =
376 InstantSearchPrerenderer::GetForProfile(profile);
377 if (prerenderer && prerenderer->UsePrerenderedPage(url, params))
378 return true;
380 prerender::PrerenderManager* prerender_manager =
381 prerender::PrerenderManagerFactory::GetForProfile(profile);
382 return prerender_manager &&
383 prerender_manager->MaybeUsePrerenderedPage(url, params);
386 chrome::HostDesktopType GetHostDesktop(Browser* browser) {
387 if (browser)
388 return browser->host_desktop_type();
389 return chrome::GetActiveDesktop();
392 } // namespace
394 namespace chrome {
396 NavigateParams::NavigateParams(Browser* a_browser,
397 const GURL& a_url,
398 content::PageTransition a_transition)
399 : url(a_url),
400 frame_tree_node_id(-1),
401 uses_post(false),
402 target_contents(NULL),
403 source_contents(NULL),
404 disposition(CURRENT_TAB),
405 trusted_source(false),
406 transition(a_transition),
407 is_renderer_initiated(false),
408 tabstrip_index(-1),
409 tabstrip_add_types(TabStripModel::ADD_ACTIVE),
410 window_action(NO_ACTION),
411 user_gesture(true),
412 path_behavior(RESPECT),
413 ref_behavior(IGNORE_REF),
414 browser(a_browser),
415 initiating_profile(NULL),
416 host_desktop_type(GetHostDesktop(a_browser)),
417 should_replace_current_entry(false),
418 should_set_opener(false) {
421 NavigateParams::NavigateParams(Browser* a_browser,
422 WebContents* a_target_contents)
423 : frame_tree_node_id(-1),
424 uses_post(false),
425 target_contents(a_target_contents),
426 source_contents(NULL),
427 disposition(CURRENT_TAB),
428 trusted_source(false),
429 transition(content::PAGE_TRANSITION_LINK),
430 is_renderer_initiated(false),
431 tabstrip_index(-1),
432 tabstrip_add_types(TabStripModel::ADD_ACTIVE),
433 window_action(NO_ACTION),
434 user_gesture(true),
435 path_behavior(RESPECT),
436 ref_behavior(IGNORE_REF),
437 browser(a_browser),
438 initiating_profile(NULL),
439 host_desktop_type(GetHostDesktop(a_browser)),
440 should_replace_current_entry(false),
441 should_set_opener(false) {
444 NavigateParams::NavigateParams(Profile* a_profile,
445 const GURL& a_url,
446 content::PageTransition a_transition)
447 : url(a_url),
448 frame_tree_node_id(-1),
449 uses_post(false),
450 target_contents(NULL),
451 source_contents(NULL),
452 disposition(NEW_FOREGROUND_TAB),
453 trusted_source(false),
454 transition(a_transition),
455 is_renderer_initiated(false),
456 tabstrip_index(-1),
457 tabstrip_add_types(TabStripModel::ADD_ACTIVE),
458 window_action(SHOW_WINDOW),
459 user_gesture(true),
460 path_behavior(RESPECT),
461 ref_behavior(IGNORE_REF),
462 browser(NULL),
463 initiating_profile(a_profile),
464 host_desktop_type(chrome::GetActiveDesktop()),
465 should_replace_current_entry(false),
466 should_set_opener(false) {
469 NavigateParams::~NavigateParams() {}
471 void FillNavigateParamsFromOpenURLParams(chrome::NavigateParams* nav_params,
472 const content::OpenURLParams& params) {
473 nav_params->referrer = params.referrer;
474 nav_params->frame_tree_node_id = params.frame_tree_node_id;
475 nav_params->redirect_chain = params.redirect_chain;
476 nav_params->extra_headers = params.extra_headers;
477 nav_params->disposition = params.disposition;
478 nav_params->trusted_source = false;
479 nav_params->is_renderer_initiated = params.is_renderer_initiated;
480 nav_params->transferred_global_request_id =
481 params.transferred_global_request_id;
482 nav_params->should_replace_current_entry =
483 params.should_replace_current_entry;
484 nav_params->uses_post = params.uses_post;
485 nav_params->browser_initiated_post_data = params.browser_initiated_post_data;
488 void Navigate(NavigateParams* params) {
489 Browser* source_browser = params->browser;
490 if (source_browser)
491 params->initiating_profile = source_browser->profile();
492 DCHECK(params->initiating_profile);
494 if (!AdjustNavigateParamsForURL(params))
495 return;
497 const extensions::Extension* extension =
498 extensions::ExtensionRegistry::Get(params->initiating_profile)->
499 enabled_extensions().GetExtensionOrAppByURL(params->url);
500 // Platform apps cannot navigate. Block the request.
501 if (extension && extension->is_platform_app())
502 params->url = GURL(chrome::kExtensionInvalidRequestURL);
504 // The browser window may want to adjust the disposition.
505 if (params->disposition == NEW_POPUP &&
506 source_browser &&
507 source_browser->window()) {
508 params->disposition =
509 source_browser->window()->GetDispositionForPopupBounds(
510 params->window_bounds);
513 params->browser = GetBrowserForDisposition(params);
514 if (!params->browser)
515 return;
517 #if defined(USE_ASH)
518 if (source_browser && source_browser != params->browser) {
519 // When the newly created browser was spawned by a browser which visits
520 // another user's desktop, it should be shown on the same desktop as the
521 // originating one. (This is part of the desktop separation per profile).
522 MultiUserWindowManager* manager = MultiUserWindowManager::GetInstance();
523 // Some unit tests have no manager instantiated.
524 if (manager) {
525 aura::Window* src_window = source_browser->window()->GetNativeWindow();
526 aura::Window* new_window = params->browser->window()->GetNativeWindow();
527 const std::string& src_user =
528 manager->GetUserPresentingWindow(src_window);
529 if (src_user != manager->GetUserPresentingWindow(new_window)) {
530 // Once the window gets presented, it should be shown on the same
531 // desktop as the desktop of the creating browser. Note that this
532 // command will not show the window if it wasn't shown yet by the
533 // browser creation.
534 manager->ShowWindowForUser(new_window, src_user);
538 #endif
540 // Navigate() must not return early after this point.
542 if (GetSourceProfile(params) != params->browser->profile()) {
543 // A tab is being opened from a link from a different profile, we must reset
544 // source information that may cause state to be shared.
545 params->source_contents = NULL;
546 params->referrer = content::Referrer();
549 // Make sure the Browser is shown if params call for it.
550 ScopedBrowserShower shower(params);
552 // Makes sure any WebContents created by this function is destroyed if
553 // not properly added to a tab strip.
554 ScopedTargetContentsOwner target_contents_owner(params);
556 // Some dispositions need coercion to base types.
557 NormalizeDisposition(params);
559 // If a new window has been created, it needs to be shown.
560 if (params->window_action == NavigateParams::NO_ACTION &&
561 source_browser != params->browser &&
562 params->browser->tab_strip_model()->empty()) {
563 params->window_action = NavigateParams::SHOW_WINDOW;
566 // If we create a popup window from a non user-gesture, don't activate it.
567 if (params->window_action == NavigateParams::SHOW_WINDOW &&
568 params->disposition == NEW_POPUP &&
569 params->user_gesture == false) {
570 params->window_action = NavigateParams::SHOW_WINDOW_INACTIVE;
573 // Determine if the navigation was user initiated. If it was, we need to
574 // inform the target WebContents, and we may need to update the UI.
575 content::PageTransition base_transition =
576 content::PageTransitionStripQualifier(params->transition);
577 bool user_initiated =
578 params->transition & content::PAGE_TRANSITION_FROM_ADDRESS_BAR ||
579 base_transition == content::PAGE_TRANSITION_TYPED ||
580 base_transition == content::PAGE_TRANSITION_AUTO_BOOKMARK ||
581 base_transition == content::PAGE_TRANSITION_GENERATED ||
582 base_transition == content::PAGE_TRANSITION_AUTO_TOPLEVEL ||
583 base_transition == content::PAGE_TRANSITION_RELOAD ||
584 base_transition == content::PAGE_TRANSITION_KEYWORD;
586 // Check if this is a singleton tab that already exists
587 int singleton_index = chrome::GetIndexOfSingletonTab(params);
589 // Did we use a prerender?
590 bool swapped_in_prerender = false;
592 // If no target WebContents was specified, we need to construct one if
593 // we are supposed to target a new tab; unless it's a singleton that already
594 // exists.
595 if (!params->target_contents && singleton_index < 0) {
596 GURL url;
597 if (params->url.is_empty()) {
598 url = params->browser->profile()->GetHomePage();
599 params->transition = content::PageTransitionFromInt(
600 params->transition | content::PAGE_TRANSITION_HOME_PAGE);
601 } else {
602 url = params->url;
605 if (params->disposition != CURRENT_TAB) {
606 params->target_contents = CreateTargetContents(*params, url);
608 // This function takes ownership of |params->target_contents| until it
609 // is added to a TabStripModel.
610 target_contents_owner.TakeOwnership();
611 } else {
612 // ... otherwise if we're loading in the current tab, the target is the
613 // same as the source.
614 DCHECK(params->source_contents);
615 params->target_contents = params->source_contents;
616 DCHECK(params->target_contents);
617 // Prerender expects |params->target_contents| to be attached to a browser
618 // window, so only call for CURRENT_TAB navigations. (Others are currently
619 // unsupported because of session storage namespaces anyway.)
620 // Notice that this includes middle-clicking, since middle clicking
621 // translates into a chrome::Navigate call with no URL followed by a
622 // CURRENT_TAB navigation.
623 // TODO(tburkard): We can actually swap in in non-CURRENT_TAB cases, as
624 // long as the WebContents we swap into is part of a TabStrip model.
625 // Therefore, we should swap in regardless of CURRENT_TAB, and instead,
626 // check in the swapin function whether the WebContents is not in a
627 // TabStrip model, in which case we must not swap in.
628 swapped_in_prerender = SwapInPrerender(url, params);
631 if (user_initiated)
632 params->target_contents->UserGestureDone();
634 if (!swapped_in_prerender) {
635 // Try to handle non-navigational URLs that popup dialogs and such, these
636 // should not actually navigate.
637 if (!HandleNonNavigationAboutURL(url)) {
638 // Perform the actual navigation, tracking whether it came from the
639 // renderer.
641 LoadURLInContents(params->target_contents, url, params);
642 // For prerender bookkeeping purposes, record that this pending navigate
643 // originated from chrome::Navigate.
644 content::NavigationEntry* entry =
645 params->target_contents->GetController().GetPendingEntry();
646 if (entry)
647 entry->SetExtraData(prerender::kChromeNavigateExtraDataKey,
648 base::string16());
651 } else {
652 // |target_contents| was specified non-NULL, and so we assume it has already
653 // been navigated appropriately. We need to do nothing more other than
654 // add it to the appropriate tabstrip.
657 // If the user navigated from the omnibox, and the selected tab is going to
658 // lose focus, then make sure the focus for the source tab goes away from the
659 // omnibox.
660 if (params->source_contents &&
661 (params->disposition == NEW_FOREGROUND_TAB ||
662 params->disposition == NEW_WINDOW) &&
663 (params->tabstrip_add_types & TabStripModel::ADD_INHERIT_OPENER))
664 params->source_contents->GetView()->Focus();
666 if (params->source_contents == params->target_contents ||
667 (swapped_in_prerender && params->disposition == CURRENT_TAB)) {
668 // The navigation occurred in the source tab.
669 params->browser->UpdateUIForNavigationInTab(params->target_contents,
670 params->transition,
671 user_initiated);
672 } else if (singleton_index == -1) {
673 // If some non-default value is set for the index, we should tell the
674 // TabStripModel to respect it.
675 if (params->tabstrip_index != -1)
676 params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX;
678 // The navigation should insert a new tab into the target Browser.
679 params->browser->tab_strip_model()->AddWebContents(
680 params->target_contents,
681 params->tabstrip_index,
682 params->transition,
683 params->tabstrip_add_types);
684 // Now that the |params->target_contents| is safely owned by the target
685 // Browser's TabStripModel, we can release ownership.
686 target_contents_owner.ReleaseOwnership();
689 if (singleton_index >= 0) {
690 WebContents* target =
691 params->browser->tab_strip_model()->GetWebContentsAt(singleton_index);
693 if (target->IsCrashed()) {
694 target->GetController().Reload(true);
695 } else if (params->path_behavior == NavigateParams::IGNORE_AND_NAVIGATE &&
696 target->GetURL() != params->url) {
697 LoadURLInContents(target, params->url, params);
698 // For prerender bookkeeping purposes, record that this pending navigate
699 // originated from chrome::Navigate.
700 content::NavigationEntry* entry =
701 target->GetController().GetPendingEntry();
702 if (entry)
703 entry->SetExtraData(prerender::kChromeNavigateExtraDataKey,
704 base::string16());
707 // If the singleton tab isn't already selected, select it.
708 if (params->source_contents != params->target_contents) {
709 params->browser->tab_strip_model()->ActivateTabAt(singleton_index,
710 user_initiated);
714 if (params->disposition != CURRENT_TAB) {
715 content::NotificationService::current()->Notify(
716 chrome::NOTIFICATION_TAB_ADDED,
717 content::Source<content::WebContentsDelegate>(params->browser),
718 content::Details<WebContents>(params->target_contents));
722 bool IsURLAllowedInIncognito(const GURL& url,
723 content::BrowserContext* browser_context) {
724 if (url.scheme() == content::kViewSourceScheme) {
725 // A view-source URL is allowed in incognito mode only if the URL itself
726 // is allowed in incognito mode. Remove the "view-source:" from the start
727 // of the URL and validate the rest.
728 std::string stripped_spec = url.spec();
729 DCHECK_GT(stripped_spec.size(), strlen(content::kViewSourceScheme));
730 stripped_spec.erase(0, strlen(content::kViewSourceScheme) + 1);
731 GURL stripped_url(stripped_spec);
732 return stripped_url.is_valid() &&
733 IsURLAllowedInIncognito(stripped_url, browser_context);
735 // Most URLs are allowed in incognito; the following are exceptions.
736 // chrome://extensions is on the list because it redirects to
737 // chrome://settings.
738 if (url.scheme() == content::kChromeUIScheme &&
739 (url.host() == chrome::kChromeUISettingsHost ||
740 url.host() == chrome::kChromeUISettingsFrameHost ||
741 url.host() == chrome::kChromeUIExtensionsHost ||
742 url.host() == chrome::kChromeUIBookmarksHost ||
743 #if !defined(OS_CHROMEOS)
744 url.host() == chrome::kChromeUIChromeSigninHost ||
745 #endif
746 url.host() == chrome::kChromeUIUberHost ||
747 url.host() == chrome::kChromeUIThumbnailHost ||
748 url.host() == chrome::kChromeUIThumbnailHost2 ||
749 url.host() == chrome::kChromeUIThumbnailListHost ||
750 url.host() == chrome::kChromeUISuggestionsHost ||
751 url.host() == chrome::kChromeUIDevicesHost)) {
752 return false;
755 if (url.scheme() == chrome::kChromeSearchScheme &&
756 (url.host() == chrome::kChromeUIThumbnailHost ||
757 url.host() == chrome::kChromeUIThumbnailHost2 ||
758 url.host() == chrome::kChromeUIThumbnailListHost ||
759 url.host() == chrome::kChromeUISuggestionsHost)) {
760 return false;
763 GURL rewritten_url = url;
764 bool reverse_on_redirect = false;
765 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
766 &rewritten_url, browser_context, &reverse_on_redirect);
768 // Some URLs are mapped to uber subpages. Do not allow them in incognito.
769 return !(rewritten_url.scheme() == content::kChromeUIScheme &&
770 rewritten_url.host() == chrome::kChromeUIUberHost);
773 } // namespace chrome