| 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" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 // Finds an existing Browser compatible with |profile|, making a new one if no | 47 // Finds an existing Browser compatible with |profile|, making a new one if no |
| 48 // such Browser is located. | 48 // such Browser is located. |
| 49 Browser* GetOrCreateBrowser(Profile* profile) { | 49 Browser* GetOrCreateBrowser(Profile* profile) { |
| 50 Browser* browser = BrowserList::FindBrowserWithType(profile, | 50 Browser* browser = BrowserList::FindBrowserWithType(profile, |
| 51 Browser::TYPE_NORMAL, | 51 Browser::TYPE_NORMAL, |
| 52 false); | 52 false); |
| 53 return browser ? browser : Browser::Create(profile); | 53 return browser ? browser : Browser::Create(profile); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Returns true if two URLs are equal ignoring their ref (hash fragment). | 56 // Returns true if two URLs are equal after taking |replacements| into account. |
| 57 bool CompareURLsIgnoreRef(const GURL& url, const GURL& other) { | 57 bool CompareURLsWithReplacements( |
| 58 const GURL& url, |
| 59 const GURL& other, |
| 60 const url_canon::Replacements<char>& replacements) { |
| 58 if (url == other) | 61 if (url == other) |
| 59 return true; | 62 return true; |
| 60 // If neither has a ref than there is no point in stripping the refs and | 63 |
| 61 // the URLs are different since the comparison failed in the previous if | 64 GURL url_replaced = url.ReplaceComponents(replacements); |
| 62 // statement. | 65 GURL other_replaced = other.ReplaceComponents(replacements); |
| 63 if (!url.has_ref() && !other.has_ref()) | 66 return url_replaced == other_replaced; |
| 64 return false; | |
| 65 url_canon::Replacements<char> replacements; | |
| 66 replacements.ClearRef(); | |
| 67 GURL url_no_ref = url.ReplaceComponents(replacements); | |
| 68 GURL other_no_ref = other.ReplaceComponents(replacements); | |
| 69 return url_no_ref == other_no_ref; | |
| 70 } | 67 } |
| 71 | 68 |
| 72 // Returns the index of an existing singleton tab in |params->browser| matching | 69 // Returns the index of an existing singleton tab in |params->browser| matching |
| 73 // the URL specified in |params|. | 70 // the URL specified in |params|. |
| 74 int GetIndexOfSingletonTab(browser::NavigateParams* params) { | 71 int GetIndexOfSingletonTab(browser::NavigateParams* params) { |
| 75 if (params->disposition != SINGLETON_TAB) | 72 if (params->disposition != SINGLETON_TAB) |
| 76 return -1; | 73 return -1; |
| 77 | 74 |
| 78 // In case the URL was rewritten by the BrowserURLHandler we need to ensure | 75 // 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 | 76 // that we do not open another URL that will get redirected to the rewritten |
| 80 // URL. | 77 // URL. |
| 81 GURL rewritten_url(params->url); | 78 GURL rewritten_url(params->url); |
| 82 bool reverse_on_redirect = false; | 79 bool reverse_on_redirect = false; |
| 83 BrowserURLHandler::RewriteURLIfNecessary(&rewritten_url, | 80 BrowserURLHandler::RewriteURLIfNecessary(&rewritten_url, |
| 84 params->browser->profile(), | 81 params->browser->profile(), |
| 85 &reverse_on_redirect); | 82 &reverse_on_redirect); |
| 86 | 83 |
| 87 for (int i = 0; i < params->browser->tab_count(); ++i) { | 84 for (int i = 0; i < params->browser->tab_count(); ++i) { |
| 88 TabContents* tab = params->browser->GetTabContentsAt(i); | 85 TabContents* tab = params->browser->GetTabContentsAt(i); |
| 89 if (CompareURLsIgnoreRef(tab->GetURL(), params->url) || | 86 |
| 90 CompareURLsIgnoreRef(tab->GetURL(), rewritten_url)) { | 87 url_canon::Replacements<char> replacements; |
| 88 replacements.ClearRef(); |
| 89 if (params->ignore_path) |
| 90 replacements.ClearPath(); |
| 91 |
| 92 if (CompareURLsWithReplacements(tab->GetURL(), params->url, replacements) || |
| 93 CompareURLsWithReplacements(tab->GetURL(), rewritten_url, |
| 94 replacements)) { |
| 91 params->target_contents = tab; | 95 params->target_contents = tab; |
| 92 return i; | 96 return i; |
| 93 } | 97 } |
| 94 } | 98 } |
| 99 |
| 95 return -1; | 100 return -1; |
| 96 } | 101 } |
| 97 | 102 |
| 98 // Change some of the navigation parameters based on the particular URL. | 103 // Change some of the navigation parameters based on the particular URL. |
| 99 // Currently this applies to chrome://settings and the bookmark manager, | 104 // Currently this applies to chrome://settings and the bookmark manager, |
| 100 // which we always want to open in a normal (not incognito) window. | 105 // which we always want to open in a normal (not incognito) window. |
| 101 void AdjustNavigateParamsForURL(browser::NavigateParams* params) { | 106 void AdjustNavigateParamsForURL(browser::NavigateParams* params) { |
| 102 if (!params->target_contents && | 107 if (!params->target_contents && |
| 103 params->url.scheme() == chrome::kChromeUIScheme && | 108 params->url.scheme() == chrome::kChromeUIScheme && |
| 104 (params->url.host() == chrome::kChromeUISettingsHost || | 109 (params->url.host() == chrome::kChromeUISettingsHost || |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 | 381 |
| 377 if (params->source_contents == params->target_contents) { | 382 if (params->source_contents == params->target_contents) { |
| 378 // The navigation occurred in the source tab, so update the UI. | 383 // The navigation occurred in the source tab, so update the UI. |
| 379 params->browser->UpdateUIForNavigationInTab(params->target_contents, | 384 params->browser->UpdateUIForNavigationInTab(params->target_contents, |
| 380 params->transition, | 385 params->transition, |
| 381 user_initiated); | 386 user_initiated); |
| 382 } else { | 387 } else { |
| 383 // The navigation occurred in some other tab. | 388 // The navigation occurred in some other tab. |
| 384 int singleton_index = GetIndexOfSingletonTab(params); | 389 int singleton_index = GetIndexOfSingletonTab(params); |
| 385 if (params->disposition == SINGLETON_TAB && singleton_index >= 0) { | 390 if (params->disposition == SINGLETON_TAB && singleton_index >= 0) { |
| 391 TabContents* target = params->browser->GetTabContentsAt(singleton_index); |
| 392 |
| 393 // Load the URL if the target contents URL doesn't match. This can happen |
| 394 // if the URL path is ignored when locating the singleton tab. |
| 395 if (target->GetURL() != params->url) { |
| 396 target->controller().LoadURL( |
| 397 params->url, params->referrer, params->transition); |
| 398 } |
| 399 |
| 386 // The navigation should re-select an existing tab in the target Browser. | 400 // The navigation should re-select an existing tab in the target Browser. |
| 387 params->browser->SelectTabContentsAt(singleton_index, user_initiated); | 401 params->browser->SelectTabContentsAt(singleton_index, user_initiated); |
| 388 } else { | 402 } else { |
| 389 // If some non-default value is set for the index, we should tell the | 403 // If some non-default value is set for the index, we should tell the |
| 390 // TabStripModel to respect it. | 404 // TabStripModel to respect it. |
| 391 if (params->tabstrip_index != -1) | 405 if (params->tabstrip_index != -1) |
| 392 params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX; | 406 params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX; |
| 393 | 407 |
| 394 // The navigation should insert a new tab into the target Browser. | 408 // The navigation should insert a new tab into the target Browser. |
| 395 params->browser->tabstrip_model()->AddTabContents( | 409 params->browser->tabstrip_model()->AddTabContents( |
| 396 params->target_contents, | 410 params->target_contents, |
| 397 params->tabstrip_index, | 411 params->tabstrip_index, |
| 398 params->transition, | 412 params->transition, |
| 399 params->tabstrip_add_types); | 413 params->tabstrip_add_types); |
| 400 // Now that the |params->target_contents| is safely owned by the target | 414 // Now that the |params->target_contents| is safely owned by the target |
| 401 // Browser's TabStripModel, we can release ownership. | 415 // Browser's TabStripModel, we can release ownership. |
| 402 target_contents_owner.ReleaseOwnership(); | 416 target_contents_owner.ReleaseOwnership(); |
| 403 } | 417 } |
| 404 } | 418 } |
| 405 } | 419 } |
| 406 | 420 |
| 407 } // namespace browser | 421 } // namespace browser |
| OLD | NEW |