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 { | |
|
Nico
2013/12/12 18:05:40
nit: const has implicit internal linkage, no need
noms (inactive)
2013/12/12 22:59:23
Done.
| |
| 18 | |
| 19 // Default window size. Taken from the views implementation in | |
| 20 // chrome/browser/ui/views/user_manager_view.cc. | |
| 21 // TODO(noms): Figure out if this size can be computed dynamically or adjusted | |
| 22 // for smaller screens. | |
| 23 const int kWindowWidth = 900; | |
| 24 const int kWindowHeight = 700; | |
| 25 | |
| 26 } // namespace | |
| 8 | 27 |
| 9 namespace chrome { | 28 namespace chrome { |
| 10 | 29 |
| 11 // Declared in browser_dialogs.h so others don't have to depend on this header. | 30 // 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) { | 31 void ShowUserManager(const base::FilePath& profile_path_to_focus) { |
| 14 NOTIMPLEMENTED(); | 32 UserManagerMac::Show(profile_path_to_focus); |
| 15 } | 33 } |
| 16 | 34 |
| 17 void HideUserManager() { | 35 void HideUserManager() { |
| 18 NOTIMPLEMENTED(); | 36 UserManagerMac::Hide(); |
| 19 } | 37 } |
| 20 | 38 |
| 21 } // namespace chrome | 39 } // namespace chrome |
| 40 | |
| 41 @interface UserManagerWindowController (Private) | |
| 42 - (void)windowWillClose:(NSNotification*)notification; | |
| 43 - (void)dealloc; | |
| 44 @end | |
| 45 | |
| 46 @implementation UserManagerWindowController | |
| 47 | |
| 48 - (id)initWithProfile:(Profile*)profile | |
| 49 withObserver:(UserManagerMac*)userManagerObserver { | |
| 50 | |
| 51 // Center the window on the primary screen. | |
| 52 CGFloat screenHeight = | |
| 53 [[[NSScreen screens] objectAtIndex:0] frame].size.height; | |
| 54 CGFloat screenWidth = | |
| 55 [[[NSScreen screens] objectAtIndex:0] frame].size.width; | |
| 56 | |
| 57 NSRect contentRect = NSMakeRect((screenWidth - kWindowWidth) / 2, | |
| 58 (screenHeight - kWindowHeight) / 2, | |
| 59 kWindowWidth, kWindowHeight); | |
| 60 | |
| 61 userManagerObserver_ = userManagerObserver; | |
| 62 NSWindow* window = [[NSWindow alloc] | |
| 63 initWithContentRect:contentRect | |
| 64 styleMask:NSTitledWindowMask | | |
| 65 NSClosableWindowMask | | |
| 66 NSResizableWindowMask | |
| 67 backing:NSBackingStoreBuffered | |
| 68 defer:NO]; | |
| 69 [window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)]; | |
| 70 | |
| 71 // Initialize the web view. | |
| 72 webContents_.reset(content::WebContents::Create( | |
|
Nico
2013/12/12 18:05:40
This is technically not correct (the best kind of
noms (inactive)
2013/12/12 22:59:23
Eeeek. Thank you. Fixed and won't do again.
On 201
| |
| 73 content::WebContents::CreateParams(profile))); | |
| 74 window.contentView = webContents_->GetView()->GetNativeView(); | |
| 75 DCHECK(window.contentView); | |
| 76 | |
| 77 // Initialize, but don't show the window until the web contents have loaded. | |
| 78 self = [super initWithWindow:window]; | |
| 79 | |
| 80 [[NSNotificationCenter defaultCenter] addObserver:self | |
| 81 selector:@selector(windowWillClose:) | |
| 82 name:NSWindowWillCloseNotification | |
| 83 object:self.window]; | |
| 84 return self; | |
| 85 } | |
| 86 | |
| 87 - (void)dealloc { | |
| 88 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 89 [super dealloc]; | |
| 90 } | |
| 91 | |
| 92 - (void)showURL:(const GURL&)url { | |
| 93 webContents_->GetController().LoadURL(url, content::Referrer(), | |
| 94 content::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 95 std::string()); | |
| 96 [self show]; | |
| 97 } | |
| 98 | |
| 99 - (void)show { | |
| 100 [[self window] makeKeyAndOrderFront:self]; | |
| 101 } | |
| 102 | |
| 103 - (void)close { | |
| 104 [[self window] close]; | |
| 105 } | |
| 106 | |
| 107 -(BOOL)isVisible { | |
| 108 return [[self window] isVisible]; | |
| 109 } | |
| 110 | |
| 111 - (void)windowWillClose:(NSNotification*)notification { | |
| 112 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 113 DCHECK(userManagerObserver_); | |
| 114 userManagerObserver_->WindowWasClosed(); | |
| 115 } | |
| 116 | |
| 117 @end | |
| 118 | |
| 119 // static | |
| 120 UserManagerMac* UserManagerMac::instance_ = NULL; | |
| 121 | |
| 122 UserManagerMac::UserManagerMac(Profile* profile) { | |
| 123 window_controller_.reset([[UserManagerWindowController alloc] | |
| 124 initWithProfile:profile withObserver:this]); | |
| 125 } | |
| 126 | |
| 127 UserManagerMac::~UserManagerMac() { | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 void UserManagerMac::Show(const base::FilePath& profile_path_to_focus) { | |
| 132 if (instance_) { | |
| 133 [instance_->window_controller_ show]; | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 // Create the guest profile, if necessary, and open the User Manager | |
| 138 // from the guest profile. | |
| 139 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 140 profile_manager->CreateProfileAsync( | |
| 141 ProfileManager::GetGuestProfilePath(), | |
| 142 base::Bind(&UserManagerMac::OnGuestProfileCreated, | |
| 143 profile_path_to_focus), | |
| 144 string16(), | |
| 145 string16(), | |
| 146 std::string()); | |
| 147 } | |
| 148 | |
| 149 // static | |
| 150 void UserManagerMac::Hide() { | |
| 151 if (instance_) | |
| 152 [instance_->window_controller_ close]; | |
| 153 } | |
| 154 | |
| 155 void UserManagerMac::WindowWasClosed() { | |
| 156 delete this; | |
| 157 instance_ = NULL; | |
|
Nico
2013/12/12 18:05:40
This too writes to a member variable, so this is r
noms (inactive)
2013/12/12 22:59:23
OH I see. I herp derped this one. Also, the TaskMa
Nico
2013/12/12 23:04:09
The Mac TaskManager was written by someone who had
Nico
2013/12/17 17:27:52
The person who wrote the mac task manager reached
| |
| 158 } | |
| 159 | |
| 160 void UserManagerMac::OnGuestProfileCreated( | |
| 161 const base::FilePath& profile_path_to_focus, | |
| 162 Profile* guest_profile, | |
| 163 Profile::CreateStatus status) { | |
| 164 if (status != Profile::CREATE_STATUS_INITIALIZED) | |
| 165 return; | |
| 166 | |
| 167 instance_ = new UserManagerMac(guest_profile); | |
| 168 | |
| 169 // Tell the webui which user pod should be focused. | |
| 170 std::string page = chrome::kChromeUIUserManagerURL; | |
| 171 | |
| 172 if (!profile_path_to_focus.empty()) { | |
| 173 ProfileInfoCache& cache = | |
| 174 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 175 size_t index = cache.GetIndexOfProfileWithPath(profile_path_to_focus); | |
| 176 if (index != std::string::npos) { | |
| 177 page += "#"; | |
| 178 page += base::IntToString(index); | |
| 179 } | |
| 180 } | |
| 181 [instance_->window_controller_ showURL:GURL(page)]; | |
| 182 } | |
| 183 | |
| OLD | NEW |