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

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

Issue 14923004: [Mac] AppController needs to update its "last profile" pointer when the active profile is deleted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase & fixed comments Created 7 years, 6 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
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 10 matching lines...) Expand all
21 #include "chrome/browser/browser_process.h" 21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/browser_shutdown.h" 22 #include "chrome/browser/browser_shutdown.h"
23 #include "chrome/browser/command_updater.h" 23 #include "chrome/browser/command_updater.h"
24 #include "chrome/browser/download/download_service.h" 24 #include "chrome/browser/download/download_service.h"
25 #include "chrome/browser/download/download_service_factory.h" 25 #include "chrome/browser/download/download_service_factory.h"
26 #include "chrome/browser/extensions/extension_service.h" 26 #include "chrome/browser/extensions/extension_service.h"
27 #include "chrome/browser/extensions/extension_system.h" 27 #include "chrome/browser/extensions/extension_system.h"
28 #include "chrome/browser/first_run/first_run.h" 28 #include "chrome/browser/first_run/first_run.h"
29 #include "chrome/browser/lifetime/application_lifetime.h" 29 #include "chrome/browser/lifetime/application_lifetime.h"
30 #include "chrome/browser/printing/print_dialog_cloud.h" 30 #include "chrome/browser/printing/print_dialog_cloud.h"
31 #include "chrome/browser/profiles/profile_info_cache_observer.h"
31 #include "chrome/browser/profiles/profile_manager.h" 32 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/service/service_process_control.h" 33 #include "chrome/browser/service/service_process_control.h"
33 #include "chrome/browser/sessions/session_restore.h" 34 #include "chrome/browser/sessions/session_restore.h"
34 #include "chrome/browser/sessions/session_service.h" 35 #include "chrome/browser/sessions/session_service.h"
35 #include "chrome/browser/sessions/session_service_factory.h" 36 #include "chrome/browser/sessions/session_service_factory.h"
36 #include "chrome/browser/sessions/tab_restore_service.h" 37 #include "chrome/browser/sessions/tab_restore_service.h"
37 #include "chrome/browser/sessions/tab_restore_service_factory.h" 38 #include "chrome/browser/sessions/tab_restore_service_factory.h"
38 #include "chrome/browser/signin/signin_manager.h" 39 #include "chrome/browser/signin/signin_manager.h"
39 #include "chrome/browser/signin/signin_manager_factory.h" 40 #include "chrome/browser/signin/signin_manager_factory.h"
40 #include "chrome/browser/sync/profile_sync_service.h" 41 #include "chrome/browser/sync/profile_sync_service.h"
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 - (void)openUrls:(const std::vector<GURL>&)urls; 184 - (void)openUrls:(const std::vector<GURL>&)urls;
184 - (void)getUrl:(NSAppleEventDescriptor*)event 185 - (void)getUrl:(NSAppleEventDescriptor*)event
185 withReply:(NSAppleEventDescriptor*)reply; 186 withReply:(NSAppleEventDescriptor*)reply;
186 - (void)submitCloudPrintJob:(NSAppleEventDescriptor*)event; 187 - (void)submitCloudPrintJob:(NSAppleEventDescriptor*)event;
187 - (void)windowLayeringDidChange:(NSNotification*)inNotification; 188 - (void)windowLayeringDidChange:(NSNotification*)inNotification;
188 - (void)windowChangedToProfile:(Profile*)profile; 189 - (void)windowChangedToProfile:(Profile*)profile;
189 - (void)checkForAnyKeyWindows; 190 - (void)checkForAnyKeyWindows;
190 - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; 191 - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
191 - (BOOL)shouldQuitWithInProgressDownloads; 192 - (BOOL)shouldQuitWithInProgressDownloads;
192 - (void)executeApplication:(id)sender; 193 - (void)executeApplication:(id)sender;
194 - (void)profileWasRemoved:(const string16&)profileName;
193 @end 195 @end
194 196
197 class AppControllerProfileObserver : public ProfileInfoCacheObserver {
198 public:
199 AppControllerProfileObserver(
200 ProfileManager* profile_manager, AppController* app_controller)
201 : profile_manager_(profile_manager),
202 app_controller_(app_controller) {
203 DCHECK(profile_manager_);
204 DCHECK(app_controller_);
205 profile_manager_->GetProfileInfoCache().AddObserver(this);
206 }
207
208 virtual ~AppControllerProfileObserver() {
209 DCHECK(profile_manager_);
210 profile_manager_->GetProfileInfoCache().RemoveObserver(this);
211 }
212
213 private:
214 // ProfileInfoCacheObserver implementation:
215
216 virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE {
217 }
218
219 virtual void OnProfileWasRemoved(const base::FilePath& profile_path,
220 const string16& profile_name) OVERRIDE {
221 // When a profile is deleted we need to notify the AppController,
222 // so that it can correctly update its pointer to the last used profile.
223 [app_controller_ profileWasRemoved:profile_name];
sail 2013/06/12 21:16:49 It would be more direct to use profile_path to ref
noms (inactive) 2013/06/13 20:56:25 Done.
224 }
225
226 virtual void OnProfileWillBeRemoved(
227 const base::FilePath& profile_path) OVERRIDE {
228 }
229
230 virtual void OnProfileNameChanged(const base::FilePath& profile_path,
231 const string16& old_profile_name) OVERRIDE {
232 }
233
234 virtual void OnProfileAvatarChanged(
235 const base::FilePath& profile_path) OVERRIDE {
236 }
237
238 ProfileManager* profile_manager_;
239
240 AppController* app_controller_; // Weak; owns us.
241
242 DISALLOW_COPY_AND_ASSIGN(AppControllerProfileObserver);
243 };
244
195 @implementation AppController 245 @implementation AppController
196 246
197 @synthesize startupComplete = startupComplete_; 247 @synthesize startupComplete = startupComplete_;
198 248
199 // This method is called very early in application startup (ie, before 249 // This method is called very early in application startup (ie, before
200 // the profile is loaded or any preferences have been registered). Defer any 250 // the profile is loaded or any preferences have been registered). Defer any
201 // user-data initialization until -applicationDidFinishLaunching:. 251 // user-data initialization until -applicationDidFinishLaunching:.
202 - (void)awakeFromNib { 252 - (void)awakeFromNib {
203 // We need to register the handlers early to catch events fired on launch. 253 // We need to register the handlers early to catch events fired on launch.
204 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; 254 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 // Build up the encoding menu, the order of the items differs based on the 628 // Build up the encoding menu, the order of the items differs based on the
579 // current locale (see http://crbug.com/7647 for details). 629 // current locale (see http://crbug.com/7647 for details).
580 // We need a valid g_browser_process to get the profile which is why we can't 630 // We need a valid g_browser_process to get the profile which is why we can't
581 // call this from awakeFromNib. 631 // call this from awakeFromNib.
582 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu]; 632 NSMenu* viewMenu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu];
583 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU]; 633 NSMenuItem* encodingMenuItem = [viewMenu itemWithTag:IDC_ENCODING_MENU];
584 NSMenu* encodingMenu = [encodingMenuItem submenu]; 634 NSMenu* encodingMenu = [encodingMenuItem submenu];
585 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile], 635 EncodingMenuControllerDelegate::BuildEncodingMenu([self lastProfile],
586 encodingMenu); 636 encodingMenu);
587 637
638 // Instantiate the ProfileInfoCache observer so that we can get
639 // notified when a profile is deleted.
640 profileInfoCacheObserver_.reset(new AppControllerProfileObserver(
641 g_browser_process->profile_manager(), self));
642
588 // Since Chrome is localized to more languages than the OS, tell Cocoa which 643 // Since Chrome is localized to more languages than the OS, tell Cocoa which
589 // menu is the Help so it can add the search item to it. 644 // menu is the Help so it can add the search item to it.
590 [NSApp setHelpMenu:helpMenu_]; 645 [NSApp setHelpMenu:helpMenu_];
591 646
592 // Record the path to the (browser) app bundle; this is used by the app mode 647 // Record the path to the (browser) app bundle; this is used by the app mode
593 // shim. 648 // shim.
594 RecordLastRunAppBundlePath(); 649 RecordLastRunAppBundlePath();
595 650
596 // Makes "Services" menu items available. 651 // Makes "Services" menu items available.
597 [self registerServicesMenuTypesTo:[notify object]]; 652 [self registerServicesMenuTypesTo:[notify object]];
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 762
708 // Called to determine if we should enable the "restore tab" menu item. 763 // Called to determine if we should enable the "restore tab" menu item.
709 // Checks with the TabRestoreService to see if there's anything there to 764 // Checks with the TabRestoreService to see if there's anything there to
710 // restore and returns YES if so. 765 // restore and returns YES if so.
711 - (BOOL)canRestoreTab { 766 - (BOOL)canRestoreTab {
712 TabRestoreService* service = 767 TabRestoreService* service =
713 TabRestoreServiceFactory::GetForProfile([self lastProfile]); 768 TabRestoreServiceFactory::GetForProfile([self lastProfile]);
714 return service && !service->entries().empty(); 769 return service && !service->entries().empty();
715 } 770 }
716 771
772 // Called from the AppControllerProfileObserver every time a profile is deleted.
773 - (void)profileWasRemoved:(const string16&)profileName {
774 Profile* lastProfile = [self lastProfile];
775 string16 lastProfileName = UTF8ToUTF16(lastProfile->GetPrefs()->GetString(
776 prefs::kProfileName));
777
778 // If the lastProfile has been deleted, the profile manager has
779 // already loaded a new one, so the pointer needs to be updated;
780 // otherwise we will try to start up a browser window with a pointer
781 // to the old profile.
782 if (profileName == lastProfileName) {
Alexei Svitkine (slow) 2013/06/12 21:02:31 Nit: No {}'s
noms (inactive) 2013/06/13 20:56:25 Done.
783 lastProfile_ = g_browser_process->profile_manager()->GetLastUsedProfile();
784 }
785 }
786
717 // Returns true if there is a modal window (either window- or application- 787 // Returns true if there is a modal window (either window- or application-
718 // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth 788 // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
719 // sheets) will not count as blocking the browser. But things like open/save 789 // sheets) will not count as blocking the browser. But things like open/save
720 // dialogs that are window modal will block the browser. 790 // dialogs that are window modal will block the browser.
721 - (BOOL)keyWindowIsModal { 791 - (BOOL)keyWindowIsModal {
722 if ([NSApp modalWindow]) 792 if ([NSApp modalWindow])
723 return YES; 793 return YES;
724 794
725 Browser* browser = chrome::GetLastActiveBrowser(); 795 Browser* browser = chrome::GetLastActiveBrowser();
726 return browser && 796 return browser &&
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
1090 // handles requests from services. 1160 // handles requests from services.
1091 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil]; 1161 NSArray* types = [NSArray arrayWithObjects:NSStringPboardType, nil];
1092 [app registerServicesMenuSendTypes:types returnTypes:types]; 1162 [app registerServicesMenuSendTypes:types returnTypes:types];
1093 } 1163 }
1094 1164
1095 - (Profile*)lastProfile { 1165 - (Profile*)lastProfile {
1096 // Return the profile of the last-used BrowserWindowController, if available. 1166 // Return the profile of the last-used BrowserWindowController, if available.
1097 if (lastProfile_) 1167 if (lastProfile_)
1098 return lastProfile_; 1168 return lastProfile_;
1099 1169
1170 if (!g_browser_process->profile_manager())
1171 return NULL;
1172
1100 // On first launch, no profile will be stored, so use last from Local State. 1173 // On first launch, no profile will be stored, so use last from Local State.
1101 if (g_browser_process->profile_manager()) 1174 Profile* lastUsedProfile =
Alexei Svitkine (slow) 2013/06/12 21:02:31 Nit: Just return it directly.
noms (inactive) 2013/06/13 20:56:25 Done.
1102 return g_browser_process->profile_manager()->GetLastUsedProfile(); 1175 g_browser_process->profile_manager()->GetLastUsedProfile();
1103 1176
1104 return NULL; 1177 return lastUsedProfile;
1105 } 1178 }
1106 1179
1107 // Various methods to open URLs that we get in a native fashion. We use 1180 // Various methods to open URLs that we get in a native fashion. We use
1108 // StartupBrowserCreator here because on the other platforms, URLs to open come 1181 // StartupBrowserCreator here because on the other platforms, URLs to open come
1109 // through the ProcessSingleton, and it calls StartupBrowserCreator. It's best 1182 // through the ProcessSingleton, and it calls StartupBrowserCreator. It's best
1110 // to bottleneck the openings through that for uniform handling. 1183 // to bottleneck the openings through that for uniform handling.
1111 1184
1112 - (void)openUrls:(const std::vector<GURL>&)urls { 1185 - (void)openUrls:(const std::vector<GURL>&)urls {
1113 // If the browser hasn't started yet, just queue up the URLs. 1186 // If the browser hasn't started yet, just queue up the URLs.
1114 if (!startupComplete_) { 1187 if (!startupComplete_) {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 1395
1323 //--------------------------------------------------------------------------- 1396 //---------------------------------------------------------------------------
1324 1397
1325 namespace app_controller_mac { 1398 namespace app_controller_mac {
1326 1399
1327 bool IsOpeningNewWindow() { 1400 bool IsOpeningNewWindow() {
1328 return g_is_opening_new_window; 1401 return g_is_opening_new_window;
1329 } 1402 }
1330 1403
1331 } // namespace app_controller_mac 1404 } // namespace app_controller_mac
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698