| 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/browser_navigator.h" | 5 #include "chrome/browser/browser_navigator.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
| 10 #include "chrome/browser/browser_url_handler.h" |
| 10 #include "chrome/browser/browser_window.h" | 11 #include "chrome/browser/browser_window.h" |
| 11 #include "chrome/browser/location_bar.h" | 12 #include "chrome/browser/location_bar.h" |
| 12 #include "chrome/browser/profile.h" | 13 #include "chrome/browser/profile.h" |
| 13 #include "chrome/browser/renderer_host/site_instance.h" | 14 #include "chrome/browser/renderer_host/site_instance.h" |
| 14 #include "chrome/browser/status_bubble.h" | 15 #include "chrome/browser/status_bubble.h" |
| 15 #include "chrome/browser/tabs/tab_strip_model.h" | 16 #include "chrome/browser/tabs/tab_strip_model.h" |
| 16 #include "chrome/browser/tab_contents/tab_contents.h" | 17 #include "chrome/browser/tab_contents/tab_contents.h" |
| 17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 41 | 42 |
| 42 // Finds an existing Browser compatible with |profile|, making a new one if no | 43 // Finds an existing Browser compatible with |profile|, making a new one if no |
| 43 // such Browser is located. | 44 // such Browser is located. |
| 44 Browser* GetOrCreateBrowser(Profile* profile) { | 45 Browser* GetOrCreateBrowser(Profile* profile) { |
| 45 Browser* browser = BrowserList::FindBrowserWithType(profile, | 46 Browser* browser = BrowserList::FindBrowserWithType(profile, |
| 46 Browser::TYPE_NORMAL, | 47 Browser::TYPE_NORMAL, |
| 47 false); | 48 false); |
| 48 return browser ? browser : Browser::Create(profile); | 49 return browser ? browser : Browser::Create(profile); |
| 49 } | 50 } |
| 50 | 51 |
| 52 // Returns true if two URLs are equal ignoring their ref (hash fragment). |
| 53 bool CompareURLsIgnoreRef(const GURL& url, const GURL& other) { |
| 54 if (url == other) |
| 55 return true; |
| 56 // If neither has a ref than there is no point in stripping the refs and |
| 57 // the URLs are different since the comparison failed in the previous if |
| 58 // statement. |
| 59 if (!url.has_ref() && !other.has_ref()) |
| 60 return false; |
| 61 url_canon::Replacements<char> replacements; |
| 62 replacements.ClearRef(); |
| 63 GURL url_no_ref = url.ReplaceComponents(replacements); |
| 64 GURL other_no_ref = other.ReplaceComponents(replacements); |
| 65 return url_no_ref == other_no_ref; |
| 66 } |
| 67 |
| 68 // Returns the index of an existing singleton tab in |params->browser| matching |
| 69 // the URL specified in |params|. |
| 70 int GetIndexOfSingletonTab(browser::NavigateParams* params) { |
| 71 if (params->disposition != SINGLETON_TAB) |
| 72 return -1; |
| 73 |
| 74 // In case the URL was rewritten by the BrowserURLHandler we need to ensure |
| 75 // that we do not open another URL that will get redirected to the rewritten |
| 76 // URL. |
| 77 GURL rewritten_url(params->url); |
| 78 bool reverse_on_redirect = false; |
| 79 BrowserURLHandler::RewriteURLIfNecessary(&rewritten_url, |
| 80 params->browser->profile(), |
| 81 &reverse_on_redirect); |
| 82 |
| 83 for (int i = 0; i < params->browser->tab_count(); ++i) { |
| 84 TabContents* tab = params->browser->GetTabContentsAt(i); |
| 85 if (CompareURLsIgnoreRef(tab->GetURL(), params->url) || |
| 86 CompareURLsIgnoreRef(tab->GetURL(), rewritten_url)) { |
| 87 params->target_contents = tab; |
| 88 return i; |
| 89 } |
| 90 } |
| 91 return -1; |
| 92 } |
| 93 |
| 51 // Returns a Browser that can host the navigation or tab addition specified in | 94 // Returns a Browser that can host the navigation or tab addition specified in |
| 52 // |params|. This might just return the same Browser specified in |params|, or | 95 // |params|. This might just return the same Browser specified in |params|, or |
| 53 // some other if that Browser is deemed incompatible. | 96 // some other if that Browser is deemed incompatible. |
| 54 Browser* GetBrowserForDisposition(browser::NavigateParams* params) { | 97 Browser* GetBrowserForDisposition(browser::NavigateParams* params) { |
| 55 // If no source TabContents was specified, we use the selected one from the | 98 // If no source TabContents was specified, we use the selected one from the |
| 56 // target browser. This must happen first, before GetBrowserForDisposition() | 99 // target browser. This must happen first, before GetBrowserForDisposition() |
| 57 // has a chance to replace |params->browser| with another one. | 100 // has a chance to replace |params->browser| with another one. |
| 58 if (!params->source_contents) | 101 if (!params->source_contents) |
| 59 params->source_contents = params->browser->GetSelectedTabContents(); | 102 params->source_contents = params->browser->GetSelectedTabContents(); |
| 60 | 103 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 tabstrip_index(-1), | 243 tabstrip_index(-1), |
| 201 tabstrip_add_types(TabStripModel::ADD_SELECTED), | 244 tabstrip_add_types(TabStripModel::ADD_SELECTED), |
| 202 show_window(false), | 245 show_window(false), |
| 203 browser(a_browser) { | 246 browser(a_browser) { |
| 204 DCHECK(browser); | 247 DCHECK(browser); |
| 205 } | 248 } |
| 206 | 249 |
| 207 NavigateParams::~NavigateParams() { | 250 NavigateParams::~NavigateParams() { |
| 208 } | 251 } |
| 209 | 252 |
| 210 void Navigate(NavigateParams* params, NavigatorDelegate* delegate) { | 253 void Navigate(NavigateParams* params) { |
| 211 DCHECK(delegate); | |
| 212 params->browser = GetBrowserForDisposition(params); | 254 params->browser = GetBrowserForDisposition(params); |
| 213 if (!params->browser) | 255 if (!params->browser) |
| 214 return; | 256 return; |
| 215 // Navigate() must not return early after this point. | 257 // Navigate() must not return early after this point. |
| 216 | 258 |
| 217 // Make sure the Browser is shown if params call for it. | 259 // Make sure the Browser is shown if params call for it. |
| 218 ScopedBrowserDisplayer displayer(params); | 260 ScopedBrowserDisplayer displayer(params); |
| 219 | 261 |
| 220 // Makes sure any TabContents created by this function is destroyed if | 262 // Makes sure any TabContents created by this function is destroyed if |
| 221 // not properly added to a tab strip. | 263 // not properly added to a tab strip. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 PageTransition::StripQualifier(params->transition); | 301 PageTransition::StripQualifier(params->transition); |
| 260 bool user_initiated = base_transition == PageTransition::TYPED || | 302 bool user_initiated = base_transition == PageTransition::TYPED || |
| 261 base_transition == PageTransition::AUTO_BOOKMARK; | 303 base_transition == PageTransition::AUTO_BOOKMARK; |
| 262 if (user_initiated) { | 304 if (user_initiated) { |
| 263 RenderViewHostDelegate::BrowserIntegration* integration = | 305 RenderViewHostDelegate::BrowserIntegration* integration = |
| 264 params->target_contents; | 306 params->target_contents; |
| 265 integration->OnUserGesture(); | 307 integration->OnUserGesture(); |
| 266 } | 308 } |
| 267 | 309 |
| 268 // Perform the actual navigation. | 310 // Perform the actual navigation. |
| 269 GURL url = params->url.is_empty() ? delegate->GetHomePage() : params->url; | 311 GURL url = params->url.is_empty() ? params->browser->GetHomePage() |
| 312 : params->url; |
| 270 params->target_contents->controller().LoadURL(url, params->referrer, | 313 params->target_contents->controller().LoadURL(url, params->referrer, |
| 271 params->transition); | 314 params->transition); |
| 272 | 315 |
| 273 // Update the UI for the navigation. | |
| 274 if (params->source_contents == params->target_contents) { | 316 if (params->source_contents == params->target_contents) { |
| 275 // The navigation occurred in the source tab. | 317 // The navigation occurred in the source tab, so update the UI. |
| 276 delegate->UpdateUIForNavigationInTab(params->target_contents, | 318 params->browser->UpdateUIForNavigationInTab(params->target_contents, |
| 277 params->transition, | 319 params->transition, |
| 278 user_initiated); | 320 user_initiated); |
| 279 } else { | 321 } else { |
| 280 // The navigation occurred in some other tab yet to be added to the | 322 // The navigation occurred in some other tab. |
| 281 // tabstrip. Add it now. | 323 int singleton_index = GetIndexOfSingletonTab(params); |
| 282 params->browser->tabstrip_model()->AddTabContents( | 324 if (params->disposition == SINGLETON_TAB && singleton_index >= 0) { |
| 283 params->target_contents, | 325 // The navigation should re-select an existing tab in the target Browser. |
| 284 params->tabstrip_index, | 326 params->browser->SelectTabContentsAt(singleton_index, user_initiated); |
| 285 params->transition, | 327 } else { |
| 286 params->tabstrip_add_types); | 328 // The navigation should insert a new tab into the target Browser. |
| 287 // Now that the |params->target_contents| is safely owned by the target | 329 params->browser->tabstrip_model()->AddTabContents( |
| 288 // Browser's TabStripModel, we can release ownership. | 330 params->target_contents, |
| 289 target_contents_owner.ReleaseOwnership(); | 331 params->tabstrip_index, |
| 332 params->transition, |
| 333 params->tabstrip_add_types); |
| 334 // Now that the |params->target_contents| is safely owned by the target |
| 335 // Browser's TabStripModel, we can release ownership. |
| 336 target_contents_owner.ReleaseOwnership(); |
| 337 } |
| 290 } | 338 } |
| 291 } | 339 } |
| 292 | 340 |
| 293 } // namespace browser | 341 } // namespace browser |
| 294 | 342 |
| OLD | NEW |