Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <Cocoa/Cocoa.h> | 5 #include "chrome/browser/ui/cocoa/user_manager_mac.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/profiles/profile_manager.h" | |
| 7 #include "chrome/browser/ui/browser_dialogs.h" | 10 #include "chrome/browser/ui/browser_dialogs.h" |
| 11 #include "chrome/common/url_constants.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 #include "content/public/browser/web_contents_view.h" | |
| 14 #include "grit/generated_resources.h" | |
| 15 #include "ui/base/l10n/l10n_util_mac.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Default window size. Taken from the views implementation in | |
| 20 // chrome/browser/ui/views/user_manager_view.cc. | |
| 21 const int kWindowWidth = 900; | |
|
groby-ooo-7-16
2013/12/10 00:41:06
I'd leave at least a TODO to adjust to smaller scr
noms (inactive)
2013/12/10 15:41:35
Done.
| |
| 22 const int kWindowHeight = 700; | |
| 23 | |
| 24 } // namespace | |
| 8 | 25 |
| 9 namespace chrome { | 26 namespace chrome { |
| 10 | 27 |
| 11 // Declared in browser_dialogs.h so others don't have to depend on this header. | 28 // Declared in browser_dialogs.h so others don't have to depend on this header. |
| 12 // TODO(noms): Add implementation when the User Manager dialog is implemented. | |
| 13 void ShowUserManager(const base::FilePath& profile_path_to_focus) { | 29 void ShowUserManager(const base::FilePath& profile_path_to_focus) { |
| 14 NOTIMPLEMENTED(); | 30 UserManagerMac::Show(profile_path_to_focus); |
| 15 } | 31 } |
| 16 | 32 |
| 17 void HideUserManager() { | 33 void HideUserManager() { |
| 18 NOTIMPLEMENTED(); | 34 UserManagerMac::Hide(); |
| 19 } | 35 } |
| 20 | 36 |
| 21 } // namespace chrome | 37 } // namespace chrome |
| 38 | |
| 39 @interface UserManagerWindowController (Private) | |
| 40 - (void)windowWillClose:(NSNotification*)notification; | |
| 41 - (void)dealloc; | |
| 42 @end | |
| 43 | |
| 44 @implementation UserManagerWindowController | |
| 45 | |
| 46 - (id)initWithProfile:(Profile*)profile | |
| 47 withObserver:(UserManagerMac*)userManagerObserver { | |
| 48 | |
| 49 // Center the window on the primary screen. | |
|
groby-ooo-7-16
2013/12/10 00:41:06
What do you mean by "primary" screen? The one with
noms (inactive)
2013/12/10 15:41:35
The reference says primary means the one that cont
| |
| 50 CGFloat screenHeight = | |
| 51 [[[NSScreen screens] objectAtIndex:0] frame].size.height; | |
| 52 CGFloat screenWidth = | |
| 53 [[[NSScreen screens] objectAtIndex:0] frame].size.width; | |
| 54 | |
| 55 NSRect contentRect = NSMakeRect((screenWidth - kWindowWidth) / 2, | |
| 56 (screenHeight - kWindowHeight) / 2, | |
| 57 kWindowWidth, kWindowHeight); | |
| 58 | |
| 59 userManagerObserver_ = userManagerObserver; | |
| 60 NSWindow* window = [[NSWindow alloc] | |
| 61 initWithContentRect:contentRect | |
| 62 styleMask:NSTitledWindowMask | | |
| 63 NSClosableWindowMask | | |
| 64 NSResizableWindowMask | |
| 65 backing:NSBackingStoreBuffered | |
| 66 defer:NO]; | |
| 67 [window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)]; | |
| 68 | |
| 69 // Initialize the web view. | |
| 70 webContents_.reset(content::WebContents::Create( | |
| 71 content::WebContents::CreateParams(profile))); | |
| 72 window.contentView = webContents_->GetView()->GetNativeView(); | |
| 73 DCHECK(window.contentView); | |
| 74 | |
| 75 // Initialize, but don't show the window until the web contents have loaded. | |
| 76 self = [super initWithWindow:window]; | |
| 77 | |
| 78 [[NSNotificationCenter defaultCenter] addObserver:self | |
| 79 selector:@selector(windowWillClose:) | |
| 80 name:NSWindowWillCloseNotification | |
| 81 object:self.window]; | |
| 82 return self; | |
| 83 } | |
| 84 | |
| 85 - (void)dealloc { | |
| 86 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 87 [super dealloc]; | |
| 88 } | |
| 89 | |
| 90 - (void)showURL:(GURL)url { | |
| 91 webContents_->GetController().LoadURL(url, content::Referrer(), | |
| 92 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 93 std::string()); | |
| 94 [self show]; | |
| 95 } | |
| 96 | |
| 97 - (void)show { | |
| 98 [[self window] makeKeyAndOrderFront:self]; | |
| 99 } | |
| 100 | |
| 101 - (void)close { | |
| 102 [[self window] close]; | |
| 103 } | |
| 104 | |
| 105 -(BOOL)isVisible { | |
| 106 return [[self window] isVisible]; | |
| 107 } | |
| 108 | |
| 109 - (void)windowWillClose:(NSNotification*)notification { | |
| 110 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 111 | |
| 112 // Reset the web contents so that the guest profile doesn't have any | |
| 113 // renderer hosts, so that the profile can be destroyed cleanly in case the | |
| 114 // process is force-killed. | |
|
groby-ooo-7-16
2013/12/10 00:41:06
I still don't understand the reference to force-ki
noms (inactive)
2013/12/10 15:41:35
I meant when you Ctrl-C the process in a terminal.
| |
| 115 webContents_.reset(); | |
|
groby-ooo-7-16
2013/12/10 00:41:06
Do you still need the reset? Doesn't the observer
noms (inactive)
2013/12/10 15:41:35
Done.
| |
| 116 | |
| 117 if (userManagerObserver_) { | |
|
groby-ooo-7-16
2013/12/10 00:41:06
Just DCHECK the observer - should never be NULL.
noms (inactive)
2013/12/10 15:41:35
Done.
| |
| 118 userManagerObserver_->WindowWasClosed(); | |
| 119 userManagerObserver_ = nil; | |
|
groby-ooo-7-16
2013/12/10 00:41:06
There's a tiny potential for conflict here - Windo
noms (inactive)
2013/12/10 15:41:35
Done.
| |
| 120 } | |
| 121 } | |
| 122 | |
| 123 @end | |
| 124 | |
| 125 // static | |
| 126 UserManagerMac* UserManagerMac::instance_ = NULL; | |
| 127 | |
| 128 UserManagerMac::UserManagerMac(Profile* profile) { | |
| 129 window_controller_.reset([[UserManagerWindowController alloc] | |
| 130 initWithProfile:profile withObserver:this]); | |
| 131 } | |
| 132 | |
| 133 UserManagerMac::~UserManagerMac() { | |
| 134 } | |
| 135 | |
| 136 // static | |
| 137 void UserManagerMac::Show(const base::FilePath& profile_path_to_focus) { | |
| 138 if (instance_) { | |
| 139 [instance_->window_controller_ show]; | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 // Create the guest profile, if necessary, and open the User Manager | |
| 144 // from the guest profile. | |
| 145 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 146 profile_manager->CreateProfileAsync( | |
| 147 ProfileManager::GetGuestProfilePath(), | |
| 148 base::Bind(&UserManagerMac::OnGuestProfileCreated, | |
| 149 profile_path_to_focus), | |
| 150 string16(), | |
| 151 string16(), | |
| 152 std::string()); | |
| 153 } | |
| 154 | |
| 155 // static | |
| 156 void UserManagerMac::Hide() { | |
| 157 if (instance_) | |
| 158 [instance_->window_controller_ close]; | |
| 159 } | |
| 160 | |
| 161 void UserManagerMac::WindowWasClosed() { | |
| 162 delete this; | |
| 163 instance_ = NULL; | |
|
groby-ooo-7-16
2013/12/10 00:41:06
instance_ is technically already freed memory - do
noms (inactive)
2013/12/10 15:41:35
Hmm, instance is static. Freeing the 'this' object
| |
| 164 } | |
| 165 | |
| 166 void UserManagerMac::OnGuestProfileCreated( | |
| 167 const base::FilePath& profile_path_to_focus, | |
| 168 Profile* guest_profile, | |
| 169 Profile::CreateStatus status) { | |
| 170 if (status != Profile::CREATE_STATUS_INITIALIZED) | |
| 171 return; | |
| 172 | |
| 173 instance_ = new UserManagerMac(guest_profile); | |
| 174 | |
| 175 // Tell the webui which user pod should be focused. | |
| 176 std::string page = chrome::kChromeUIUserManagerURL; | |
| 177 | |
| 178 if (!profile_path_to_focus.empty()) { | |
| 179 ProfileInfoCache& cache = | |
| 180 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 181 size_t index = cache.GetIndexOfProfileWithPath(profile_path_to_focus); | |
| 182 if (index != std::string::npos) { | |
| 183 page += "#"; | |
| 184 page += base::IntToString(index); | |
| 185 } | |
| 186 } | |
| 187 [instance_->window_controller_ showURL:GURL(page)]; | |
| 188 } | |
| 189 | |
| OLD | NEW |