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