Add 'context-menu' event to 'WebContents'

This commit is contained in:
Daniel Pham 2016-05-02 20:02:33 -04:00
parent d64e3784f4
commit 0fffbea79d
6 changed files with 166 additions and 4 deletions

View file

@ -440,11 +440,13 @@ void WebContents::RendererResponsive(content::WebContents* source) {
}
bool WebContents::HandleContextMenu(const content::ContextMenuParams& params) {
if (!params.custom_context.is_pepper_menu)
return false;
if (params.custom_context.is_pepper_menu) {
Emit("pepper-context-menu", std::make_pair(params, web_contents()));
web_contents()->NotifyContextMenuClosed(params.custom_context);
} else {
Emit("context-menu", std::make_pair(params, web_contents()));
}
return true;
}

View file

@ -15,6 +15,7 @@
#include "third_party/WebKit/public/web/WebDeviceEmulationParams.h"
#include "third_party/WebKit/public/web/WebFindOptions.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "ui/base/clipboard/clipboard.h"
namespace {
@ -296,4 +297,91 @@ bool Converter<blink::WebFindOptions>::FromV8(
return true;
}
// static
v8::Local<v8::Value> Converter<blink::WebContextMenuData::MediaType>::ToV8(
v8::Isolate* isolate, const blink::WebContextMenuData::MediaType& in) {
switch (in) {
case blink::WebContextMenuData::MediaTypeImage:
return mate::StringToV8(isolate, "image");
case blink::WebContextMenuData::MediaTypeVideo:
return mate::StringToV8(isolate, "video");
case blink::WebContextMenuData::MediaTypeAudio:
return mate::StringToV8(isolate, "audio");
case blink::WebContextMenuData::MediaTypeCanvas:
return mate::StringToV8(isolate, "canvas");
case blink::WebContextMenuData::MediaTypeFile:
return mate::StringToV8(isolate, "file");
case blink::WebContextMenuData::MediaTypePlugin:
return mate::StringToV8(isolate, "plugin");
default:
return mate::StringToV8(isolate, "none");
}
}
// static
v8::Local<v8::Value> Converter<blink::WebContextMenuData::InputFieldType>::ToV8(
v8::Isolate* isolate,
const blink::WebContextMenuData::InputFieldType& in) {
switch (in) {
case blink::WebContextMenuData::InputFieldTypePlainText:
return mate::StringToV8(isolate, "plain-text");
case blink::WebContextMenuData::InputFieldTypePassword:
return mate::StringToV8(isolate, "password");
case blink::WebContextMenuData::InputFieldTypeOther:
return mate::StringToV8(isolate, "other");
default:
return mate::StringToV8(isolate, "none");
}
}
v8::Local<v8::Value> EditFlagsToV8(v8::Isolate* isolate, int editFlags) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("canUndo",
!!(editFlags & blink::WebContextMenuData::CanUndo));
dict.Set("canRedo",
!!(editFlags & blink::WebContextMenuData::CanRedo));
dict.Set("canCut",
!!(editFlags & blink::WebContextMenuData::CanCut));
dict.Set("canCopy",
!!(editFlags & blink::WebContextMenuData::CanCopy));
bool pasteFlag = false;
if (editFlags & blink::WebContextMenuData::CanPaste) {
std::vector<base::string16> types;
bool ignore;
ui::Clipboard::GetForCurrentThread()->ReadAvailableTypes(
ui::CLIPBOARD_TYPE_COPY_PASTE, &types, &ignore);
pasteFlag = !types.empty();
}
dict.Set("canPaste", pasteFlag);
dict.Set("canDelete",
!!(editFlags & blink::WebContextMenuData::CanDelete));
dict.Set("canSelectAll",
!!(editFlags & blink::WebContextMenuData::CanSelectAll));
return mate::ConvertToV8(isolate, dict);
}
v8::Local<v8::Value> MediaFlagsToV8(v8::Isolate* isolate, int mediaFlags) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("inError",
!!(mediaFlags & blink::WebContextMenuData::MediaInError));
dict.Set("isPaused",
!!(mediaFlags & blink::WebContextMenuData::MediaPaused));
dict.Set("isMuted",
!!(mediaFlags & blink::WebContextMenuData::MediaMuted));
dict.Set("hasAudio",
!!(mediaFlags & blink::WebContextMenuData::MediaHasAudio));
dict.Set("isLooping",
(mediaFlags & blink::WebContextMenuData::MediaLoop) != 0);
dict.Set("isControlsVisible",
(mediaFlags & blink::WebContextMenuData::MediaControls) != 0);
dict.Set("canToggleControls",
!!(mediaFlags & blink::WebContextMenuData::MediaCanToggleControls));
dict.Set("canRotate",
!!(mediaFlags & blink::WebContextMenuData::MediaCanRotate));
return mate::ConvertToV8(isolate, dict);
}
} // namespace mate

View file

@ -6,6 +6,7 @@
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_
#include "native_mate/converter.h"
#include "third_party/WebKit/public/web/WebContextMenuData.h"
namespace blink {
class WebInputEvent;
@ -87,6 +88,22 @@ struct Converter<blink::WebFindOptions> {
blink::WebFindOptions* out);
};
template<>
struct Converter<blink::WebContextMenuData::MediaType> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const blink::WebContextMenuData::MediaType& in);
};
template<>
struct Converter<blink::WebContextMenuData::InputFieldType> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const blink::WebContextMenuData::InputFieldType& in);
};
v8::Local<v8::Value> EditFlagsToV8(v8::Isolate* isolate, int editFlags);
v8::Local<v8::Value> MediaFlagsToV8(v8::Isolate* isolate, int mediaFlags);
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_

View file

@ -9,8 +9,11 @@
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/web_contents_permission_helper.h"
#include "atom/common/native_mate_converters/blink_converter.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/ui_base_types_converter.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/context_menu_params.h"
#include "native_mate/dictionary.h"
@ -94,6 +97,23 @@ v8::Local<v8::Value> Converter<ContextMenuParamsWithWebContents>::ToV8(
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("x", params.x);
dict.Set("y", params.y);
dict.Set("linkURL", params.link_url);
dict.Set("linkText", params.link_text);
dict.Set("pageURL", params.page_url);
dict.Set("frameURL", params.frame_url);
dict.Set("srcURL", params.src_url);
dict.Set("mediaType", params.media_type);
dict.Set("mediaFlags", MediaFlagsToV8(isolate, params.media_flags));
dict.Set("hasImageContents", params.has_image_contents);
dict.Set("isEditable", params.is_editable);
dict.Set("editFlags", EditFlagsToV8(isolate, params.edit_flags));
dict.Set("selectionText", params.selection_text);
dict.Set("titleText", params.title_text);
dict.Set("misspelledWord", params.misspelled_word);
dict.Set("frameCharset", params.frame_charset);
dict.Set("inputFieldType", params.input_field_type);
dict.Set("menuSourceType", params.source_type);
if (params.custom_context.is_pepper_menu)
dict.Set("menu", MenuToV8(isolate, val.second, params.custom_context,
params.custom_items));

View file

@ -0,0 +1,34 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_UI_BASE_TYPES_CONVERTER_H_
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_UI_BASE_TYPES_CONVERTER_H_
#include "native_mate/converter.h"
#include "ui/base/ui_base_types.h"
namespace mate {
template<>
struct Converter<ui::MenuSourceType> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const ui::MenuSourceType& in) {
switch (in) {
case ui::MENU_SOURCE_MOUSE:
return mate::StringToV8(isolate, "mouse");
case ui::MENU_SOURCE_KEYBOARD:
return mate::StringToV8(isolate, "keyboard");
case ui::MENU_SOURCE_TOUCH:
return mate::StringToV8(isolate, "touch");
case ui::MENU_SOURCE_TOUCH_EDIT_MENU:
return mate::StringToV8(isolate, "touch-menu");
default:
return mate::StringToV8(isolate, "none");
}
}
};
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_UI_BASE_TYPES_CONVERTER_H_

View file

@ -362,6 +362,7 @@
'atom/common/native_mate_converters/net_converter.cc',
'atom/common/native_mate_converters/net_converter.h',
'atom/common/native_mate_converters/string16_converter.h',
'atom/common/native_mate_converters/ui_base_types_converter.h',
'atom/common/native_mate_converters/v8_value_converter.cc',
'atom/common/native_mate_converters/v8_value_converter.h',
'atom/common/native_mate_converters/value_converter.cc',