Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: chrome/browser/ui/browser_navigator.cc

Issue 10696222: Make chrome::Navigate take in a Browser* that is never NULL. That function doesn't have a context, … (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 // technically an incognito window, these URLs are allowed. 67 // technically an incognito window, these URLs are allowed.
68 // Returns true on success. Otherwise, if changing params leads the browser into 68 // Returns true on success. Otherwise, if changing params leads the browser into
69 // an erroneous state, returns false. 69 // an erroneous state, returns false.
70 bool AdjustNavigateParamsForURL(chrome::NavigateParams* params) { 70 bool AdjustNavigateParamsForURL(chrome::NavigateParams* params) {
71 if (params->target_contents != NULL || 71 if (params->target_contents != NULL ||
72 chrome::IsURLAllowedInIncognito(params->url) || 72 chrome::IsURLAllowedInIncognito(params->url) ||
73 Profile::IsGuestSession()) { 73 Profile::IsGuestSession()) {
74 return true; 74 return true;
75 } 75 }
76 76
77 Profile* profile = 77 Profile* profile = params->browser->profile();
78 params->browser ? params->browser->profile() : params->profile;
79 78
80 if (profile->IsOffTheRecord() || params->disposition == OFF_THE_RECORD) { 79 if (profile->IsOffTheRecord() || params->disposition == OFF_THE_RECORD) {
81 profile = profile->GetOriginalProfile(); 80 profile = profile->GetOriginalProfile();
82 81
83 // If incognito is forced, we punt. 82 // If incognito is forced, we punt.
84 PrefService* prefs = profile->GetPrefs(); 83 PrefService* prefs = profile->GetPrefs();
85 if (prefs && IncognitoModePrefs::GetAvailability(prefs) == 84 if (prefs && IncognitoModePrefs::GetAvailability(prefs) ==
86 IncognitoModePrefs::FORCED) { 85 IncognitoModePrefs::FORCED) {
87 return false; 86 return false;
88 } 87 }
89 88
90 params->disposition = SINGLETON_TAB; 89 params->disposition = SINGLETON_TAB;
91 params->profile = profile;
92 params->browser = browser::FindOrCreateTabbedBrowser(profile); 90 params->browser = browser::FindOrCreateTabbedBrowser(profile);
93 params->window_action = chrome::NavigateParams::SHOW_WINDOW; 91 params->window_action = chrome::NavigateParams::SHOW_WINDOW;
94 } 92 }
95 93
96 return true; 94 return true;
97 } 95 }
98 96
99 // Returns a Browser that can host the navigation or tab addition specified in 97 // Returns a Browser that can host the navigation or tab addition specified in
100 // |params|. This might just return the same Browser specified in |params|, or 98 // |params|. This might just return the same Browser specified in |params|, or
101 // some other if that Browser is deemed incompatible. 99 // some other if that Browser is deemed incompatible.
102 Browser* GetBrowserForDisposition(chrome::NavigateParams* params) { 100 Browser* GetBrowserForDisposition(chrome::NavigateParams* params) {
103 // If no source TabContents was specified, we use the selected one from 101 // If no source TabContents was specified, we use the selected one from
104 // the target browser. This must happen first, before 102 // the target browser. This must happen first, before
105 // GetBrowserForDisposition() has a chance to replace |params->browser| with 103 // GetBrowserForDisposition() has a chance to replace |params->browser| with
106 // another one. 104 // another one.
107 if (!params->source_contents && params->browser) 105 if (!params->source_contents)
108 params->source_contents = chrome::GetActiveTabContents(params->browser); 106 params->source_contents = chrome::GetActiveTabContents(params->browser);
109 107
110 Profile* profile = 108 Profile* profile = params->browser->profile();
111 params->browser ? params->browser->profile() : params->profile;
112 109
113 switch (params->disposition) { 110 switch (params->disposition) {
114 case CURRENT_TAB: 111 case CURRENT_TAB:
115 if (!params->browser && profile) {
116 // We specified a profile instead of a browser; find or create one.
117 params->browser = browser::FindOrCreateTabbedBrowser(profile);
118 }
119 return params->browser; 112 return params->browser;
120 case SINGLETON_TAB: 113 case SINGLETON_TAB:
121 case NEW_FOREGROUND_TAB: 114 case NEW_FOREGROUND_TAB:
122 case NEW_BACKGROUND_TAB: 115 case NEW_BACKGROUND_TAB:
123 // See if we can open the tab in the window this navigator is bound to. 116 // See if we can open the tab in the window this navigator is bound to.
124 if (params->browser && WindowCanOpenTabs(params->browser)) 117 if (WindowCanOpenTabs(params->browser))
125 return params->browser; 118 return params->browser;
126 // Find a compatible window and re-execute this command in it. Otherwise 119 // Find a compatible window and re-execute this command in it. Otherwise
127 // re-run with NEW_WINDOW. 120 // re-run with NEW_WINDOW.
128 if (profile) 121 return GetOrCreateBrowser(profile);
129 return GetOrCreateBrowser(profile);
130 return NULL;
131 case NEW_POPUP: { 122 case NEW_POPUP: {
132 // Make a new popup window. 123 // Make a new popup window.
133 if (profile) { 124 // Coerce app-style if |source| represents an app.
134 // Coerce app-style if |params->browser| or |source| represents an app. 125 std::string app_name;
135 std::string app_name; 126 if (!params->extension_app_id.empty()) {
136 if (!params->extension_app_id.empty()) { 127 app_name = web_app::GenerateApplicationNameFromExtensionId(
137 app_name = web_app::GenerateApplicationNameFromExtensionId( 128 params->extension_app_id);
138 params->extension_app_id); 129 } else if (!params->browser->app_name().empty()) {
139 } else if (params->browser && !params->browser->app_name().empty()) { 130 app_name = params->browser->app_name();
140 app_name = params->browser->app_name(); 131 } else if (params->source_contents &&
141 } else if (params->source_contents && 132 params->source_contents->extension_tab_helper()->is_app()) {
142 params->source_contents->extension_tab_helper()->is_app()) { 133 app_name = web_app::GenerateApplicationNameFromExtensionId(
143 app_name = web_app::GenerateApplicationNameFromExtensionId( 134 params->source_contents->extension_tab_helper()->
144 params->source_contents->extension_tab_helper()-> 135 extension_app()->id());
145 extension_app()->id());
146 }
147 if (app_name.empty()) {
148 Browser::CreateParams browser_params(Browser::TYPE_POPUP, profile);
149 browser_params.initial_bounds = params->window_bounds;
150 return Browser::CreateWithParams(browser_params);
151 } else {
152 return Browser::CreateWithParams(
153 Browser::CreateParams::CreateForApp(
154 Browser::TYPE_POPUP, app_name, params->window_bounds,
155 profile));
156 }
157 } 136 }
158 return NULL; 137 if (app_name.empty()) {
138 Browser::CreateParams browser_params(Browser::TYPE_POPUP, profile);
139 browser_params.initial_bounds = params->window_bounds;
140 return Browser::CreateWithParams(browser_params);
141 }
142
143 return Browser::CreateWithParams(
144 Browser::CreateParams::CreateForApp(
145 Browser::TYPE_POPUP, app_name, params->window_bounds,
146 profile));
159 } 147 }
160 case NEW_WINDOW: 148 case NEW_WINDOW: {
161 // Make a new normal browser window. 149 // Make a new normal browser window.
162 if (profile) { 150 Browser* browser = new Browser(Browser::TYPE_TABBED, profile);
163 Browser* browser = new Browser(Browser::TYPE_TABBED, profile); 151 browser->InitBrowserWindow();
164 browser->InitBrowserWindow(); 152 return browser;
165 return browser; 153 }
166 }
167 return NULL;
168 case OFF_THE_RECORD: 154 case OFF_THE_RECORD:
169 // Make or find an incognito window. 155 // Make or find an incognito window.
170 if (profile) 156 return GetOrCreateBrowser(profile->GetOffTheRecordProfile());
171 return GetOrCreateBrowser(profile->GetOffTheRecordProfile());
172 return NULL;
173 // The following types all result in no navigation. 157 // The following types all result in no navigation.
174 case SUPPRESS_OPEN: 158 case SUPPRESS_OPEN:
175 case SAVE_TO_DISK: 159 case SAVE_TO_DISK:
176 case IGNORE_ACTION: 160 case IGNORE_ACTION:
177 return NULL; 161 return NULL;
178 default: 162 default:
179 NOTREACHED(); 163 NOTREACHED();
180 } 164 }
181 return NULL; 165 return NULL;
182 } 166 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 } 208 }
225 209
226 // Obtain the profile used by the code that originated the Navigate() request. 210 // Obtain the profile used by the code that originated the Navigate() request.
227 // |source_browser| represents the Browser that was supplied in |params| before 211 // |source_browser| represents the Browser that was supplied in |params| before
228 // it was modified. 212 // it was modified.
229 Profile* GetSourceProfile(chrome::NavigateParams* params, 213 Profile* GetSourceProfile(chrome::NavigateParams* params,
230 Browser* source_browser) { 214 Browser* source_browser) {
231 if (params->source_contents) 215 if (params->source_contents)
232 return params->source_contents->profile(); 216 return params->source_contents->profile();
233 217
234 if (source_browser) 218 return source_browser->profile();
235 return source_browser->profile();
236
237 if (params->profile)
238 return params->profile;
239
240 // We couldn't find one in any of the source metadata, so we'll fall back to
241 // the profile associated with the target browser.
242 return params->browser->profile();
243 } 219 }
244 220
245 void LoadURLInContents(WebContents* target_contents, 221 void LoadURLInContents(WebContents* target_contents,
246 const GURL& url, 222 const GURL& url,
247 chrome::NavigateParams* params, 223 chrome::NavigateParams* params,
248 const std::string& extra_headers) { 224 const std::string& extra_headers) {
249 if (params->transferred_global_request_id != GlobalRequestID()) { 225 if (params->transferred_global_request_id != GlobalRequestID()) {
250 target_contents->GetController().TransferURL( 226 target_contents->GetController().TransferURL(
251 url, 227 url,
252 params->referrer, 228 params->referrer,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 private: 292 private:
317 chrome::NavigateParams* params_; 293 chrome::NavigateParams* params_;
318 scoped_ptr<TabContents> target_contents_owner_; 294 scoped_ptr<TabContents> target_contents_owner_;
319 DISALLOW_COPY_AND_ASSIGN(ScopedTargetContentsOwner); 295 DISALLOW_COPY_AND_ASSIGN(ScopedTargetContentsOwner);
320 }; 296 };
321 297
322 void InitializeExtraHeaders(chrome::NavigateParams* params, 298 void InitializeExtraHeaders(chrome::NavigateParams* params,
323 Profile* profile, 299 Profile* profile,
324 std::string* extra_headers) { 300 std::string* extra_headers) {
325 #if defined(ENABLE_RLZ) 301 #if defined(ENABLE_RLZ)
326 if (!profile)
327 profile = params->profile;
328
329 // If this is a home page navigation, check to see if the home page is 302 // If this is a home page navigation, check to see if the home page is
330 // set to Google and add RLZ HTTP headers to the request. This is only 303 // set to Google and add RLZ HTTP headers to the request. This is only
331 // done if Google was the original home page, and not changed afterwards by 304 // done if Google was the original home page, and not changed afterwards by
332 // the user. 305 // the user.
333 if (profile && 306 if (profile &&
334 (params->transition & content::PAGE_TRANSITION_HOME_PAGE) != 0) { 307 (params->transition & content::PAGE_TRANSITION_HOME_PAGE) != 0) {
335 PrefService* pref_service = profile->GetPrefs(); 308 PrefService* pref_service = profile->GetPrefs();
336 if (pref_service) { 309 if (pref_service) {
337 if (!pref_service->GetBoolean(prefs::kHomePageChanged)) { 310 if (!pref_service->GetBoolean(prefs::kHomePageChanged)) {
338 std::string homepage = pref_service->GetString(prefs::kHomePage); 311 std::string homepage = pref_service->GetString(prefs::kHomePage);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 source_contents(NULL), 347 source_contents(NULL),
375 disposition(CURRENT_TAB), 348 disposition(CURRENT_TAB),
376 transition(a_transition), 349 transition(a_transition),
377 is_renderer_initiated(false), 350 is_renderer_initiated(false),
378 tabstrip_index(-1), 351 tabstrip_index(-1),
379 tabstrip_add_types(TabStripModel::ADD_ACTIVE), 352 tabstrip_add_types(TabStripModel::ADD_ACTIVE),
380 window_action(NO_ACTION), 353 window_action(NO_ACTION),
381 user_gesture(true), 354 user_gesture(true),
382 path_behavior(RESPECT), 355 path_behavior(RESPECT),
383 ref_behavior(IGNORE_REF), 356 ref_behavior(IGNORE_REF),
384 browser(a_browser), 357 browser(a_browser) {
385 profile(NULL) {
386 } 358 }
387 359
388 NavigateParams::NavigateParams(Browser* a_browser, 360 NavigateParams::NavigateParams(Browser* a_browser,
389 TabContents* a_target_contents) 361 TabContents* a_target_contents)
390 : target_contents(a_target_contents), 362 : target_contents(a_target_contents),
391 source_contents(NULL), 363 source_contents(NULL),
392 disposition(CURRENT_TAB), 364 disposition(CURRENT_TAB),
393 transition(content::PAGE_TRANSITION_LINK), 365 transition(content::PAGE_TRANSITION_LINK),
394 is_renderer_initiated(false), 366 is_renderer_initiated(false),
395 tabstrip_index(-1), 367 tabstrip_index(-1),
396 tabstrip_add_types(TabStripModel::ADD_ACTIVE), 368 tabstrip_add_types(TabStripModel::ADD_ACTIVE),
397 window_action(NO_ACTION), 369 window_action(NO_ACTION),
398 user_gesture(true), 370 user_gesture(true),
399 path_behavior(RESPECT), 371 path_behavior(RESPECT),
400 ref_behavior(IGNORE_REF), 372 ref_behavior(IGNORE_REF),
401 browser(a_browser), 373 browser(a_browser) {
402 profile(NULL) {
403 } 374 }
404 375
405 NavigateParams::~NavigateParams() { 376 NavigateParams::~NavigateParams() {
406 } 377 }
407 378
408 void Navigate(NavigateParams* params) { 379 void Navigate(NavigateParams* params) {
409 Browser* source_browser = params->browser; 380 Browser* source_browser = params->browser;
410 381
411 if (!AdjustNavigateParamsForURL(params)) 382 if (!AdjustNavigateParamsForURL(params))
412 return; 383 return;
413 384
414 // The browser window may want to adjust the disposition. 385 // The browser window may want to adjust the disposition.
415 if (params->disposition == NEW_POPUP && 386 if (params->disposition == NEW_POPUP && source_browser->window()) {
416 (source_browser && source_browser->window())) {
417 params->disposition = 387 params->disposition =
418 source_browser->window()->GetDispositionForPopupBounds( 388 source_browser->window()->GetDispositionForPopupBounds(
419 params->window_bounds); 389 params->window_bounds);
420 } 390 }
421 391
422 // Adjust disposition for the navigation happending in the sad page of the 392 // Adjust disposition for the navigation happending in the sad page of the
423 // panel window. 393 // panel window.
424 if (params->source_contents && 394 if (params->source_contents &&
425 params->source_contents->web_contents()->IsCrashed() && 395 params->source_contents->web_contents()->IsCrashed() &&
426 params->disposition == CURRENT_TAB && 396 params->disposition == CURRENT_TAB &&
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 } 555 }
586 556
587 if (singleton_index >= 0) { 557 if (singleton_index >= 0) {
588 WebContents* target = 558 WebContents* target =
589 chrome::GetWebContentsAt(params->browser, singleton_index); 559 chrome::GetWebContentsAt(params->browser, singleton_index);
590 560
591 if (target->IsCrashed()) { 561 if (target->IsCrashed()) {
592 target->GetController().Reload(true); 562 target->GetController().Reload(true);
593 } else if (params->path_behavior == NavigateParams::IGNORE_AND_NAVIGATE && 563 } else if (params->path_behavior == NavigateParams::IGNORE_AND_NAVIGATE &&
594 target->GetURL() != params->url) { 564 target->GetURL() != params->url) {
595 InitializeExtraHeaders(params, NULL, &extra_headers); 565 TabContents* target_tab = TabContents::FromWebContents(target);
566 InitializeExtraHeaders(params, target_tab->profile(), &extra_headers);
596 LoadURLInContents(target, params->url, params, extra_headers); 567 LoadURLInContents(target, params->url, params, extra_headers);
597 } 568 }
598 569
599 // If the singleton tab isn't already selected, select it. 570 // If the singleton tab isn't already selected, select it.
600 if (params->source_contents != params->target_contents) 571 if (params->source_contents != params->target_contents)
601 chrome::ActivateTabAt(params->browser, singleton_index, user_initiated); 572 chrome::ActivateTabAt(params->browser, singleton_index, user_initiated);
602 } 573 }
603 574
604 if (params->disposition != CURRENT_TAB) { 575 if (params->disposition != CURRENT_TAB) {
605 content::NotificationService::current()->Notify( 576 content::NotificationService::current()->Notify(
(...skipping 11 matching lines...) Expand all
617 return !(url.scheme() == chrome::kChromeUIScheme && 588 return !(url.scheme() == chrome::kChromeUIScheme &&
618 (url.host() == chrome::kChromeUISettingsHost || 589 (url.host() == chrome::kChromeUISettingsHost ||
619 url.host() == chrome::kChromeUISettingsFrameHost || 590 url.host() == chrome::kChromeUISettingsFrameHost ||
620 url.host() == chrome::kChromeUIExtensionsHost || 591 url.host() == chrome::kChromeUIExtensionsHost ||
621 url.host() == chrome::kChromeUIBookmarksHost || 592 url.host() == chrome::kChromeUIBookmarksHost ||
622 url.host() == chrome::kChromeUISyncPromoHost || 593 url.host() == chrome::kChromeUISyncPromoHost ||
623 url.host() == chrome::kChromeUIUberHost)); 594 url.host() == chrome::kChromeUIUberHost));
624 } 595 }
625 596
626 } // namespace chrome 597 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/ui/browser_navigator.h ('k') | chrome/browser/ui/browser_navigator_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698