// Copyright (c) 2012 The Chromium Embedded Framework Authors. // Portions copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "libcef/browser/browser_host_impl.h" #import #import #include "base/file_util.h" #include "base/mac/mac_util.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" #include "base/threading/thread_restrictions.h" #include "content/public/browser/native_web_keyboard_event.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_view.h" #include "content/public/common/file_chooser_params.h" #import "ui/base/cocoa/underlay_opengl_hosting_window.h" #include "ui/gfx/rect.h" // Wrapper NSView for the native view. Necessary to destroy the browser when // the view is deleted. @interface CefBrowserHostView : NSView { @private CefBrowserHostImpl* browser_; // weak bool is_in_onsetfocus_; } @property (nonatomic, assign) CefBrowserHostImpl* browser; @end @implementation CefBrowserHostView @synthesize browser = browser_; - (void) dealloc { if (browser_) { browser_->DestroyBrowser(); browser_->Release(); } [super dealloc]; } - (BOOL)acceptsFirstResponder { return browser_ && browser_->GetWebContents(); } - (BOOL)becomeFirstResponder { if (browser_ && browser_->GetWebContents()) { // Avoid re-entering OnSetFocus. if (!is_in_onsetfocus_) { is_in_onsetfocus_ = true; browser_->OnSetFocus(FOCUS_SOURCE_SYSTEM); is_in_onsetfocus_ = false; } } return YES; } @end namespace { // Accept-types to file-types helper. NSMutableArray* GetFileTypesFromAcceptTypes( const std::vector& accept_types) { NSMutableArray* acceptArray = [[NSMutableArray alloc] init]; for (size_t i=0; iGetView()->GetNativeView(); [browser_view addSubview:native_view]; [native_view setFrame:bounds]; [native_view setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; [native_view setNeedsDisplay:YES]; window_info_.view = browser_view; if (newWnd != nil && !window_info_.hidden) { // Show the window. [newWnd makeKeyAndOrderFront: nil]; } return true; } void CefBrowserHostImpl::PlatformCloseWindow() { if (window_info_.view != nil) { [[window_info_.view window] performSelector:@selector(performClose:) withObject:nil afterDelay:0]; } } void CefBrowserHostImpl::PlatformSizeTo(int width, int height) { // Not needed; subviews are bound. } CefWindowHandle CefBrowserHostImpl::PlatformGetWindowHandle() { return window_info_.view; } void CefBrowserHostImpl::PlatformHandleKeyboardEvent( const content::NativeWebKeyboardEvent& event) { // Give the top level menu equivalents a chance to handle the event. if ([event.os_event type] == NSKeyDown) [[NSApp mainMenu] performKeyEquivalent:event.os_event]; } void CefBrowserHostImpl::PlatformRunFileChooser( content::WebContents* contents, const content::FileChooserParams& params, std::vector& files) { NSOpenPanel* openPanel = [NSOpenPanel openPanel]; if (!params.title.empty()) [openPanel setTitle:base::SysUTF16ToNSString(params.title)]; // Consider default file name if any. FilePath default_file_name(params.default_file_name); if (!default_file_name.empty()) { if (!default_file_name.BaseName().empty()) { NSString* defaultName = base::SysUTF8ToNSString( default_file_name.BaseName().value()); [openPanel setNameFieldStringValue:defaultName]; } if (!default_file_name.DirName().empty()) { NSString* defaultDir = base::SysUTF8ToNSString( default_file_name.DirName().value()); [openPanel setDirectoryURL:[NSURL fileURLWithPath:defaultDir]]; } } // Consider supported file types if (!params.accept_types.empty()) { [openPanel setAllowedFileTypes:GetFileTypesFromAcceptTypes( params.accept_types)]; } // Further panel configuration. [openPanel setAllowsOtherFileTypes:YES]; [openPanel setAllowsMultipleSelection: (params.mode == content::FileChooserParams::OpenMultiple)]; [openPanel setCanChooseFiles:YES]; [openPanel setCanChooseDirectories:NO]; // Show panel. NSView* view = contents->GetNativeView(); [openPanel beginSheetModalForWindow:[view window] completionHandler:nil]; if ([openPanel runModal] == NSFileHandlingPanelOKButton) { NSArray *urls = [openPanel URLs]; int i, count = [urls count]; for (i=0; i