| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/browser_navigator.h" | 5 #include "chrome/browser/ui/browser_navigator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/browser_about_handler.h" | 12 #include "chrome/browser/browser_about_handler.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/tab_helper.h" | 14 #include "chrome/browser/extensions/tab_helper.h" |
| 15 #include "chrome/browser/google/google_url_tracker.h" | 15 #include "chrome/browser/google/google_url_tracker.h" |
| 16 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 16 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/browser/prerender/prerender_manager.h" | 18 #include "chrome/browser/prerender/prerender_manager.h" |
| 19 #include "chrome/browser/prerender/prerender_manager_factory.h" | 19 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/tab_contents/tab_util.h" | 21 #include "chrome/browser/tab_contents/tab_util.h" |
| 22 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/browser_finder.h" | 23 #include "chrome/browser/ui/browser_finder.h" |
| 24 #include "chrome/browser/ui/browser_instant_controller.h" |
| 24 #include "chrome/browser/ui/browser_tab_contents.h" | 25 #include "chrome/browser/ui/browser_tab_contents.h" |
| 25 #include "chrome/browser/ui/browser_window.h" | 26 #include "chrome/browser/ui/browser_window.h" |
| 26 #include "chrome/browser/ui/host_desktop.h" | 27 #include "chrome/browser/ui/host_desktop.h" |
| 27 #include "chrome/browser/ui/omnibox/location_bar.h" | 28 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 28 #include "chrome/browser/ui/singleton_tabs.h" | 29 #include "chrome/browser/ui/singleton_tabs.h" |
| 29 #include "chrome/browser/ui/status_bubble.h" | 30 #include "chrome/browser/ui/status_bubble.h" |
| 30 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 31 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 31 #include "chrome/browser/web_applications/web_app.h" | 32 #include "chrome/browser/web_applications/web_app.h" |
| 32 #include "chrome/common/chrome_notification_types.h" | 33 #include "chrome/common/chrome_notification_types.h" |
| 33 #include "chrome/common/extensions/extension.h" | 34 #include "chrome/common/extensions/extension.h" |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 case NEW_FOREGROUND_TAB: | 222 case NEW_FOREGROUND_TAB: |
| 222 case SINGLETON_TAB: | 223 case SINGLETON_TAB: |
| 223 params->tabstrip_add_types |= TabStripModel::ADD_ACTIVE; | 224 params->tabstrip_add_types |= TabStripModel::ADD_ACTIVE; |
| 224 break; | 225 break; |
| 225 | 226 |
| 226 default: | 227 default: |
| 227 break; | 228 break; |
| 228 } | 229 } |
| 229 } | 230 } |
| 230 | 231 |
| 232 GURL NormalizeURL(chrome::NavigateParams* params) { |
| 233 if (params->url.is_empty()) { |
| 234 params->transition = content::PageTransitionFromInt( |
| 235 params->transition | content::PAGE_TRANSITION_HOME_PAGE); |
| 236 return params->browser->profile()->GetHomePage(); |
| 237 } |
| 238 return params->url; |
| 239 } |
| 240 |
| 231 // Obtain the profile used by the code that originated the Navigate() request. | 241 // Obtain the profile used by the code that originated the Navigate() request. |
| 232 Profile* GetSourceProfile(chrome::NavigateParams* params) { | 242 Profile* GetSourceProfile(chrome::NavigateParams* params) { |
| 233 if (params->source_contents) { | 243 if (params->source_contents) { |
| 234 return Profile::FromBrowserContext( | 244 return Profile::FromBrowserContext( |
| 235 params->source_contents->GetBrowserContext()); | 245 params->source_contents->GetBrowserContext()); |
| 236 } | 246 } |
| 237 | 247 |
| 238 return params->initiating_profile; | 248 return params->initiating_profile; |
| 239 } | 249 } |
| 240 | 250 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 WebContents* ReleaseOwnership() { | 312 WebContents* ReleaseOwnership() { |
| 303 return target_contents_owner_.release(); | 313 return target_contents_owner_.release(); |
| 304 } | 314 } |
| 305 | 315 |
| 306 private: | 316 private: |
| 307 chrome::NavigateParams* params_; | 317 chrome::NavigateParams* params_; |
| 308 scoped_ptr<WebContents> target_contents_owner_; | 318 scoped_ptr<WebContents> target_contents_owner_; |
| 309 DISALLOW_COPY_AND_ASSIGN(ScopedTargetContentsOwner); | 319 DISALLOW_COPY_AND_ASSIGN(ScopedTargetContentsOwner); |
| 310 }; | 320 }; |
| 311 | 321 |
| 322 content::WebContents* CreateTargetContents(chrome::NavigateParams* params, |
| 323 const GURL& url) { |
| 324 WebContents::CreateParams create_params( |
| 325 params->browser->profile(), |
| 326 tab_util::GetSiteInstanceForNewTab(params->browser->profile(), url)); |
| 327 if (params->source_contents) { |
| 328 create_params.initial_size = |
| 329 params->source_contents->GetView()->GetContainerSize(); |
| 330 } |
| 331 #if defined(USE_AURA) |
| 332 if (params->browser->window() && |
| 333 params->browser->window()->GetNativeWindow()) { |
| 334 create_params.context = |
| 335 params->browser->window()->GetNativeWindow(); |
| 336 } |
| 337 #endif |
| 338 |
| 339 content::WebContents* target_contents = WebContents::Create(create_params); |
| 340 // New tabs can have WebUI URLs that will make calls back to arbitrary |
| 341 // tab helpers, so the entire set of tab helpers needs to be set up |
| 342 // immediately. |
| 343 BrowserNavigatorWebContentsAdoption::AttachTabHelpers(target_contents); |
| 344 extensions::TabHelper::FromWebContents(target_contents)-> |
| 345 SetExtensionAppById(params->extension_app_id); |
| 346 // TODO(sky): Figure out why this is needed. Without it we seem to get |
| 347 // failures in startup tests. |
| 348 // By default, content believes it is not hidden. When adding contents |
| 349 // in the background, tell it that it's hidden. |
| 350 if ((params->tabstrip_add_types & TabStripModel::ADD_ACTIVE) == 0) { |
| 351 // TabStripModel::AddWebContents invokes WasHidden if not foreground. |
| 352 target_contents->WasHidden(); |
| 353 } |
| 354 return target_contents; |
| 355 } |
| 356 |
| 312 // If a prerendered page exists for |url|, replace the page at |target_contents| | 357 // If a prerendered page exists for |url|, replace the page at |target_contents| |
| 313 // with it. | 358 // with it. |
| 314 bool SwapInPrerender(WebContents* target_contents, const GURL& url) { | 359 bool SwapInPrerender(WebContents* target_contents, const GURL& url) { |
| 315 prerender::PrerenderManager* prerender_manager = | 360 prerender::PrerenderManager* prerender_manager = |
| 316 prerender::PrerenderManagerFactory::GetForProfile( | 361 prerender::PrerenderManagerFactory::GetForProfile( |
| 317 Profile::FromBrowserContext(target_contents->GetBrowserContext())); | 362 Profile::FromBrowserContext(target_contents->GetBrowserContext())); |
| 318 return prerender_manager && | 363 return prerender_manager && |
| 319 prerender_manager->MaybeUsePrerenderedPage(target_contents, url); | 364 prerender_manager->MaybeUsePrerenderedPage(target_contents, url); |
| 320 } | 365 } |
| 321 | 366 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 base_transition == content::PAGE_TRANSITION_TYPED || | 504 base_transition == content::PAGE_TRANSITION_TYPED || |
| 460 base_transition == content::PAGE_TRANSITION_AUTO_BOOKMARK || | 505 base_transition == content::PAGE_TRANSITION_AUTO_BOOKMARK || |
| 461 base_transition == content::PAGE_TRANSITION_GENERATED || | 506 base_transition == content::PAGE_TRANSITION_GENERATED || |
| 462 base_transition == content::PAGE_TRANSITION_AUTO_TOPLEVEL || | 507 base_transition == content::PAGE_TRANSITION_AUTO_TOPLEVEL || |
| 463 base_transition == content::PAGE_TRANSITION_RELOAD || | 508 base_transition == content::PAGE_TRANSITION_RELOAD || |
| 464 base_transition == content::PAGE_TRANSITION_KEYWORD; | 509 base_transition == content::PAGE_TRANSITION_KEYWORD; |
| 465 | 510 |
| 466 // Check if this is a singleton tab that already exists | 511 // Check if this is a singleton tab that already exists |
| 467 int singleton_index = chrome::GetIndexOfSingletonTab(params); | 512 int singleton_index = chrome::GetIndexOfSingletonTab(params); |
| 468 | 513 |
| 514 // Did we use Instant's NTP contents? |
| 515 bool swapped_in_instant = false; |
| 516 |
| 469 // If no target WebContents was specified, we need to construct one if | 517 // If no target WebContents was specified, we need to construct one if |
| 470 // we are supposed to target a new tab; unless it's a singleton that already | 518 // we are supposed to target a new tab; unless it's a singleton that already |
| 471 // exists. | 519 // exists. |
| 472 if (!params->target_contents && singleton_index < 0) { | 520 if (!params->target_contents && singleton_index < 0) { |
| 473 GURL url; | 521 GURL url = NormalizeURL(params); |
| 474 if (params->url.is_empty()) { | 522 BrowserInstantController* instant = params->browser->instant_controller(); |
| 475 url = params->browser->profile()->GetHomePage(); | |
| 476 params->transition = content::PageTransitionFromInt( | |
| 477 params->transition | content::PAGE_TRANSITION_HOME_PAGE); | |
| 478 } else { | |
| 479 url = params->url; | |
| 480 } | |
| 481 | |
| 482 if (params->disposition != CURRENT_TAB) { | 523 if (params->disposition != CURRENT_TAB) { |
| 483 WebContents::CreateParams create_params( | 524 swapped_in_instant = instant && instant->MaybeSwapInInstantContents( |
| 484 params->browser->profile(), | 525 url, NULL, ¶ms->target_contents); |
| 485 tab_util::GetSiteInstanceForNewTab(params->browser->profile(), url)); | 526 if (!swapped_in_instant) |
| 486 if (params->source_contents) { | 527 params->target_contents = CreateTargetContents(params, url); |
| 487 create_params.initial_size = | |
| 488 params->source_contents->GetView()->GetContainerSize(); | |
| 489 } | |
| 490 #if defined(USE_AURA) | |
| 491 if (params->browser->window() && | |
| 492 params->browser->window()->GetNativeWindow()) { | |
| 493 create_params.context = | |
| 494 params->browser->window()->GetNativeWindow(); | |
| 495 } | |
| 496 #endif | |
| 497 params->target_contents = WebContents::Create(create_params); | |
| 498 // New tabs can have WebUI URLs that will make calls back to arbitrary | |
| 499 // tab helpers, so the entire set of tab helpers needs to be set up | |
| 500 // immediately. | |
| 501 BrowserNavigatorWebContentsAdoption::AttachTabHelpers( | |
| 502 params->target_contents); | |
| 503 // This function takes ownership of |params->target_contents| until it | 528 // This function takes ownership of |params->target_contents| until it |
| 504 // is added to a TabStripModel. | 529 // is added to a TabStripModel. |
| 505 target_contents_owner.TakeOwnership(); | 530 target_contents_owner.TakeOwnership(); |
| 506 extensions::TabHelper::FromWebContents(params->target_contents)-> | |
| 507 SetExtensionAppById(params->extension_app_id); | |
| 508 // TODO(sky): Figure out why this is needed. Without it we seem to get | |
| 509 // failures in startup tests. | |
| 510 // By default, content believes it is not hidden. When adding contents | |
| 511 // in the background, tell it that it's hidden. | |
| 512 if ((params->tabstrip_add_types & TabStripModel::ADD_ACTIVE) == 0) { | |
| 513 // TabStripModel::AddWebContents invokes WasHidden if not foreground. | |
| 514 params->target_contents->WasHidden(); | |
| 515 } | |
| 516 } else { | 531 } else { |
| 517 // ... otherwise if we're loading in the current tab, the target is the | 532 // ... otherwise if we're loading in the current tab, the target is the |
| 518 // same as the source. | 533 // same as the source. |
| 519 params->target_contents = params->source_contents; | 534 DCHECK(params->source_contents); |
| 535 swapped_in_instant = instant && instant->MaybeSwapInInstantContents( |
| 536 url, params->source_contents, ¶ms->target_contents); |
| 537 if (!swapped_in_instant) |
| 538 params->target_contents = params->source_contents; |
| 520 DCHECK(params->target_contents); | 539 DCHECK(params->target_contents); |
| 521 } | 540 } |
| 522 | 541 |
| 523 if (user_initiated) | 542 if (user_initiated) |
| 524 params->target_contents->UserGestureDone(); | 543 params->target_contents->UserGestureDone(); |
| 525 | 544 |
| 526 if (SwapInPrerender(params->target_contents, url)) | 545 if (!swapped_in_instant) { |
| 527 return; | 546 if (SwapInPrerender(params->target_contents, url)) |
| 547 return; |
| 528 | 548 |
| 529 // Try to handle non-navigational URLs that popup dialogs and such, these | 549 // Try to handle non-navigational URLs that popup dialogs and such, these |
| 530 // should not actually navigate. | 550 // should not actually navigate. |
| 531 if (!HandleNonNavigationAboutURL(url)) { | 551 if (!HandleNonNavigationAboutURL(url)) { |
| 532 // Perform the actual navigation, tracking whether it came from the | 552 // Perform the actual navigation, tracking whether it came from the |
| 533 // renderer. | 553 // renderer. |
| 534 | 554 |
| 535 LoadURLInContents(params->target_contents, url, params); | 555 LoadURLInContents(params->target_contents, url, params); |
| 556 } |
| 536 } | 557 } |
| 537 } else { | 558 } else { |
| 538 // |target_contents| was specified non-NULL, and so we assume it has already | 559 // |target_contents| was specified non-NULL, and so we assume it has already |
| 539 // been navigated appropriately. We need to do nothing more other than | 560 // been navigated appropriately. We need to do nothing more other than |
| 540 // add it to the appropriate tabstrip. | 561 // add it to the appropriate tabstrip. |
| 541 } | 562 } |
| 542 | 563 |
| 543 // If the user navigated from the omnibox, and the selected tab is going to | 564 // If the user navigated from the omnibox, and the selected tab is going to |
| 544 // lose focus, then make sure the focus for the source tab goes away from the | 565 // lose focus, then make sure the focus for the source tab goes away from the |
| 545 // omnibox. | 566 // omnibox. |
| 546 if (params->source_contents && | 567 if (params->source_contents && |
| 547 (params->disposition == NEW_FOREGROUND_TAB || | 568 (params->disposition == NEW_FOREGROUND_TAB || |
| 548 params->disposition == NEW_WINDOW) && | 569 params->disposition == NEW_WINDOW) && |
| 549 (params->tabstrip_add_types & TabStripModel::ADD_INHERIT_OPENER)) | 570 (params->tabstrip_add_types & TabStripModel::ADD_INHERIT_OPENER)) |
| 550 params->source_contents->Focus(); | 571 params->source_contents->Focus(); |
| 551 | 572 |
| 552 if (params->source_contents == params->target_contents) { | 573 if (params->source_contents == params->target_contents || |
| 574 (swapped_in_instant && params->disposition == CURRENT_TAB)) { |
| 553 // The navigation occurred in the source tab. | 575 // The navigation occurred in the source tab. |
| 554 params->browser->UpdateUIForNavigationInTab(params->target_contents, | 576 params->browser->UpdateUIForNavigationInTab(params->target_contents, |
| 555 params->transition, | 577 params->transition, |
| 556 user_initiated); | 578 user_initiated); |
| 557 } else if (singleton_index == -1) { | 579 } else if (singleton_index == -1) { |
| 558 // If some non-default value is set for the index, we should tell the | 580 // If some non-default value is set for the index, we should tell the |
| 559 // TabStripModel to respect it. | 581 // TabStripModel to respect it. |
| 560 if (params->tabstrip_index != -1) | 582 if (params->tabstrip_index != -1) |
| 561 params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX; | 583 params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX; |
| 562 | 584 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 bool reverse_on_redirect = false; | 638 bool reverse_on_redirect = false; |
| 617 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary( | 639 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary( |
| 618 &rewritten_url, browser_context, &reverse_on_redirect); | 640 &rewritten_url, browser_context, &reverse_on_redirect); |
| 619 | 641 |
| 620 // Some URLs are mapped to uber subpages. Do not allow them in incognito. | 642 // Some URLs are mapped to uber subpages. Do not allow them in incognito. |
| 621 return !(rewritten_url.scheme() == chrome::kChromeUIScheme && | 643 return !(rewritten_url.scheme() == chrome::kChromeUIScheme && |
| 622 rewritten_url.host() == chrome::kChromeUIUberHost); | 644 rewritten_url.host() == chrome::kChromeUIUberHost); |
| 623 } | 645 } |
| 624 | 646 |
| 625 } // namespace chrome | 647 } // namespace chrome |
| OLD | NEW |