2010-10-23 19:00:47 +02:00
|
|
|
// Copyright (c) 2008 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.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/browser_webview_delegate.h"
|
|
|
|
#import "libcef/browser_webview_mac.h"
|
|
|
|
#include "libcef/browser_impl.h"
|
|
|
|
#include "libcef/drag_data_impl.h"
|
2011-08-02 18:50:06 +02:00
|
|
|
#import "include/cef_application_mac.h"
|
2010-10-23 19:00:47 +02:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2011-11-03 16:10:37 +01:00
|
|
|
#include "base/file_util.h"
|
2011-08-02 18:50:06 +02:00
|
|
|
#include "base/mac/mac_util.h"
|
2010-10-23 19:00:47 +02:00
|
|
|
#include "base/sys_string_conversions.h"
|
2013-01-18 22:37:07 +01:00
|
|
|
#include "base/utf_string_conversions.h"
|
2011-11-03 16:10:37 +01:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2011-08-02 18:50:06 +02:00
|
|
|
#include "skia/ext/skia_utils_mac.h"
|
2013-02-26 00:44:37 +01:00
|
|
|
#include "third_party/WebKit/Source/Platform/chromium/public/WebDragData.h"
|
|
|
|
#include "third_party/WebKit/Source/Platform/chromium/public/WebImage.h"
|
|
|
|
#include "third_party/WebKit/Source/Platform/chromium/public/WebPoint.h"
|
2011-09-12 21:49:38 +02:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
|
2011-02-15 19:07:24 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h"
|
2013-01-18 22:37:07 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
2011-02-15 19:07:24 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
|
2012-12-06 21:49:39 +01:00
|
|
|
#include "ui/base/layout.h"
|
|
|
|
#include "ui/gfx/image/image_skia.h"
|
|
|
|
#include "ui/gfx/image/image_skia_util_mac.h"
|
2010-10-23 19:00:47 +02:00
|
|
|
#include "webkit/glue/webcursor.h"
|
2011-08-02 18:50:06 +02:00
|
|
|
#include "webkit/glue/webdropdata.h"
|
2011-01-07 22:34:20 +01:00
|
|
|
#include "webkit/plugins/npapi/plugin_list.h"
|
|
|
|
#include "webkit/plugins/npapi/webplugin_delegate_impl.h"
|
2010-10-23 19:00:47 +02:00
|
|
|
#include "webkit/glue/webmenurunner_mac.h"
|
|
|
|
|
2011-09-10 21:40:03 +02:00
|
|
|
using webkit::npapi::WebPluginDelegateImpl;
|
2011-09-12 21:49:38 +02:00
|
|
|
using WebKit::WebContextMenuData;
|
2010-10-23 19:00:47 +02:00
|
|
|
using WebKit::WebCursorInfo;
|
2011-08-02 18:50:06 +02:00
|
|
|
using WebKit::WebDragData;
|
|
|
|
using WebKit::WebDragOperationsMask;
|
2011-04-09 18:54:59 +02:00
|
|
|
using WebKit::WebExternalPopupMenu;
|
|
|
|
using WebKit::WebExternalPopupMenuClient;
|
2011-08-02 18:50:06 +02:00
|
|
|
using WebKit::WebImage;
|
2010-10-23 19:00:47 +02:00
|
|
|
using WebKit::WebNavigationPolicy;
|
2011-08-02 18:50:06 +02:00
|
|
|
using WebKit::WebPoint;
|
2010-10-23 19:00:47 +02:00
|
|
|
using WebKit::WebPopupMenuInfo;
|
|
|
|
using WebKit::WebRect;
|
|
|
|
using WebKit::WebWidget;
|
|
|
|
|
2011-09-12 21:49:38 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void AddMenuItem(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefMenuHandler> handler,
|
|
|
|
NSMenu* menu,
|
2012-05-16 18:56:38 +02:00
|
|
|
id target,
|
2011-12-08 02:38:30 +01:00
|
|
|
cef_menu_id_t menuId,
|
2011-09-12 21:49:38 +02:00
|
|
|
const std::string& label,
|
|
|
|
bool enabled) {
|
|
|
|
std::string disp_str;
|
|
|
|
if (handler.get()) {
|
|
|
|
// Let the handler change the label if desired.
|
|
|
|
CefString actual_label(label);
|
|
|
|
handler->GetMenuLabel(browser, menuId, actual_label);
|
|
|
|
disp_str = actual_label;
|
|
|
|
} else {
|
|
|
|
disp_str = label;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* str = base::SysUTF8ToNSString(disp_str);
|
|
|
|
NSMenuItem* item =
|
|
|
|
[[[NSMenuItem alloc] initWithTitle:str
|
|
|
|
action:enabled?@selector(menuItemSelected:):nil
|
2012-05-16 18:56:38 +02:00
|
|
|
keyEquivalent:@""] autorelease];
|
|
|
|
[item setTarget:target];
|
2011-09-12 21:49:38 +02:00
|
|
|
[item setTag:menuId];
|
|
|
|
[menu addItem:item];
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddMenuSeparator(NSMenu* menu) {
|
|
|
|
NSMenuItem* item = [NSMenuItem separatorItem];
|
|
|
|
[menu addItem:item];
|
|
|
|
}
|
|
|
|
|
2013-01-18 22:37:07 +01:00
|
|
|
NSString* GetDialogLabel(WebKit::WebFrame* webframe, const std::string& label) {
|
|
|
|
const GURL& url = webframe->document().url();
|
|
|
|
std::string urlStr;
|
|
|
|
if (!url.is_empty())
|
|
|
|
urlStr = url.host();
|
|
|
|
string16 labelStr = ASCIIToUTF16(label);
|
|
|
|
if (!urlStr.empty())
|
|
|
|
labelStr += ASCIIToUTF16(" - " + urlStr);
|
|
|
|
return base::SysUTF16ToNSString(labelStr);
|
|
|
|
}
|
|
|
|
|
2011-09-12 21:49:38 +02:00
|
|
|
} // namespace
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
@interface BrowserMenuDelegate : NSObject <NSMenuDelegate> {
|
|
|
|
@private
|
|
|
|
CefRefPtr<CefBrowserImpl> browser_;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithBrowser:(CefBrowserImpl*)browser;
|
|
|
|
- (void)menuItemSelected:(id)sender;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BrowserMenuDelegate
|
|
|
|
|
|
|
|
- (id)initWithBrowser:(CefBrowserImpl*)browser {
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
browser_ = browser;
|
|
|
|
return self;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when a context menu item is selected by the user.
|
|
|
|
- (void)menuItemSelected:(id)sender {
|
|
|
|
cef_menu_id_t menuId = static_cast<cef_menu_id_t>([sender tag]);
|
|
|
|
bool handled = false;
|
|
|
|
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefMenuHandler> handler = client->GetMenuHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
// Ask the handler if it wants to handle the action.
|
|
|
|
handled = handler->OnMenuAction(browser_.get(), menuId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!handled) {
|
|
|
|
// Execute the action.
|
|
|
|
browser_->UIT_HandleAction(menuId, browser_->GetFocusedFrame());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2011-09-12 21:49:38 +02:00
|
|
|
|
2010-10-23 19:00:47 +02:00
|
|
|
// WebViewClient --------------------------------------------------------------
|
|
|
|
|
2011-04-09 18:54:59 +02:00
|
|
|
WebExternalPopupMenu* BrowserWebViewDelegate::createExternalPopupMenu(
|
|
|
|
const WebPopupMenuInfo& info,
|
|
|
|
WebExternalPopupMenuClient* client) {
|
|
|
|
DCHECK(!external_popup_menu_.get());
|
|
|
|
external_popup_menu_.reset(new ExternalPopupMenu(this, info, client));
|
|
|
|
return external_popup_menu_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::ClosePopupMenu() {
|
|
|
|
if (external_popup_menu_ == NULL) {
|
|
|
|
NOTREACHED();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
external_popup_menu_.reset();
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::showContextMenu(
|
|
|
|
WebKit::WebFrame* frame, const WebKit::WebContextMenuData& data) {
|
2011-09-12 21:49:38 +02:00
|
|
|
WebWidgetHost* host = GetWidgetHost();
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
NSView *view = browser_->UIT_GetMainWndHandle();
|
2011-09-12 21:49:38 +02:00
|
|
|
if (!view)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NSWindow* window = [view window];
|
2012-05-16 18:56:38 +02:00
|
|
|
|
|
|
|
int screenX = -1;
|
|
|
|
int screenY = -1;
|
|
|
|
NSPoint mouse_pt = {data.mousePosition.x, data.mousePosition.y};
|
|
|
|
if (!browser_->IsWindowRenderingDisabled()) {
|
|
|
|
mouse_pt = [window mouseLocationOutsideOfEventStream];
|
|
|
|
NSPoint screen_pt = [window convertBaseToScreen:mouse_pt];
|
|
|
|
screenX = screen_pt.x;
|
|
|
|
screenY = screen_pt.y;
|
|
|
|
}
|
2011-09-12 21:49:38 +02:00
|
|
|
|
|
|
|
int edit_flags = 0;
|
|
|
|
int type_flags = 0;
|
|
|
|
NSMenu* menu = nil;
|
|
|
|
|
|
|
|
// Make sure events can be pumped while the menu is up.
|
|
|
|
MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
|
|
|
|
|
|
|
|
// Give the client a chance to handle the menu.
|
2012-05-16 18:56:38 +02:00
|
|
|
if (OnBeforeMenu(data, mouse_pt.x, mouse_pt.y, edit_flags, type_flags))
|
2011-09-12 21:49:38 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
CefRefPtr<CefMenuHandler> handler;
|
|
|
|
if (client.get())
|
|
|
|
handler = client->GetMenuHandler();
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
if (client.get() && browser_->IsWindowRenderingDisabled()) {
|
|
|
|
// Retrieve the screen coordinates.
|
|
|
|
CefRefPtr<CefRenderHandler> render_handler = client->GetRenderHandler();
|
|
|
|
if (!render_handler.get() ||
|
|
|
|
!render_handler->GetScreenPoint(browser_, mouse_pt.x, mouse_pt.y,
|
|
|
|
screenX, screenY)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserMenuDelegate* delegate =
|
|
|
|
[[[BrowserMenuDelegate alloc] initWithBrowser:browser_] autorelease];
|
|
|
|
|
2011-09-12 21:49:38 +02:00
|
|
|
// Build the correct default context menu
|
|
|
|
if (type_flags & MENUTYPE_EDITABLE) {
|
|
|
|
menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_UNDO, "Undo",
|
2011-09-12 21:49:38 +02:00
|
|
|
!!(edit_flags & MENU_CAN_UNDO));
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_REDO, "Redo",
|
2011-09-12 21:49:38 +02:00
|
|
|
!!(edit_flags & MENU_CAN_REDO));
|
|
|
|
AddMenuSeparator(menu);
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_CUT, "Cut",
|
2011-09-12 21:49:38 +02:00
|
|
|
!!(edit_flags & MENU_CAN_CUT));
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_COPY, "Copy",
|
2011-09-12 21:49:38 +02:00
|
|
|
!!(edit_flags & MENU_CAN_COPY));
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_PASTE, "Paste",
|
2011-09-12 21:49:38 +02:00
|
|
|
!!(edit_flags & MENU_CAN_PASTE));
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_DELETE, "Delete",
|
2011-09-12 21:49:38 +02:00
|
|
|
!!(edit_flags & MENU_CAN_DELETE));
|
|
|
|
AddMenuSeparator(menu);
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_SELECTALL,
|
|
|
|
"Select All", !!(edit_flags & MENU_CAN_SELECT_ALL));
|
2011-09-12 21:49:38 +02:00
|
|
|
} else if(type_flags & MENUTYPE_SELECTION) {
|
|
|
|
menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_COPY, "Copy",
|
2011-09-12 21:49:38 +02:00
|
|
|
!!(edit_flags & MENU_CAN_COPY));
|
|
|
|
} else if(type_flags & (MENUTYPE_PAGE | MENUTYPE_FRAME)) {
|
|
|
|
menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_NAV_BACK, "Back",
|
2011-09-12 21:49:38 +02:00
|
|
|
!!(edit_flags & MENU_CAN_GO_BACK));
|
2012-05-16 18:56:38 +02:00
|
|
|
AddMenuItem(browser_, handler, menu, delegate, MENU_ID_NAV_FORWARD,
|
|
|
|
"Forward", !!(edit_flags & MENU_CAN_GO_FORWARD));
|
2011-09-12 21:49:38 +02:00
|
|
|
// TODO(port): Enable the below menu items when supported.
|
|
|
|
//AddMenuSeparator(menu);
|
2012-05-16 18:56:38 +02:00
|
|
|
//AddMenuItem(browser_, handler, menu, delegate, MENU_ID_PRINT, "Print",
|
|
|
|
// true);
|
|
|
|
//AddMenuItem(browser_, handler, menu, delegate, MENU_ID_VIEWSOURCE,
|
|
|
|
// "View Source", true);
|
2011-09-12 21:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!menu)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Synthesize an event for the click, as there is no certainty that
|
|
|
|
// [NSApp currentEvent] will return a valid event.
|
2012-05-16 18:56:38 +02:00
|
|
|
NSPoint screen_pt = {screenX, screenY};
|
|
|
|
NSPoint window_pt = [window convertScreenToBase:screen_pt];
|
|
|
|
|
2011-09-12 21:49:38 +02:00
|
|
|
NSEvent* currentEvent = [NSApp currentEvent];
|
|
|
|
NSTimeInterval eventTime = [currentEvent timestamp];
|
|
|
|
NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
|
2012-05-16 18:56:38 +02:00
|
|
|
location:window_pt
|
|
|
|
modifierFlags:NSRightMouseDownMask
|
|
|
|
timestamp:eventTime
|
|
|
|
windowNumber:[window windowNumber]
|
|
|
|
context:nil
|
|
|
|
eventNumber:0
|
|
|
|
clickCount:1
|
|
|
|
pressure:1.0];
|
2011-09-12 21:49:38 +02:00
|
|
|
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
// Menu selection events go to the BrowserMenuDelegate.
|
|
|
|
[menu setDelegate:delegate];
|
2011-09-12 21:49:38 +02:00
|
|
|
|
|
|
|
// Show the menu.
|
|
|
|
[NSMenu popUpContextMenu:menu
|
|
|
|
withEvent:clickEvent
|
|
|
|
forView:view];
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WebWidgetClient ------------------------------------------------------------
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::show(WebNavigationPolicy policy) {
|
2012-05-16 18:56:38 +02:00
|
|
|
DCHECK(this != browser_->UIT_GetPopupDelegate());
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::didChangeCursor(const WebCursorInfo& cursor_info) {
|
2012-02-18 01:19:52 +01:00
|
|
|
NSCursor* ns_cursor = WebCursor(cursor_info).GetNativeCursor();
|
2012-05-16 18:56:38 +02:00
|
|
|
|
|
|
|
if (!browser_->IsWindowRenderingDisabled()) {
|
|
|
|
[ns_cursor set];
|
|
|
|
} else {
|
|
|
|
// Notify the handler of cursor change.
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefRenderHandler> handler = client->GetRenderHandler();
|
|
|
|
if (handler.get())
|
|
|
|
handler->OnCursorChange(browser_, ns_cursor);
|
|
|
|
}
|
|
|
|
}
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WebRect BrowserWebViewDelegate::windowRect() {
|
|
|
|
if (WebWidgetHost* host = GetWidgetHost()) {
|
2012-05-16 18:56:38 +02:00
|
|
|
if (!browser_->IsWindowRenderingDisabled()) {
|
|
|
|
NSView *view = host->view_handle();
|
|
|
|
NSRect rect = [view frame];
|
|
|
|
return gfx::Rect(NSRectToCGRect(rect));
|
|
|
|
} else {
|
|
|
|
// Retrieve the view rectangle from the handler.
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefRenderHandler> handler = client->GetRenderHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRect rect(0, 0, 0, 0);
|
|
|
|
if (handler->GetViewRect(browser_, rect))
|
|
|
|
return WebRect(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
return WebRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::setWindowRect(const WebRect& rect) {
|
2011-01-29 02:42:59 +01:00
|
|
|
if (this == browser_->UIT_GetWebViewDelegate()) {
|
2010-10-23 19:00:47 +02:00
|
|
|
// TODO(port): Set the window rectangle.
|
2012-05-16 18:56:38 +02:00
|
|
|
} else if (this == browser_->UIT_GetPopupDelegate()) {
|
|
|
|
NOTREACHED();
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebRect BrowserWebViewDelegate::rootWindowRect() {
|
|
|
|
if (WebWidgetHost* host = GetWidgetHost()) {
|
|
|
|
NSView *view = host->view_handle();
|
|
|
|
NSRect rect = [[[view window] contentView] frame];
|
|
|
|
return gfx::Rect(NSRectToCGRect(rect));
|
|
|
|
}
|
|
|
|
return WebRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
@interface NSWindow(OSInternals)
|
|
|
|
- (NSRect)_growBoxRect;
|
|
|
|
@end
|
|
|
|
|
|
|
|
WebRect BrowserWebViewDelegate::windowResizerRect() {
|
|
|
|
NSRect resize_rect = NSMakeRect(0, 0, 0, 0);
|
|
|
|
WebWidgetHost* host = GetWidgetHost();
|
|
|
|
if (host) {
|
|
|
|
NSView *view = host->view_handle();
|
|
|
|
NSWindow* window = [view window];
|
2011-07-03 04:00:01 +02:00
|
|
|
if (window == nil)
|
|
|
|
return gfx::Rect();
|
|
|
|
|
2010-10-23 19:00:47 +02:00
|
|
|
resize_rect = [window _growBoxRect];
|
|
|
|
// The scrollbar assumes that the resizer goes all the way down to the
|
|
|
|
// bottom corner, so we ignore any y offset to the rect itself and use the
|
|
|
|
// entire bottom corner.
|
|
|
|
resize_rect.origin.y = 0;
|
|
|
|
// Convert to view coordinates from window coordinates.
|
|
|
|
resize_rect = [view convertRect:resize_rect fromView:nil];
|
|
|
|
// Flip the rect in view coordinates
|
|
|
|
resize_rect.origin.y =
|
|
|
|
[view frame].size.height - resize_rect.origin.y -
|
|
|
|
resize_rect.size.height;
|
|
|
|
}
|
|
|
|
return gfx::Rect(NSRectToCGRect(resize_rect));
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
void BrowserWebViewDelegate::startDragging(
|
2012-05-23 22:33:32 +02:00
|
|
|
WebKit::WebFrame* frame,
|
2012-05-23 21:01:04 +02:00
|
|
|
const WebDragData& data,
|
|
|
|
WebDragOperationsMask mask,
|
|
|
|
const WebImage& image,
|
|
|
|
const WebPoint& image_offset) {
|
2012-05-16 18:56:38 +02:00
|
|
|
if (browser_->settings().drag_drop_disabled ||
|
|
|
|
browser_->IsWindowRenderingDisabled()) {
|
2011-08-02 19:30:11 +02:00
|
|
|
browser_->UIT_GetWebView()->dragSourceSystemDragEnded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-02 18:50:06 +02:00
|
|
|
WebWidgetHost* host = GetWidgetHost();
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BrowserWebView *view = static_cast<BrowserWebView*>(host->view_handle());
|
|
|
|
if (!view)
|
|
|
|
return;
|
|
|
|
|
2011-08-22 03:31:55 +02:00
|
|
|
WebDropData drop_data(data);
|
|
|
|
|
|
|
|
CefRefPtr<CefClient> client = browser_->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefDragHandler> handler = client->GetDragHandler();
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefDragData> data(new CefDragDataImpl(drop_data));
|
|
|
|
if (handler->OnDragStart(browser_, data,
|
|
|
|
static_cast<CefDragHandler::DragOperationsMask>(mask))) {
|
|
|
|
browser_->UIT_GetWebView()->dragSourceSystemDragEnded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-02 18:50:06 +02:00
|
|
|
// By allowing nested tasks, the code below also allows Close(),
|
|
|
|
// which would deallocate |this|. The same problem can occur while
|
|
|
|
// processing -sendEvent:, so Close() is deferred in that case.
|
|
|
|
// Drags from web content do not come via -sendEvent:, this sets the
|
|
|
|
// same flag -sendEvent: would.
|
|
|
|
CefScopedSendingEvent sendingEventScoper;
|
|
|
|
|
|
|
|
// The drag invokes a nested event loop, arrange to continue
|
|
|
|
// processing events.
|
|
|
|
MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
|
2011-08-22 03:31:55 +02:00
|
|
|
|
|
|
|
NSImage* ns_image = nil;
|
|
|
|
if (!image.isNull()) {
|
2012-12-06 21:49:39 +01:00
|
|
|
ui::ScaleFactor scale_factor =
|
|
|
|
ui::GetScaleFactorForNativeView(host->view_handle());
|
|
|
|
gfx::ImageSkia image_skia(
|
|
|
|
gfx::ImageSkiaRep(image.getSkBitmap(), scale_factor));
|
|
|
|
ns_image = gfx::NSImageFromImageSkia(image_skia);
|
2011-08-22 03:31:55 +02:00
|
|
|
}
|
2011-08-02 18:50:06 +02:00
|
|
|
NSPoint offset = NSPointFromCGPoint(gfx::Point(image_offset).ToCGPoint());
|
2012-06-01 20:40:04 +02:00
|
|
|
|
|
|
|
// Keep a reference to the NSView so that it won't be destroyed until after
|
|
|
|
// the drag operation has completed.
|
|
|
|
[view retain];
|
|
|
|
|
2011-08-22 03:31:55 +02:00
|
|
|
[view startDragWithDropData:drop_data
|
|
|
|
dragOperationMask:static_cast<NSDragOperation>(mask)
|
2011-08-02 18:50:06 +02:00
|
|
|
image:ns_image
|
|
|
|
offset:offset];
|
2012-06-01 20:40:04 +02:00
|
|
|
|
|
|
|
[view release];
|
2011-08-02 18:50:06 +02:00
|
|
|
}
|
|
|
|
|
2010-10-23 19:00:47 +02:00
|
|
|
void BrowserWebViewDelegate::runModal() {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebPluginPageDelegate ------------------------------------------------------
|
|
|
|
|
2011-01-07 22:34:20 +01:00
|
|
|
webkit::npapi::WebPluginDelegate* BrowserWebViewDelegate::CreatePluginDelegate(
|
2013-02-26 00:44:37 +01:00
|
|
|
const base::FilePath& path,
|
2010-10-23 19:00:47 +02:00
|
|
|
const std::string& mime_type) {
|
|
|
|
WebWidgetHost *host = GetWidgetHost();
|
|
|
|
if (!host)
|
|
|
|
return NULL;
|
|
|
|
|
2012-11-06 18:13:58 +01:00
|
|
|
WebPluginDelegateImpl* delegate = WebPluginDelegateImpl::Create(path, mime_type);
|
2011-09-10 21:40:03 +02:00
|
|
|
if (delegate)
|
|
|
|
delegate->SetNoBufferContext();
|
|
|
|
return delegate;
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::CreatedPluginWindow(
|
|
|
|
gfx::PluginWindowHandle handle) {
|
2012-05-16 18:56:38 +02:00
|
|
|
if (browser_->IsWindowRenderingDisabled()) {
|
|
|
|
WebViewHost* host = browser_->UIT_GetWebViewHost();
|
|
|
|
if (host)
|
|
|
|
host->AddWindowedPlugin(handle);
|
|
|
|
}
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::WillDestroyPluginWindow(
|
|
|
|
gfx::PluginWindowHandle handle) {
|
2012-05-16 18:56:38 +02:00
|
|
|
if (browser_->IsWindowRenderingDisabled()) {
|
|
|
|
WebViewHost* host = browser_->UIT_GetWebViewHost();
|
|
|
|
if (host)
|
|
|
|
host->RemoveWindowedPlugin(handle);
|
|
|
|
}
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::DidMovePlugin(
|
2011-01-07 22:34:20 +01:00
|
|
|
const webkit::npapi::WebPluginGeometry& move) {
|
2012-05-16 18:56:38 +02:00
|
|
|
if (browser_->IsWindowRenderingDisabled()) {
|
|
|
|
WebViewHost* host = browser_->UIT_GetWebViewHost();
|
|
|
|
if (host) {
|
|
|
|
host->MoveWindowedPlugin(move);
|
|
|
|
}
|
|
|
|
}
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Protected methods ----------------------------------------------------------
|
|
|
|
|
|
|
|
void BrowserWebViewDelegate::ShowJavaScriptAlert(
|
2010-11-23 15:46:01 +01:00
|
|
|
WebKit::WebFrame* webframe, const CefString& message) {
|
2013-01-18 22:37:07 +01:00
|
|
|
NSString* label = GetDialogLabel(webframe, "JavaScript Alert");
|
2010-11-23 15:46:01 +01:00
|
|
|
std::string messageStr(message);
|
2013-01-18 22:37:07 +01:00
|
|
|
NSString* text = [NSString stringWithUTF8String:messageStr.c_str()];
|
|
|
|
|
|
|
|
NSAlert* alert = [NSAlert alertWithMessageText:label
|
2010-10-23 19:00:47 +02:00
|
|
|
defaultButton:@"OK"
|
|
|
|
alternateButton:nil
|
|
|
|
otherButton:nil
|
|
|
|
informativeTextWithFormat:text];
|
|
|
|
[alert runModal];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BrowserWebViewDelegate::ShowJavaScriptConfirm(
|
2010-11-23 15:46:01 +01:00
|
|
|
WebKit::WebFrame* webframe, const CefString& message) {
|
2013-01-18 22:37:07 +01:00
|
|
|
NSString* label = GetDialogLabel(webframe, "JavaScript Confirm");
|
|
|
|
std::string messageStr(message);
|
|
|
|
NSString* text = [NSString stringWithUTF8String:messageStr.c_str()];
|
|
|
|
|
|
|
|
NSAlert *alert = [NSAlert alertWithMessageText:label
|
|
|
|
defaultButton:@"OK"
|
|
|
|
alternateButton:@"Cancel"
|
|
|
|
otherButton:nil
|
|
|
|
informativeTextWithFormat:text];
|
|
|
|
|
|
|
|
NSInteger r = [alert runModal];
|
|
|
|
|
|
|
|
return (r == NSAlertDefaultReturn);
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BrowserWebViewDelegate::ShowJavaScriptPrompt(
|
2010-11-23 15:46:01 +01:00
|
|
|
WebKit::WebFrame* webframe, const CefString& message,
|
|
|
|
const CefString& default_value, CefString* result) {
|
2013-01-18 22:37:07 +01:00
|
|
|
NSString* label = GetDialogLabel(webframe, "JavaScript Prompt");
|
|
|
|
|
|
|
|
NSAlert *alert =
|
|
|
|
[NSAlert alertWithMessageText:label
|
|
|
|
defaultButton:@"OK"
|
|
|
|
alternateButton:@"Cancel"
|
|
|
|
otherButton:nil
|
|
|
|
informativeTextWithFormat:@""];
|
|
|
|
|
|
|
|
NSTextField *input =
|
|
|
|
[[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)];
|
|
|
|
[[input cell] setLineBreakMode:NSLineBreakByTruncatingTail];
|
|
|
|
std::string default_valueStr(default_value);
|
|
|
|
[input setStringValue:
|
|
|
|
[NSString stringWithUTF8String:default_valueStr.c_str()]];
|
|
|
|
[alert setAccessoryView:input];
|
|
|
|
[input release];
|
|
|
|
|
|
|
|
std::string messageStr(message);
|
|
|
|
[alert setInformativeText:[NSString stringWithUTF8String:messageStr.c_str()]];
|
|
|
|
|
|
|
|
NSInteger r = [alert runModal];
|
|
|
|
if (r == NSAlertDefaultReturn) {
|
|
|
|
[input validateEditing];
|
|
|
|
*result = base::SysNSStringToUTF8([input stringValue]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (r == NSAlertDefaultReturn);
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called to show the file chooser dialog.
|
2013-01-18 19:00:25 +01:00
|
|
|
bool BrowserWebViewDelegate::ShowFileChooser(
|
2013-02-26 00:44:37 +01:00
|
|
|
std::vector<base::FilePath>& file_names,
|
2013-01-18 19:00:25 +01:00
|
|
|
bool multi_select,
|
|
|
|
const WebKit::WebString& title,
|
2013-02-26 00:44:37 +01:00
|
|
|
const base::FilePath& default_file,
|
2013-01-18 19:00:25 +01:00
|
|
|
const std::vector<std::string>& accept_mime_types) {
|
2011-11-03 16:10:37 +01:00
|
|
|
NSOpenPanel* dialog = [NSOpenPanel openPanel];
|
|
|
|
if (!title.isNull())
|
|
|
|
[dialog setTitle:base::SysUTF16ToNSString(title)];
|
|
|
|
|
|
|
|
NSString* default_dir = nil;
|
|
|
|
NSString* default_filename = nil;
|
|
|
|
if (!default_file.empty()) {
|
|
|
|
// The file dialog is going to do a ton of stats anyway. Not much
|
|
|
|
// point in eliminating this one.
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
|
|
|
if (file_util::DirectoryExists(default_file)) {
|
|
|
|
default_dir = base::SysUTF8ToNSString(default_file.value());
|
|
|
|
} else {
|
|
|
|
default_dir = base::SysUTF8ToNSString(default_file.DirName().value());
|
|
|
|
default_filename =
|
|
|
|
base::SysUTF8ToNSString(default_file.BaseName().value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[dialog setAllowsOtherFileTypes:YES];
|
|
|
|
[dialog setAllowsMultipleSelection:multi_select];
|
|
|
|
[dialog setCanChooseFiles:YES];
|
|
|
|
[dialog setCanChooseDirectories:NO];
|
|
|
|
|
2012-11-26 21:01:27 +01:00
|
|
|
// [NSOpenPanel runModalForDirectory:file:] was deprecated on OS-X 10.7.
|
|
|
|
NSInteger result;
|
|
|
|
if ([NSOpenPanel respondsToSelector:@selector(runModalForDirectory:file:)]) {
|
|
|
|
result = (NSInteger) [dialog performSelector:@selector(runModalForDirectory:file:)
|
|
|
|
withObject:default_dir
|
|
|
|
withObject:default_filename];
|
|
|
|
} else {
|
2013-01-18 18:23:56 +01:00
|
|
|
if (default_dir)
|
|
|
|
[dialog setDirectoryURL:[NSURL URLWithString:default_dir]];
|
|
|
|
if (default_filename)
|
|
|
|
[dialog setNameFieldStringValue:default_filename];
|
2012-11-26 21:01:27 +01:00
|
|
|
result = [dialog runModal];
|
|
|
|
}
|
|
|
|
|
2011-11-04 20:52:14 +01:00
|
|
|
if (result == NSFileHandlingPanelCancelButton)
|
2011-11-03 16:10:37 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
NSArray *urls = [dialog URLs];
|
|
|
|
int i, count = [urls count];
|
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
NSURL* url = [urls objectAtIndex:i];
|
2013-02-26 00:44:37 +01:00
|
|
|
if ([url isFileURL]) {
|
|
|
|
file_names.push_back(
|
|
|
|
base::FilePath(base::SysNSStringToUTF8([url path])));
|
|
|
|
}
|
2011-11-03 16:10:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-10-23 19:00:47 +02:00
|
|
|
}
|