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

Side by Side Diff: chrome/browser/app_controller_mac.mm

Issue 678423003: mac: Opening a URL should replace the NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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
« no previous file with comments | « no previous file | chrome/browser/app_controller_mac_browsertest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #import "chrome/browser/app_controller_mac.h" 5 #import "chrome/browser/app_controller_mac.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 } 206 }
207 207
208 } // anonymous namespace 208 } // anonymous namespace
209 209
210 @interface AppController (Private) 210 @interface AppController (Private)
211 - (void)initMenuState; 211 - (void)initMenuState;
212 - (void)initProfileMenu; 212 - (void)initProfileMenu;
213 - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item; 213 - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item;
214 - (void)updateDisplayMessageCenterPrefMenuItem:(NSMenuItem*)item; 214 - (void)updateDisplayMessageCenterPrefMenuItem:(NSMenuItem*)item;
215 - (void)registerServicesMenuTypesTo:(NSApplication*)app; 215 - (void)registerServicesMenuTypesTo:(NSApplication*)app;
216 - (void)openUrls:(const std::vector<GURL>&)urls;
217 - (void)getUrl:(NSAppleEventDescriptor*)event 216 - (void)getUrl:(NSAppleEventDescriptor*)event
218 withReply:(NSAppleEventDescriptor*)reply; 217 withReply:(NSAppleEventDescriptor*)reply;
219 - (void)windowLayeringDidChange:(NSNotification*)inNotification; 218 - (void)windowLayeringDidChange:(NSNotification*)inNotification;
220 - (void)activeSpaceDidChange:(NSNotification*)inNotification; 219 - (void)activeSpaceDidChange:(NSNotification*)inNotification;
221 - (void)checkForAnyKeyWindows; 220 - (void)checkForAnyKeyWindows;
222 - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; 221 - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
223 - (BOOL)shouldQuitWithInProgressDownloads; 222 - (BOOL)shouldQuitWithInProgressDownloads;
224 - (void)executeApplication:(id)sender; 223 - (void)executeApplication:(id)sender;
225 - (void)profileWasRemoved:(const base::FilePath&)profilePath; 224 - (void)profileWasRemoved:(const base::FilePath&)profilePath;
225
226 // Opens a tab for each GURL in |urls|.
227 - (void)openUrls:(const std::vector<GURL>&)urls;
228
229 // This class cannot open urls until startup has finished. The urls that cannot
230 // be opened are cached in |startupUrls_|. This method must be called exactly
231 // once after startup has completed. It opens the urls in |startupUrls_|, and
232 // clears |startupUrls_|.
233 - (void)openStartupUrls;
234
235 // Opens a tab for each GURL in |urls|. If there exactly 1 tab open before this
Avi (use Gerrit) 2014/10/29 14:38:59 "If there is exactly one tab"...
erikchen 2014/10/29 17:18:02 Done.
236 // method is called, and that tab is the NTP, then this method closes the NTP
237 // after all the |urls| have been opened.
238 - (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls;
226 @end 239 @end
227 240
228 class AppControllerProfileObserver : public ProfileInfoCacheObserver { 241 class AppControllerProfileObserver : public ProfileInfoCacheObserver {
229 public: 242 public:
230 AppControllerProfileObserver( 243 AppControllerProfileObserver(
231 ProfileManager* profile_manager, AppController* app_controller) 244 ProfileManager* profile_manager, AppController* app_controller)
232 : profile_manager_(profile_manager), 245 : profile_manager_(profile_manager),
233 app_controller_(app_controller) { 246 app_controller_(app_controller) {
234 DCHECK(profile_manager_); 247 DCHECK(profile_manager_);
235 DCHECK(app_controller_); 248 DCHECK(app_controller_);
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 if (!plist) { 666 if (!plist) {
654 const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0; 667 const float fiveHoursInSeconds = 5.0 * 60.0 * 60.0;
655 NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds]; 668 NSNumber* value = [NSNumber numberWithFloat:fiveHoursInSeconds];
656 CFPreferencesSetAppValue(checkInterval, value, app); 669 CFPreferencesSetAppValue(checkInterval, value, app);
657 CFPreferencesAppSynchronize(app); 670 CFPreferencesAppSynchronize(app);
658 } 671 }
659 #endif 672 #endif
660 } 673 }
661 674
662 - (void)openStartupUrls { 675 - (void)openStartupUrls {
676 DCHECK(startupComplete_);
677 [self openUrlsReplacingNTP:startupUrls_];
678 startupUrls_.clear();
679 }
680
681 - (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls {
682 if (urls.empty())
683 return;
684
663 // On Mac, the URLs are passed in via Cocoa, not command line. The Chrome 685 // On Mac, the URLs are passed in via Cocoa, not command line. The Chrome
664 // NSApplication is created in MainMessageLoop, and then the shortcut urls 686 // NSApplication is created in MainMessageLoop, and then the shortcut urls
665 // are passed in via Apple events. At this point, the first browser is 687 // are passed in via Apple events. At this point, the first browser is
666 // already loaded in PreMainMessageLoop. If we initialize NSApplication 688 // already loaded in PreMainMessageLoop. If we initialize NSApplication
667 // before PreMainMessageLoop to capture shortcut URL events, it may cause 689 // before PreMainMessageLoop to capture shortcut URL events, it may cause
668 // more problems because it relies on things created in PreMainMessageLoop 690 // more problems because it relies on things created in PreMainMessageLoop
669 // and may break existing message loop design. 691 // and may break existing message loop design.
670 if (startupUrls_.empty()) 692
693 // If the browser hasn't started yet, just queue up the URLs.
694 if (!startupComplete_) {
695 startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end());
671 return; 696 return;
697 }
672 698
673 // If there's only 1 tab and the tab is NTP, close this NTP tab and open all 699 // If there's only 1 tab and the tab is NTP, close this NTP tab and open all
674 // startup urls in new tabs, because the omnibox will stay focused if we 700 // startup urls in new tabs, because the omnibox will stay focused if we
675 // load url in NTP tab. 701 // load url in NTP tab.
676 Browser* browser = chrome::GetLastActiveBrowser(); 702 Browser* browser = chrome::GetLastActiveBrowser();
677 int startupIndex = TabStripModel::kNoTab; 703 int startupIndex = TabStripModel::kNoTab;
678 content::WebContents* startupContent = NULL; 704 content::WebContents* startupContent = NULL;
679 705
680 if (browser && browser->tab_strip_model()->count() == 1) { 706 if (browser && browser->tab_strip_model()->count() == 1) {
681 startupIndex = browser->tab_strip_model()->active_index(); 707 startupIndex = browser->tab_strip_model()->active_index();
682 startupContent = browser->tab_strip_model()->GetActiveWebContents(); 708 startupContent = browser->tab_strip_model()->GetActiveWebContents();
683 } 709 }
684 710
685 if (startupUrls_.size()) { 711 [self openUrls:urls];
686 [self openUrls:startupUrls_];
687 startupUrls_.clear();
688 }
689 712
690 if (startupIndex != TabStripModel::kNoTab && 713 if (startupIndex != TabStripModel::kNoTab &&
691 startupContent->GetVisibleURL() == GURL(chrome::kChromeUINewTabURL)) { 714 startupContent->GetVisibleURL() == GURL(chrome::kChromeUINewTabURL)) {
692 browser->tab_strip_model()->CloseWebContentsAt(startupIndex, 715 browser->tab_strip_model()->CloseWebContentsAt(startupIndex,
693 TabStripModel::CLOSE_NONE); 716 TabStripModel::CLOSE_NONE);
694 } 717 }
695 } 718 }
696 719
697 // This is called after profiles have been loaded and preferences registered. 720 // This is called after profiles have been loaded and preferences registered.
698 // It is safe to access the default profile here. 721 // It is safe to access the default profile here.
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
1308 if (profile->IsGuestSession()) 1331 if (profile->IsGuestSession())
1309 return profile->GetOffTheRecordProfile(); 1332 return profile->GetOffTheRecordProfile();
1310 1333
1311 return profile; 1334 return profile;
1312 } 1335 }
1313 1336
1314 // Various methods to open URLs that we get in a native fashion. We use 1337 // Various methods to open URLs that we get in a native fashion. We use
1315 // StartupBrowserCreator here because on the other platforms, URLs to open come 1338 // StartupBrowserCreator here because on the other platforms, URLs to open come
1316 // through the ProcessSingleton, and it calls StartupBrowserCreator. It's best 1339 // through the ProcessSingleton, and it calls StartupBrowserCreator. It's best
1317 // to bottleneck the openings through that for uniform handling. 1340 // to bottleneck the openings through that for uniform handling.
1318
1319 - (void)openUrls:(const std::vector<GURL>&)urls { 1341 - (void)openUrls:(const std::vector<GURL>&)urls {
1320 // If the browser hasn't started yet, just queue up the URLs.
1321 if (!startupComplete_) { 1342 if (!startupComplete_) {
1322 startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end()); 1343 startupUrls_.insert(startupUrls_.end(), urls.begin(), urls.end());
1323 return; 1344 return;
1324 } 1345 }
1325 1346
1326 Browser* browser = chrome::GetLastActiveBrowser(); 1347 Browser* browser = chrome::GetLastActiveBrowser();
1327 // if no browser window exists then create one with no tabs to be filled in 1348 // if no browser window exists then create one with no tabs to be filled in
1328 if (!browser) { 1349 if (!browser) {
1329 browser = new Browser(Browser::CreateParams( 1350 browser = new Browser(Browser::CreateParams(
1330 [self lastProfile], chrome::HOST_DESKTOP_TYPE_NATIVE)); 1351 [self lastProfile], chrome::HOST_DESKTOP_TYPE_NATIVE));
1331 browser->window()->Show(); 1352 browser->window()->Show();
1332 } 1353 }
1333 1354
1334 CommandLine dummy(CommandLine::NO_PROGRAM); 1355 CommandLine dummy(CommandLine::NO_PROGRAM);
1335 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ? 1356 chrome::startup::IsFirstRun first_run = first_run::IsChromeFirstRun() ?
1336 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN; 1357 chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
1337 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run); 1358 StartupBrowserCreatorImpl launch(base::FilePath(), dummy, first_run);
1338 launch.OpenURLsInBrowser(browser, false, urls, browser->host_desktop_type()); 1359 launch.OpenURLsInBrowser(browser, false, urls, browser->host_desktop_type());
1339 } 1360 }
1340 1361
1341 - (void)getUrl:(NSAppleEventDescriptor*)event 1362 - (void)getUrl:(NSAppleEventDescriptor*)event
1342 withReply:(NSAppleEventDescriptor*)reply { 1363 withReply:(NSAppleEventDescriptor*)reply {
1343 NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject] 1364 NSString* urlStr = [[event paramDescriptorForKeyword:keyDirectObject]
1344 stringValue]; 1365 stringValue];
1345 1366
1346 GURL gurl(base::SysNSStringToUTF8(urlStr)); 1367 GURL gurl(base::SysNSStringToUTF8(urlStr));
1347 std::vector<GURL> gurlVector; 1368 std::vector<GURL> gurlVector;
1348 gurlVector.push_back(gurl); 1369 gurlVector.push_back(gurl);
1349 1370
1350 [self openUrls:gurlVector]; 1371 [self openUrlsReplacingNTP:gurlVector];
1351 } 1372 }
1352 1373
1353 - (void)application:(NSApplication*)sender 1374 - (void)application:(NSApplication*)sender
1354 openFiles:(NSArray*)filenames { 1375 openFiles:(NSArray*)filenames {
1355 std::vector<GURL> gurlVector; 1376 std::vector<GURL> gurlVector;
1356 for (NSString* file in filenames) { 1377 for (NSString* file in filenames) {
1357 GURL gurl = 1378 GURL gurl =
1358 net::FilePathToFileURL(base::FilePath([file fileSystemRepresentation])); 1379 net::FilePathToFileURL(base::FilePath([file fileSystemRepresentation]));
1359 gurlVector.push_back(gurl); 1380 gurlVector.push_back(gurl);
1360 } 1381 }
1361 if (!gurlVector.empty()) 1382 if (!gurlVector.empty())
1362 [self openUrls:gurlVector]; 1383 [self openUrlsReplacingNTP:gurlVector];
1363 else 1384 else
1364 NOTREACHED() << "Nothing to open!"; 1385 NOTREACHED() << "Nothing to open!";
1365 1386
1366 [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess]; 1387 [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
1367 } 1388 }
1368 1389
1369 // Show the preferences window, or bring it to the front if it's already 1390 // Show the preferences window, or bring it to the front if it's already
1370 // visible. 1391 // visible.
1371 - (IBAction)showPreferences:(id)sender { 1392 - (IBAction)showPreferences:(id)sender {
1372 if (Browser* browser = ActivateBrowser([self lastProfile])) { 1393 if (Browser* browser = ActivateBrowser([self lastProfile])) {
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
1552 } 1573 }
1553 1574
1554 NSURL* url = userActivity.webPageURL; 1575 NSURL* url = userActivity.webPageURL;
1555 if (!url) 1576 if (!url)
1556 return NO; 1577 return NO;
1557 1578
1558 GURL gurl(base::SysNSStringToUTF8([url absoluteString])); 1579 GURL gurl(base::SysNSStringToUTF8([url absoluteString]));
1559 std::vector<GURL> gurlVector; 1580 std::vector<GURL> gurlVector;
1560 gurlVector.push_back(gurl); 1581 gurlVector.push_back(gurl);
1561 1582
1562 [self openUrls:gurlVector]; 1583 [self openUrlsReplacingNTP:gurlVector];
1563 return YES; 1584 return YES;
1564 } 1585 }
1565 1586
1566 - (void)application:(NSApplication*)application 1587 - (void)application:(NSApplication*)application
1567 didFailToContinueUserActivityWithType:(NSString*)userActivityType 1588 didFailToContinueUserActivityWithType:(NSString*)userActivityType
1568 error:(NSError*)error { 1589 error:(NSError*)error {
1569 } 1590 }
1570 1591
1571 @end // @implementation AppController 1592 @end // @implementation AppController
1572 1593
1573 //--------------------------------------------------------------------------- 1594 //---------------------------------------------------------------------------
1574 1595
1575 namespace app_controller_mac { 1596 namespace app_controller_mac {
1576 1597
1577 bool IsOpeningNewWindow() { 1598 bool IsOpeningNewWindow() {
1578 return g_is_opening_new_window; 1599 return g_is_opening_new_window;
1579 } 1600 }
1580 1601
1581 } // namespace app_controller_mac 1602 } // namespace app_controller_mac
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/app_controller_mac_browsertest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698