diff options
author | kinaba@chromium.org <kinaba@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-12 11:38:32 +0000 |
---|---|---|
committer | kinaba@chromium.org <kinaba@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-12 11:38:32 +0000 |
commit | 73bf95813e1c23c5fb2eaadc7e88ce248084b083 (patch) | |
tree | e1d5ba6ff5d4647b5de4d908394026bbeb5fa0d4 /webkit | |
parent | 137bc957b93eae5f02969880b89c15e48d02fd86 (diff) | |
download | chromium_src-73bf95813e1c23c5fb2eaadc7e88ce248084b083.zip chromium_src-73bf95813e1c23c5fb2eaadc7e88ce248084b083.tar.gz chromium_src-73bf95813e1c23c5fb2eaadc7e88ce248084b083.tar.bz2 |
Implement Pepper IME API.
BUG=59425
TEST=Build chrome and ppapi_example_ime,
Confirm "out/Release/chrome --register-pepper-plugins='out/Release/lib/libppapi_example_ime.so;application/x-ppapi-example' ppapi/examples/ime/ime.html" works.
This CL is the last part for adding the basic IME support for PPAPI,
preceded by the previous two changes
codereview.chromium.org/7882004 (API declarations) and
codereview.chromium.org/7978019 (thunk and proxy implementation).
This CL comes with the actual Chrome-side implementation of the API
with an example to show how to use IME in PPAPI.
Keep in mind the current implementation still not reached the point of "the complete" set of IME APIs yet.
- Advanced features in design doc (like surrounding text retrieval) is not available.
- DOM and PPAPI composition events are not converted each other.
Rather, it aims to provide basic set of functions just needed to implement inline composition in plugins.
Review URL: http://codereview.chromium.org/8073021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105056 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/glue/webkit_glue.gypi | 1 | ||||
-rw-r--r-- | webkit/plugins/ppapi/DEPS | 1 | ||||
-rw-r--r-- | webkit/plugins/ppapi/mock_plugin_delegate.cc | 10 | ||||
-rw-r--r-- | webkit/plugins/ppapi/mock_plugin_delegate.h | 4 | ||||
-rw-r--r-- | webkit/plugins/ppapi/plugin_delegate.h | 9 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppapi_plugin_instance.cc | 164 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppapi_plugin_instance.h | 46 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_text_input_impl.cc | 36 |
8 files changed, 247 insertions, 24 deletions
diff --git a/webkit/glue/webkit_glue.gypi b/webkit/glue/webkit_glue.gypi index 83b09fd..89c42c2 100644 --- a/webkit/glue/webkit_glue.gypi +++ b/webkit/glue/webkit_glue.gypi @@ -116,6 +116,7 @@ '<(DEPTH)/third_party/icu/icu.gyp:icui18n', '<(DEPTH)/third_party/icu/icu.gyp:icuuc', '<(DEPTH)/third_party/npapi/npapi.gyp:npapi', + '<(DEPTH)/ui/ui.gyp:ui', '<(DEPTH)/v8/tools/gyp/v8.gyp:v8', 'webkit_resources', 'webkit_strings', diff --git a/webkit/plugins/ppapi/DEPS b/webkit/plugins/ppapi/DEPS index ffafa6f..a2b6af6 100644 --- a/webkit/plugins/ppapi/DEPS +++ b/webkit/plugins/ppapi/DEPS @@ -4,6 +4,7 @@ include_rules = [ "+ppapi/thunk", "+printing", "+media/video", + "+ui/base/ime", # This should technically not be allowed. Brett is refactoring this and will # move this file to a more proper shared location in a future iteration. diff --git a/webkit/plugins/ppapi/mock_plugin_delegate.cc b/webkit/plugins/ppapi/mock_plugin_delegate.cc index 09cc038..8aeacdd 100644 --- a/webkit/plugins/ppapi/mock_plugin_delegate.cc +++ b/webkit/plugins/ppapi/mock_plugin_delegate.cc @@ -18,7 +18,15 @@ MockPluginDelegate::MockPluginDelegate() { MockPluginDelegate::~MockPluginDelegate() { } -void MockPluginDelegate::PluginFocusChanged(bool focused) { +void MockPluginDelegate::PluginFocusChanged(PluginInstance* instance, + bool focused) { +} + +void MockPluginDelegate::PluginTextInputTypeChanged(PluginInstance* instance) { +} + +void MockPluginDelegate::PluginRequestedCancelComposition( + PluginInstance* instance) { } void MockPluginDelegate::PluginCrashed(PluginInstance* instance) { diff --git a/webkit/plugins/ppapi/mock_plugin_delegate.h b/webkit/plugins/ppapi/mock_plugin_delegate.h index 29c989b..bef5340 100644 --- a/webkit/plugins/ppapi/mock_plugin_delegate.h +++ b/webkit/plugins/ppapi/mock_plugin_delegate.h @@ -15,7 +15,9 @@ class MockPluginDelegate : public PluginDelegate { MockPluginDelegate(); virtual ~MockPluginDelegate(); - virtual void PluginFocusChanged(bool focused); + virtual void PluginFocusChanged(PluginInstance* instance, bool focused); + virtual void PluginTextInputTypeChanged(PluginInstance* instance); + virtual void PluginRequestedCancelComposition(PluginInstance* instance); virtual void PluginCrashed(PluginInstance* instance); virtual void InstanceCreated(PluginInstance* instance); virtual void InstanceDeleted(PluginInstance* instance); diff --git a/webkit/plugins/ppapi/plugin_delegate.h b/webkit/plugins/ppapi/plugin_delegate.h index 0cbeabf..3200431 100644 --- a/webkit/plugins/ppapi/plugin_delegate.h +++ b/webkit/plugins/ppapi/plugin_delegate.h @@ -246,7 +246,14 @@ class PluginDelegate { }; // Notification that the given plugin is focused or unfocused. - virtual void PluginFocusChanged(bool focused) = 0; + virtual void PluginFocusChanged(webkit::ppapi::PluginInstance* instance, + bool focused) = 0; + // Notification that the text input status of the given plugin is changed. + virtual void PluginTextInputTypeChanged( + webkit::ppapi::PluginInstance* instance) = 0; + // Notification that the plugin requested to cancel the current composition. + virtual void PluginRequestedCancelComposition( + webkit::ppapi::PluginInstance* instance) = 0; // Notification that the given plugin has crashed. When a plugin crashes, all // instances associated with that plugin will notify that they've crashed via diff --git a/webkit/plugins/ppapi/ppapi_plugin_instance.cc b/webkit/plugins/ppapi/ppapi_plugin_instance.cc index 9824c67..34717e1 100644 --- a/webkit/plugins/ppapi/ppapi_plugin_instance.cc +++ b/webkit/plugins/ppapi/ppapi_plugin_instance.cc @@ -10,6 +10,7 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" #include "base/metrics/histogram.h" +#include "base/utf_offset_string_conversions.h" #include "base/utf_string_conversions.h" #include "ppapi/c/dev/ppb_console_dev.h" #include "ppapi/c/dev/ppb_find_dev.h" @@ -34,6 +35,7 @@ #include "ppapi/c/private/ppp_instance_private.h" #include "ppapi/shared_impl/input_event_impl.h" #include "ppapi/shared_impl/resource.h" +#include "ppapi/shared_impl/time_conversion.h" #include "ppapi/shared_impl/url_util_impl.h" #include "ppapi/shared_impl/var.h" #include "ppapi/thunk/enter.h" @@ -134,6 +136,20 @@ typedef bool (*RenderPDFPageToDCProc)( namespace { +#if !defined(TOUCH_UI) +// The default text input type is to regard the plugin always accept text input. +// This is for allowing users to use input methods even on completely-IME- +// unaware plugins (e.g., PPAPI Flash or PDF plugin for M16). +// Plugins need to explicitly opt out the text input mode if they know +// that they don't accept texts. +const ui::TextInputType kPluginDefaultTextInputType = ui::TEXT_INPUT_TYPE_TEXT; +#else +// On the other hand, for touch ui, accepting text input implies to pop up +// virtual keyboard always. It makes IME-unaware plugins almost unusable, +// and hence is disabled by default (codereview.chromium.org/7800044). +const ui::TextInputType kPluginDefaultTextInputType = ui::TEXT_INPUT_TYPE_NONE; +#endif + #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, np_name) \ COMPILE_ASSERT(static_cast<int>(WebCursorInfo::webkit_name) \ == static_cast<int>(np_name), \ @@ -266,6 +282,10 @@ PluginInstance::PluginInstance( sad_plugin_(NULL), input_event_mask_(0), filtered_input_event_mask_(0), + text_input_type_(kPluginDefaultTextInputType), + text_input_caret_(0, 0, 0, 0), + text_input_caret_bounds_(0, 0, 0, 0), + text_input_caret_set_(false), lock_mouse_callback_(PP_BlockUntilComplete()) { pp_instance_ = ResourceTracker::Get()->AddInstance(this); @@ -475,6 +495,148 @@ bool PluginInstance::HandleDocumentLoad(PPB_URLLoader_Impl* loader) { pp_instance(), loader->pp_resource())); } +bool PluginInstance::SendCompositionEventToPlugin(PP_InputEvent_Type type, + const string16& text) { + std::vector<WebKit::WebCompositionUnderline> empty; + return SendCompositionEventWithUnderlineInformationToPlugin( + type, text, empty, static_cast<int>(text.size()), + static_cast<int>(text.size())); +} + +bool PluginInstance::SendCompositionEventWithUnderlineInformationToPlugin( + PP_InputEvent_Type type, + const string16& text, + const std::vector<WebKit::WebCompositionUnderline>& underlines, + int selection_start, + int selection_end) { + // Keep a reference on the stack. See NOTE above. + scoped_refptr<PluginInstance> ref(this); + + if (!LoadInputEventInterface()) + return false; + + PP_InputEvent_Class event_class = PP_INPUTEVENT_CLASS_IME; + if (!(filtered_input_event_mask_ & event_class) && + !(input_event_mask_ & event_class)) + return false; + + ::ppapi::InputEventData event; + event.event_type = type; + event.event_time_stamp = ::ppapi::TimeTicksToPPTimeTicks( + base::TimeTicks::Now()); + + // Convert UTF16 text to UTF8 with offset conversion. + std::vector<size_t> utf16_offsets; + utf16_offsets.push_back(selection_start); + utf16_offsets.push_back(selection_end); + for (size_t i = 0; i < underlines.size(); ++i) { + utf16_offsets.push_back(underlines[i].startOffset); + utf16_offsets.push_back(underlines[i].endOffset); + } + std::vector<size_t> utf8_offsets(utf16_offsets); + event.character_text = UTF16ToUTF8AndAdjustOffsets(text, &utf8_offsets); + + // Set the converted selection range. + event.composition_selection_start = (utf8_offsets[0] == std::string::npos ? + event.character_text.size() : utf8_offsets[0]); + event.composition_selection_end = (utf8_offsets[1] == std::string::npos ? + event.character_text.size() : utf8_offsets[1]); + + // Set the converted segmentation points. + // Be sure to add 0 and size(), and remove duplication or errors. + std::set<size_t> offset_set(utf8_offsets.begin()+2, utf8_offsets.end()); + offset_set.insert(0); + offset_set.insert(event.character_text.size()); + offset_set.erase(std::string::npos); + event.composition_segment_offsets.assign(offset_set.begin(), + offset_set.end()); + + // Set the composition target. + for (size_t i = 0; i < underlines.size(); ++i) { + if (underlines[i].thick) { + std::vector<uint32_t>::iterator it = + std::find(event.composition_segment_offsets.begin(), + event.composition_segment_offsets.end(), + utf8_offsets[2*i+2]); + if (it != event.composition_segment_offsets.end()) { + event.composition_target_segment = + it - event.composition_segment_offsets.begin(); + break; + } + } + } + + // Send the event. + bool handled = false; + if (filtered_input_event_mask_ & event_class) + event.is_filtered = true; + else + handled = true; // Unfiltered events are assumed to be handled. + scoped_refptr<InputEventImpl> event_resource( + new InputEventImpl(InputEventImpl::InitAsImpl(), + pp_instance(), event)); + handled |= PP_ToBool(plugin_input_event_interface_->HandleInputEvent( + pp_instance(), event_resource->pp_resource())); + return handled; +} + +bool PluginInstance::HandleCompositionStart(const string16& text) { + return SendCompositionEventToPlugin(PP_INPUTEVENT_TYPE_IME_COMPOSITION_START, + text); +} + +bool PluginInstance::HandleCompositionUpdate( + const string16& text, + const std::vector<WebKit::WebCompositionUnderline>& underlines, + int selection_start, + int selection_end) { + return SendCompositionEventWithUnderlineInformationToPlugin( + PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE, + text, underlines, selection_start, selection_end); +} + +bool PluginInstance::HandleCompositionEnd(const string16& text) { + return SendCompositionEventToPlugin(PP_INPUTEVENT_TYPE_IME_COMPOSITION_END, + text); +} + +bool PluginInstance::HandleTextInput(const string16& text) { + return SendCompositionEventToPlugin(PP_INPUTEVENT_TYPE_IME_TEXT, + text); +} + +void PluginInstance::UpdateCaretPosition(const gfx::Rect& caret, + const gfx::Rect& bounding_box) { + text_input_caret_ = caret; + text_input_caret_bounds_ = bounding_box; + text_input_caret_set_ = true; +} + +void PluginInstance::SetTextInputType(ui::TextInputType type) { + text_input_type_ = type; +} + +bool PluginInstance::IsPluginAcceptingCompositionEvents() const { + return (filtered_input_event_mask_ & PP_INPUTEVENT_CLASS_IME) || + (input_event_mask_ & PP_INPUTEVENT_CLASS_IME); +} + +gfx::Rect PluginInstance::GetCaretBounds() const { + if (!text_input_caret_set_) { + // If it is never set by the plugin, use the bottom left corner. + return gfx::Rect(position().x(), position().y()+position().height(), 0, 0); + } + + // TODO(kinaba) Take CSS transformation into accont. + // TODO(kinaba) Take bounding_box into account. On some platforms, an + // "exclude rectangle" where candidate window must avoid the region can be + // passed to IME. Currently, we pass only the caret rectangle because + // it is the only information supported uniformly in Chromium. + gfx::Rect caret(text_input_caret_); + caret.Offset(position().origin()); + return caret; +} + bool PluginInstance::HandleInputEvent(const WebKit::WebInputEvent& event, WebCursorInfo* cursor_info) { TRACE_EVENT0("ppapi", "PluginInstance::HandleInputEvent"); @@ -583,7 +745,7 @@ void PluginInstance::SetWebKitFocus(bool has_focus) { bool old_plugin_focus = PluginHasFocus(); has_webkit_focus_ = has_focus; if (PluginHasFocus() != old_plugin_focus) { - delegate()->PluginFocusChanged(PluginHasFocus()); + delegate()->PluginFocusChanged(this, PluginHasFocus()); instance_interface_->DidChangeFocus(pp_instance(), PP_FromBool(PluginHasFocus())); } diff --git a/webkit/plugins/ppapi/ppapi_plugin_instance.h b/webkit/plugins/ppapi/ppapi_plugin_instance.h index a7356b6..00fbfab 100644 --- a/webkit/plugins/ppapi/ppapi_plugin_instance.h +++ b/webkit/plugins/ppapi/ppapi_plugin_instance.h @@ -22,6 +22,7 @@ #include "ppapi/c/pp_instance.h" #include "ppapi/c/pp_resource.h" #include "ppapi/c/pp_var.h" +#include "ppapi/c/ppb_input_event.h" #include "ppapi/c/ppp_graphics_3d.h" #include "ppapi/c/ppp_instance.h" #include "ppapi/shared_impl/function_group_base.h" @@ -31,6 +32,7 @@ #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkRefCnt.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" +#include "ui/base/ime/text_input_type.h" #include "ui/gfx/rect.h" #include "webkit/plugins/ppapi/plugin_delegate.h" @@ -48,14 +50,11 @@ struct PPP_Zoom_Dev; class SkBitmap; class TransportDIB; -namespace gfx { -class Rect; -} - namespace WebKit { -struct WebCursorInfo; class WebInputEvent; class WebPluginContainer; +struct WebCompositionUnderline; +struct WebCursorInfo; } namespace ppapi { @@ -167,6 +166,26 @@ class PluginInstance : public base::RefCounted<PluginInstance>, PP_Var GetInstanceObject(); void ViewChanged(const gfx::Rect& position, const gfx::Rect& clip); + // Handlers for composition events. + bool HandleCompositionStart(const string16& text); + bool HandleCompositionUpdate( + const string16& text, + const std::vector<WebKit::WebCompositionUnderline>& underlines, + int selection_start, + int selection_end); + bool HandleCompositionEnd(const string16& text); + bool HandleTextInput(const string16& text); + + // Implementation of composition API. + void UpdateCaretPosition(const gfx::Rect& caret, + const gfx::Rect& bounding_box); + void SetTextInputType(ui::TextInputType type); + + // Gets the current text input status. + ui::TextInputType text_input_type() const { return text_input_type_; } + gfx::Rect GetCaretBounds() const; + bool IsPluginAcceptingCompositionEvents() const; + // Notifications about focus changes, see has_webkit_focus_ below. void SetWebKitFocus(bool has_focus); void SetContentAreaFocus(bool has_focus); @@ -412,6 +431,17 @@ class PluginInstance : public base::RefCounted<PluginInstance>, void DoSetCursor(WebKit::WebCursorInfo* cursor); + // Internal helper functions for HandleCompositionXXX(). + bool SendCompositionEventToPlugin( + PP_InputEvent_Type type, + const string16& text); + bool SendCompositionEventWithUnderlineInformationToPlugin( + PP_InputEvent_Type type, + const string16& text, + const std::vector<WebKit::WebCompositionUnderline>& underlines, + int selection_start, + int selection_end); + // Checks if the security origin of the document containing this instance can // assess the security origin of the main frame document. bool CanAccessMainFrame() const; @@ -555,6 +585,12 @@ class PluginInstance : public base::RefCounted<PluginInstance>, uint32_t input_event_mask_; uint32_t filtered_input_event_mask_; + // Text composition status. + ui::TextInputType text_input_type_; + gfx::Rect text_input_caret_; + gfx::Rect text_input_caret_bounds_; + bool text_input_caret_set_; + PP_CompletionCallback lock_mouse_callback_; DISALLOW_COPY_AND_ASSIGN(PluginInstance); diff --git a/webkit/plugins/ppapi/ppb_text_input_impl.cc b/webkit/plugins/ppapi/ppb_text_input_impl.cc index fc02925..9a1cc92 100644 --- a/webkit/plugins/ppapi/ppb_text_input_impl.cc +++ b/webkit/plugins/ppapi/ppb_text_input_impl.cc @@ -5,7 +5,8 @@ #include "webkit/plugins/ppapi/ppb_text_input_impl.h" #include "base/logging.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextInputType.h" +#include "ui/base/ime/text_input_type.h" +#include "ui/gfx/rect.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" namespace webkit { @@ -20,39 +21,44 @@ PPB_TextInput_Impl::AsPPB_TextInput_FunctionAPI() { return this; } -COMPILE_ASSERT(int(WebKit::WebTextInputTypeNone) == \ +// Check PP_TextInput_Type and ui::TextInputType are kept in sync. +COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_NONE) == \ int(PP_TEXTINPUT_TYPE_NONE), mismatching_enums); -COMPILE_ASSERT(int(WebKit::WebTextInputTypeText) == \ +COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_TEXT) == \ int(PP_TEXTINPUT_TYPE_TEXT), mismatching_enums); -COMPILE_ASSERT(int(WebKit::WebTextInputTypePassword) == \ +COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_PASSWORD) == \ int(PP_TEXTINPUT_TYPE_PASSWORD), mismatching_enums); -COMPILE_ASSERT(int(WebKit::WebTextInputTypeSearch) == \ +COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_SEARCH) == \ int(PP_TEXTINPUT_TYPE_SEARCH), mismatching_enums); -COMPILE_ASSERT(int(WebKit::WebTextInputTypeEmail) == \ +COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_EMAIL) == \ int(PP_TEXTINPUT_TYPE_EMAIL), mismatching_enums); -COMPILE_ASSERT(int(WebKit::WebTextInputTypeNumber) == \ +COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_NUMBER) == \ int(PP_TEXTINPUT_TYPE_NUMBER), mismatching_enums); -COMPILE_ASSERT(int(WebKit::WebTextInputTypeTelephone) == \ +COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_TELEPHONE) == \ int(PP_TEXTINPUT_TYPE_TELEPHONE), mismatching_enums); -COMPILE_ASSERT(int(WebKit::WebTextInputTypeURL) == \ +COMPILE_ASSERT(int(ui::TEXT_INPUT_TYPE_URL) == \ int(PP_TEXTINPUT_TYPE_URL), mismatching_enums); void PPB_TextInput_Impl::SetTextInputType(PP_Instance instance, PP_TextInput_Type type) { - // TODO(kinaba) the implementation is split to another CL for reviewing. - NOTIMPLEMENTED(); + int itype = type; + if (itype < 0 || itype > ui::TEXT_INPUT_TYPE_URL) + itype = ui::TEXT_INPUT_TYPE_NONE; + instance_->SetTextInputType(static_cast<ui::TextInputType>(itype)); } void PPB_TextInput_Impl::UpdateCaretPosition(PP_Instance instance, const PP_Rect& caret, const PP_Rect& bounding_box) { - // TODO(kinaba) the implementation is split to another CL for reviewing. - NOTIMPLEMENTED(); + instance_->UpdateCaretPosition( + gfx::Rect(caret.point.x, caret.point.y, + caret.size.width, caret.size.height), + gfx::Rect(bounding_box.point.x, bounding_box.point.y, + bounding_box.size.width, bounding_box.size.height)); } void PPB_TextInput_Impl::CancelCompositionText(PP_Instance instance) { - // TODO(kinaba) the implementation is split to another CL for reviewing. - NOTIMPLEMENTED(); + instance_->delegate()->PluginRequestedCancelComposition(instance_); } } // namespace ppapi |