| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/browser_list.h" | 8 #include "chrome/browser/browser_list.h" |
| 9 #include "chrome/browser/browser_url_handler.h" | 9 #include "chrome/browser/browser_url_handler.h" |
| 10 #include "chrome/browser/browser_window.h" | 10 #include "chrome/browser/browser_window.h" |
| 11 #include "chrome/browser/location_bar.h" | 11 #include "chrome/browser/location_bar.h" |
| 12 #include "chrome/browser/profile.h" | 12 #include "chrome/browser/profile.h" |
| 13 #include "chrome/browser/renderer_host/site_instance.h" | 13 #include "chrome/browser/renderer_host/site_instance.h" |
| 14 #include "chrome/browser/status_bubble.h" | 14 #include "chrome/browser/status_bubble.h" |
| 15 #include "chrome/browser/tabs/tab_strip_model.h" | 15 #include "chrome/browser/tabs/tab_strip_model.h" |
| 16 #include "chrome/browser/tab_contents/tab_contents.h" | 16 #include "chrome/browser/tab_contents/tab_contents.h" |
| 17 #include "chrome/browser/tab_contents_wrapper.h" |
| 17 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Returns the SiteInstance for |source_contents| if it represents the same | 24 // Returns the SiteInstance for |source_contents| if it represents the same |
| 24 // website as |url|, or NULL otherwise. |source_contents| cannot be NULL. | 25 // website as |url|, or NULL otherwise. |source_contents| cannot be NULL. |
| 25 SiteInstance* GetSiteInstance(TabContents* source_contents, const GURL& url) { | 26 SiteInstance* GetSiteInstance(TabContents* source_contents, const GURL& url) { |
| 26 if (!source_contents) | 27 if (!source_contents) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // In case the URL was rewritten by the BrowserURLHandler we need to ensure | 79 // In case the URL was rewritten by the BrowserURLHandler we need to ensure |
| 79 // that we do not open another URL that will get redirected to the rewritten | 80 // that we do not open another URL that will get redirected to the rewritten |
| 80 // URL. | 81 // URL. |
| 81 GURL rewritten_url(params->url); | 82 GURL rewritten_url(params->url); |
| 82 bool reverse_on_redirect = false; | 83 bool reverse_on_redirect = false; |
| 83 BrowserURLHandler::RewriteURLIfNecessary(&rewritten_url, | 84 BrowserURLHandler::RewriteURLIfNecessary(&rewritten_url, |
| 84 params->browser->profile(), | 85 params->browser->profile(), |
| 85 &reverse_on_redirect); | 86 &reverse_on_redirect); |
| 86 | 87 |
| 87 for (int i = 0; i < params->browser->tab_count(); ++i) { | 88 for (int i = 0; i < params->browser->tab_count(); ++i) { |
| 88 TabContents* tab = params->browser->GetTabContentsAt(i); | 89 TabContentsWrapper* tab = params->browser->GetTabContentsWrapperAt(i); |
| 89 if (CompareURLsIgnoreRef(tab->GetURL(), params->url) || | 90 if (CompareURLsIgnoreRef(tab->tab_contents()->GetURL(), params->url) || |
| 90 CompareURLsIgnoreRef(tab->GetURL(), rewritten_url)) { | 91 CompareURLsIgnoreRef(tab->tab_contents()->GetURL(), rewritten_url)) { |
| 91 params->target_contents = tab; | 92 params->target_contents = tab; |
| 92 return i; | 93 return i; |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 return -1; | 96 return -1; |
| 96 } | 97 } |
| 97 | 98 |
| 98 // Change some of the navigation parameters based on the particular URL. | 99 // Change some of the navigation parameters based on the particular URL. |
| 99 // Currently this applies to chrome://settings and the bookmark manager, | 100 // Currently this applies to chrome://settings and the bookmark manager, |
| 100 // which we always want to open in a normal (not incognito) window. | 101 // which we always want to open in a normal (not incognito) window. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 118 } | 119 } |
| 119 | 120 |
| 120 // Returns a Browser that can host the navigation or tab addition specified in | 121 // Returns a Browser that can host the navigation or tab addition specified in |
| 121 // |params|. This might just return the same Browser specified in |params|, or | 122 // |params|. This might just return the same Browser specified in |params|, or |
| 122 // some other if that Browser is deemed incompatible. | 123 // some other if that Browser is deemed incompatible. |
| 123 Browser* GetBrowserForDisposition(browser::NavigateParams* params) { | 124 Browser* GetBrowserForDisposition(browser::NavigateParams* params) { |
| 124 // If no source TabContents was specified, we use the selected one from the | 125 // If no source TabContents was specified, we use the selected one from the |
| 125 // target browser. This must happen first, before GetBrowserForDisposition() | 126 // target browser. This must happen first, before GetBrowserForDisposition() |
| 126 // has a chance to replace |params->browser| with another one. | 127 // has a chance to replace |params->browser| with another one. |
| 127 if (!params->source_contents && params->browser) | 128 if (!params->source_contents && params->browser) |
| 128 params->source_contents = params->browser->GetSelectedTabContents(); | 129 params->source_contents = |
| 130 params->browser->GetSelectedTabContentsWrapper(); |
| 129 | 131 |
| 130 Profile* profile = | 132 Profile* profile = |
| 131 params->browser ? params->browser->profile() : params->profile; | 133 params->browser ? params->browser->profile() : params->profile; |
| 132 | 134 |
| 133 switch (params->disposition) { | 135 switch (params->disposition) { |
| 134 case CURRENT_TAB: | 136 case CURRENT_TAB: |
| 135 if (!params->browser && profile) { | 137 if (!params->browser && profile) { |
| 136 // We specified a profile instead of a browser; find or create one. | 138 // We specified a profile instead of a browser; find or create one. |
| 137 params->browser = Browser::GetOrCreateTabbedBrowser(profile); | 139 params->browser = Browser::GetOrCreateTabbedBrowser(profile); |
| 138 } | 140 } |
| 139 return params->browser; | 141 return params->browser; |
| 140 case SINGLETON_TAB: | 142 case SINGLETON_TAB: |
| 141 case NEW_FOREGROUND_TAB: | 143 case NEW_FOREGROUND_TAB: |
| 142 case NEW_BACKGROUND_TAB: | 144 case NEW_BACKGROUND_TAB: |
| 143 // See if we can open the tab in the window this navigator is bound to. | 145 // See if we can open the tab in the window this navigator is bound to. |
| 144 if (params->browser && WindowCanOpenTabs(params->browser)) | 146 if (params->browser && WindowCanOpenTabs(params->browser)) |
| 145 return params->browser; | 147 return params->browser; |
| 146 // Find a compatible window and re-execute this command in it. Otherwise | 148 // Find a compatible window and re-execute this command in it. Otherwise |
| 147 // re-run with NEW_WINDOW. | 149 // re-run with NEW_WINDOW. |
| 148 if (profile) | 150 if (profile) |
| 149 return GetOrCreateBrowser(profile); | 151 return GetOrCreateBrowser(profile); |
| 150 return NULL; | 152 return NULL; |
| 151 case NEW_POPUP: { | 153 case NEW_POPUP: { |
| 152 // Make a new popup window. Coerce app-style if |params->browser| or the | 154 // Make a new popup window. Coerce app-style if |params->browser| or the |
| 153 // |source| represents an app. | 155 // |source| represents an app. |
| 154 Browser::Type type = Browser::TYPE_POPUP; | 156 Browser::Type type = Browser::TYPE_POPUP; |
| 155 if ((params->browser && params->browser->type() == Browser::TYPE_APP) || | 157 if ((params->browser && params->browser->type() == Browser::TYPE_APP) || |
| 156 (params->source_contents && params->source_contents->is_app())) { | 158 (params->source_contents && |
| 159 params->source_contents->is_app())) { |
| 157 type = Browser::TYPE_APP_POPUP; | 160 type = Browser::TYPE_APP_POPUP; |
| 158 } | 161 } |
| 159 if (profile) { | 162 if (profile) { |
| 160 Browser* browser = new Browser(type, profile); | 163 Browser* browser = new Browser(type, profile); |
| 161 browser->set_override_bounds(params->window_bounds); | 164 browser->set_override_bounds(params->window_bounds); |
| 162 browser->CreateBrowserWindow(); | 165 browser->CreateBrowserWindow(); |
| 163 return browser; | 166 return browser; |
| 164 } | 167 } |
| 165 return NULL; | 168 return NULL; |
| 166 } | 169 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 params_->target_contents = NULL; | 253 params_->target_contents = NULL; |
| 251 } | 254 } |
| 252 | 255 |
| 253 // Assumes ownership of |params_|' target_contents until ReleaseOwnership | 256 // Assumes ownership of |params_|' target_contents until ReleaseOwnership |
| 254 // is called. | 257 // is called. |
| 255 void TakeOwnership() { | 258 void TakeOwnership() { |
| 256 target_contents_owner_.reset(params_->target_contents); | 259 target_contents_owner_.reset(params_->target_contents); |
| 257 } | 260 } |
| 258 | 261 |
| 259 // Relinquishes ownership of |params_|' target_contents. | 262 // Relinquishes ownership of |params_|' target_contents. |
| 260 TabContents* ReleaseOwnership() { | 263 TabContentsWrapper* ReleaseOwnership() { |
| 261 return target_contents_owner_.release(); | 264 return target_contents_owner_.release(); |
| 262 } | 265 } |
| 263 | 266 |
| 264 private: | 267 private: |
| 265 browser::NavigateParams* params_; | 268 browser::NavigateParams* params_; |
| 266 scoped_ptr<TabContents> target_contents_owner_; | 269 scoped_ptr<TabContentsWrapper> target_contents_owner_; |
| 267 DISALLOW_COPY_AND_ASSIGN(ScopedTargetContentsOwner); | 270 DISALLOW_COPY_AND_ASSIGN(ScopedTargetContentsOwner); |
| 268 }; | 271 }; |
| 269 | 272 |
| 270 } // namespace | 273 } // namespace |
| 271 | 274 |
| 272 namespace browser { | 275 namespace browser { |
| 273 | 276 |
| 274 NavigateParams::NavigateParams( | 277 NavigateParams::NavigateParams( |
| 275 Browser* a_browser, | 278 Browser* a_browser, |
| 276 const GURL& a_url, | 279 const GURL& a_url, |
| 277 PageTransition::Type a_transition) | 280 PageTransition::Type a_transition) |
| 278 : url(a_url), | 281 : url(a_url), |
| 279 target_contents(NULL), | 282 target_contents(NULL), |
| 280 source_contents(NULL), | 283 source_contents(NULL), |
| 281 disposition(CURRENT_TAB), | 284 disposition(CURRENT_TAB), |
| 282 transition(a_transition), | 285 transition(a_transition), |
| 283 tabstrip_index(-1), | 286 tabstrip_index(-1), |
| 284 tabstrip_add_types(TabStripModel::ADD_SELECTED), | 287 tabstrip_add_types(TabStripModel::ADD_SELECTED), |
| 285 show_window(false), | 288 show_window(false), |
| 286 browser(a_browser), | 289 browser(a_browser), |
| 287 profile(NULL) { | 290 profile(NULL) { |
| 288 } | 291 } |
| 289 | 292 |
| 290 NavigateParams::NavigateParams(Browser* a_browser, | 293 NavigateParams::NavigateParams(Browser* a_browser, |
| 291 TabContents* a_target_contents) | 294 TabContentsWrapper* a_target_contents) |
| 292 : target_contents(a_target_contents), | 295 : target_contents(a_target_contents), |
| 293 source_contents(NULL), | 296 source_contents(NULL), |
| 294 disposition(CURRENT_TAB), | 297 disposition(CURRENT_TAB), |
| 295 transition(PageTransition::LINK), | 298 transition(PageTransition::LINK), |
| 296 tabstrip_index(-1), | 299 tabstrip_index(-1), |
| 297 tabstrip_add_types(TabStripModel::ADD_SELECTED), | 300 tabstrip_add_types(TabStripModel::ADD_SELECTED), |
| 298 show_window(false), | 301 show_window(false), |
| 299 browser(a_browser), | 302 browser(a_browser), |
| 300 profile(NULL) { | 303 profile(NULL) { |
| 301 } | 304 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 325 // inform the target TabContents, and we may need to update the UI. | 328 // inform the target TabContents, and we may need to update the UI. |
| 326 PageTransition::Type base_transition = | 329 PageTransition::Type base_transition = |
| 327 PageTransition::StripQualifier(params->transition); | 330 PageTransition::StripQualifier(params->transition); |
| 328 bool user_initiated = base_transition == PageTransition::TYPED || | 331 bool user_initiated = base_transition == PageTransition::TYPED || |
| 329 base_transition == PageTransition::AUTO_BOOKMARK; | 332 base_transition == PageTransition::AUTO_BOOKMARK; |
| 330 | 333 |
| 331 // If no target TabContents was specified, we need to construct one if we are | 334 // If no target TabContents was specified, we need to construct one if we are |
| 332 // supposed to target a new tab. | 335 // supposed to target a new tab. |
| 333 if (!params->target_contents) { | 336 if (!params->target_contents) { |
| 334 if (params->disposition != CURRENT_TAB) { | 337 if (params->disposition != CURRENT_TAB) { |
| 338 TabContents* source_contents = params->source_contents ? |
| 339 params->source_contents->tab_contents() : NULL; |
| 335 params->target_contents = | 340 params->target_contents = |
| 336 new TabContents(params->browser->profile(), | 341 Browser::TabContentsFactory( |
| 337 GetSiteInstance(params->source_contents, params->url), | 342 params->browser->profile(), |
| 338 MSG_ROUTING_NONE, | 343 GetSiteInstance(source_contents, params->url), |
| 339 params->source_contents, | 344 MSG_ROUTING_NONE, |
| 340 NULL); | 345 source_contents, |
| 346 NULL); |
| 341 // This function takes ownership of |params->target_contents| until it | 347 // This function takes ownership of |params->target_contents| until it |
| 342 // is added to a TabStripModel. | 348 // is added to a TabStripModel. |
| 343 target_contents_owner.TakeOwnership(); | 349 target_contents_owner.TakeOwnership(); |
| 344 params->target_contents->SetExtensionAppById(params->extension_app_id); | 350 params->target_contents->SetExtensionAppById(params->extension_app_id); |
| 345 // TODO(sky): figure out why this is needed. Without it we seem to get | 351 // TODO(sky): figure out why this is needed. Without it we seem to get |
| 346 // failures in startup tests. | 352 // failures in startup tests. |
| 347 // By default, content believes it is not hidden. When adding contents | 353 // By default, content believes it is not hidden. When adding contents |
| 348 // in the background, tell it that it's hidden. | 354 // in the background, tell it that it's hidden. |
| 349 if ((params->tabstrip_add_types & TabStripModel::ADD_SELECTED) == 0) { | 355 if ((params->tabstrip_add_types & TabStripModel::ADD_SELECTED) == 0) { |
| 350 // TabStripModel::AddTabContents invokes HideContents if not foreground. | 356 // TabStripModel::AddTabContents invokes HideContents if not foreground. |
| 351 params->target_contents->WasHidden(); | 357 params->target_contents->tab_contents()->WasHidden(); |
| 352 } | 358 } |
| 353 } else { | 359 } else { |
| 354 // ... otherwise if we're loading in the current tab, the target is the | 360 // ... otherwise if we're loading in the current tab, the target is the |
| 355 // same as the source. | 361 // same as the source. |
| 356 params->target_contents = params->source_contents; | 362 params->target_contents = params->source_contents; |
| 357 DCHECK(params->target_contents); | 363 DCHECK(params->target_contents); |
| 358 } | 364 } |
| 359 | 365 |
| 360 if (user_initiated) { | 366 if (user_initiated) { |
| 361 RenderViewHostDelegate::BrowserIntegration* integration = | 367 RenderViewHostDelegate::BrowserIntegration* integration = |
| 362 params->target_contents; | 368 params->target_contents->tab_contents(); |
| 363 integration->OnUserGesture(); | 369 integration->OnUserGesture(); |
| 364 } | 370 } |
| 365 | 371 |
| 366 // Perform the actual navigation. | 372 // Perform the actual navigation. |
| 367 GURL url = params->url.is_empty() ? params->browser->GetHomePage() | 373 GURL url = params->url.is_empty() ? params->browser->GetHomePage() |
| 368 : params->url; | 374 : params->url; |
| 369 params->target_contents->controller().LoadURL(url, params->referrer, | 375 params->target_contents->controller().LoadURL(url, params->referrer, |
| 370 params->transition); | 376 params->transition); |
| 371 } else { | 377 } else { |
| 372 // |target_contents| was specified non-NULL, and so we assume it has already | 378 // |target_contents| was specified non-NULL, and so we assume it has already |
| 373 // been navigated appropriately. We need to do nothing more other than | 379 // been navigated appropriately. We need to do nothing more other than |
| 374 // add it to the appropriate tabstrip. | 380 // add it to the appropriate tabstrip. |
| 375 } | 381 } |
| 376 | 382 |
| 377 if (params->source_contents == params->target_contents) { | 383 if (params->source_contents == params->target_contents) { |
| 378 // The navigation occurred in the source tab, so update the UI. | 384 // The navigation occurred in the source tab. |
| 379 params->browser->UpdateUIForNavigationInTab(params->target_contents, | 385 params->browser->UpdateUIForNavigationInTab( |
| 380 params->transition, | 386 params->target_contents, |
| 381 user_initiated); | 387 params->transition, |
| 388 user_initiated); |
| 382 } else { | 389 } else { |
| 383 // The navigation occurred in some other tab. | 390 // The navigation occurred in some other tab. |
| 384 int singleton_index = GetIndexOfSingletonTab(params); | 391 int singleton_index = GetIndexOfSingletonTab(params); |
| 385 if (params->disposition == SINGLETON_TAB && singleton_index >= 0) { | 392 if (params->disposition == SINGLETON_TAB && singleton_index >= 0) { |
| 386 // The navigation should re-select an existing tab in the target Browser. | 393 // The navigation should re-select an existing tab in the target Browser. |
| 387 params->browser->SelectTabContentsAt(singleton_index, user_initiated); | 394 params->browser->SelectTabContentsAt(singleton_index, user_initiated); |
| 388 } else { | 395 } else { |
| 389 // If some non-default value is set for the index, we should tell the | 396 // If some non-default value is set for the index, we should tell the |
| 390 // TabStripModel to respect it. | 397 // TabStripModel to respect it. |
| 391 if (params->tabstrip_index != -1) | 398 if (params->tabstrip_index != -1) |
| 392 params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX; | 399 params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX; |
| 393 | 400 |
| 394 // The navigation should insert a new tab into the target Browser. | 401 // The navigation should insert a new tab into the target Browser. |
| 395 params->browser->tabstrip_model()->AddTabContents( | 402 params->browser->tabstrip_model()->AddTabContents( |
| 396 params->target_contents, | 403 params->target_contents, |
| 397 params->tabstrip_index, | 404 params->tabstrip_index, |
| 398 params->transition, | 405 params->transition, |
| 399 params->tabstrip_add_types); | 406 params->tabstrip_add_types); |
| 400 // Now that the |params->target_contents| is safely owned by the target | 407 // Now that the |params->target_contents| is safely owned by the target |
| 401 // Browser's TabStripModel, we can release ownership. | 408 // Browser's TabStripModel, we can release ownership. |
| 402 target_contents_owner.ReleaseOwnership(); | 409 target_contents_owner.ReleaseOwnership(); |
| 403 } | 410 } |
| 404 } | 411 } |
| 405 } | 412 } |
| 406 | 413 |
| 407 } // namespace browser | 414 } // namespace browser |
| OLD | NEW |