Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / browser_window_utils.mm
blob60dd4d1f8827371fc63ecfb3f2469b5eef29e278
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
7 #include <Carbon/Carbon.h>
9 #include "base/logging.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/global_keyboard_shortcuts_mac.h"
12 #include "chrome/browser/ui/browser.h"
13 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
14 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
15 #include "content/public/browser/native_web_keyboard_event.h"
17 using content::NativeWebKeyboardEvent;
19 @implementation BrowserWindowUtils
20 + (BOOL)shouldHandleKeyboardEvent:(const NativeWebKeyboardEvent&)event {
21   if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char)
22     return NO;
23   DCHECK(event.os_event != NULL);
24   return YES;
27 + (int)getCommandId:(const NativeWebKeyboardEvent&)event {
28   return CommandForKeyEvent(event.os_event);
31 + (BOOL)handleKeyboardEvent:(NSEvent*)event
32                    inWindow:(NSWindow*)window {
33   ChromeEventProcessingWindow* event_window =
34       static_cast<ChromeEventProcessingWindow*>(window);
35   DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]);
37   // Do not fire shortcuts on key up.
38   if ([event type] == NSKeyDown) {
39     // Send the event to the menu before sending it to the browser/window
40     // shortcut handling, so that if a user configures cmd-left to mean
41     // "previous tab", it takes precedence over the built-in "history back"
42     // binding. Other than that, the |-redispatchKeyEvent:| call would take care
43     // of invoking the original menu item shortcut as well.
45     if ([[NSApp mainMenu] performKeyEquivalent:event])
46       return true;
48     if ([event_window handleExtraBrowserKeyboardShortcut:event])
49       return true;
51     if ([event_window handleExtraWindowKeyboardShortcut:event])
52       return true;
54     if ([event_window handleDelayedWindowKeyboardShortcut:event])
55       return true;
56   }
58   return [event_window redispatchKeyEvent:event];
61 + (NSString*)scheduleReplaceOldTitle:(NSString*)oldTitle
62                         withNewTitle:(NSString*)newTitle
63                            forWindow:(NSWindow*)window {
64   if (oldTitle)
65     [[NSRunLoop currentRunLoop]
66         cancelPerformSelector:@selector(setTitle:)
67                        target:window
68                      argument:oldTitle];
70   [[NSRunLoop currentRunLoop]
71       performSelector:@selector(setTitle:)
72                target:window
73              argument:newTitle
74                 order:0
75                 modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]];
76   return [newTitle copy];
79 // The titlebar/tabstrip header on the mac is slightly smaller than on Windows.
80 // There is also no window frame to the left and right of the web contents on
81 // mac.
82 // To keep:
83 // - the window background pattern (IDR_THEME_FRAME.*) lined up vertically with
84 // the tab and toolbar patterns
85 // - the toolbar pattern lined up horizontally with the NTP background.
86 // we have to shift the pattern slightly, rather than drawing from the top left
87 // corner of the frame / tabstrip. The offsets below were empirically determined
88 // in order to line these patterns up.
90 // This will make the themes look slightly different than in Windows/Linux
91 // because of the differing heights between window top and tab top, but this has
92 // been approved by UI.
93 const CGFloat kPatternHorizontalOffset = -5;
94 // Without tab strip, offset an extra pixel (determined by experimentation).
95 const CGFloat kPatternVerticalOffset = 2;
96 const CGFloat kPatternVerticalOffsetNoTabStrip = 3;
98 + (NSPoint)themeImagePositionFor:(NSView*)windowView
99                     withTabStrip:(NSView*)tabStripView
100                        alignment:(ThemeImageAlignment)alignment {
101   if (!tabStripView) {
102     return NSMakePoint(kPatternHorizontalOffset,
103                        NSHeight([windowView bounds]) +
104                            kPatternVerticalOffsetNoTabStrip);
105   }
107   NSPoint position =
108       [BrowserWindowUtils themeImagePositionInTabStripCoords:tabStripView
109                                                    alignment:alignment];
110   return [tabStripView convertPoint:position toView:windowView];
113 + (NSPoint)themeImagePositionInTabStripCoords:(NSView*)tabStripView
114                                     alignment:(ThemeImageAlignment)alignment {
115   DCHECK(tabStripView);
117   if (alignment == THEME_IMAGE_ALIGN_WITH_TAB_STRIP) {
118     // The theme image is lined up with the top of the tab which is below the
119     // top of the tab strip.
120     return NSMakePoint(kPatternHorizontalOffset,
121                        [TabStripController defaultTabHeight] +
122                            kPatternVerticalOffset);
123   }
124   // The theme image is lined up with the top of the tab strip (as opposed to
125   // the top of the tab above). This is the same as lining up with the top of
126   // the window's root view when not in presentation mode.
127   return NSMakePoint(kPatternHorizontalOffset,
128                      NSHeight([tabStripView bounds]) +
129                          kPatternVerticalOffsetNoTabStrip);
132 + (void)activateWindowForController:(NSWindowController*)controller {
133   // Per http://crbug.com/73779 and http://crbug.com/75223, we need this to
134   // properly activate windows if Chrome is not the active application.
135   [[controller window] makeKeyAndOrderFront:controller];
136   ProcessSerialNumber psn;
137   GetCurrentProcess(&psn);
138   SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly);
141 @end