OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 #include "chrome/browser/instant/instant_controller_utils.h" |
| 6 |
| 7 #include "chrome/browser/platform_util.h" |
| 8 #include "content/public/browser/render_widget_host_view.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/browser/web_contents_view.h" |
| 11 #include "third_party/icu/public/common/unicode/normalizer2.h" |
| 12 |
| 13 #if defined(TOOLKIT_VIEWS) |
| 14 #include "ui/views/widget/widget.h" |
| 15 #endif |
| 16 |
| 17 namespace { |
| 18 |
| 19 string16 Normalize(const string16& str) { |
| 20 UErrorCode status = U_ZERO_ERROR; |
| 21 const icu::Normalizer2* normalizer = |
| 22 icu::Normalizer2::getInstance(NULL, "nfkc_cf", UNORM2_COMPOSE, status); |
| 23 if (normalizer == NULL || U_FAILURE(status)) |
| 24 return str; |
| 25 icu::UnicodeString norm_str(normalizer->normalize( |
| 26 icu::UnicodeString(FALSE, str.c_str(), str.size()), status)); |
| 27 if (U_FAILURE(status)) |
| 28 return str; |
| 29 return string16(norm_str.getBuffer(), norm_str.length()); |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 // static |
| 35 gfx::NativeView InstantControllerUtils::GetViewGainingFocus( |
| 36 gfx::NativeView view_gaining_focus) { |
| 37 #if defined(TOOLKIT_VIEWS) |
| 38 // For TOOLKIT_VIEWS, the top level widget is always focused. If the focus |
| 39 // change originated in views determine the child Widget from the view that is |
| 40 // being focused. |
| 41 views::Widget* widget = view_gaining_focus ? |
| 42 views::Widget::GetWidgetForNativeView(view_gaining_focus) : NULL; |
| 43 if (widget) { |
| 44 views::FocusManager* focus_manager = widget->GetFocusManager(); |
| 45 if (focus_manager && focus_manager->is_changing_focus() && |
| 46 focus_manager->GetFocusedView() && |
| 47 focus_manager->GetFocusedView()->GetWidget()) |
| 48 return focus_manager->GetFocusedView()->GetWidget()->GetNativeView(); |
| 49 } |
| 50 #endif |
| 51 return view_gaining_focus; |
| 52 } |
| 53 |
| 54 // static |
| 55 bool InstantControllerUtils::IsViewInContents(gfx::NativeView view, |
| 56 content::WebContents* contents) { |
| 57 content::RenderWidgetHostView* rwhv = contents->GetRenderWidgetHostView(); |
| 58 if (!view || !rwhv) |
| 59 return false; |
| 60 |
| 61 gfx::NativeView tab_view = contents->GetView()->GetNativeView(); |
| 62 if (view == rwhv->GetNativeView() || view == tab_view) |
| 63 return true; |
| 64 |
| 65 // Walk up the view hierarchy to determine if the view is a subview of the |
| 66 // WebContents view (such as a windowed plugin or http auth dialog). |
| 67 while (view) { |
| 68 view = platform_util::GetParent(view); |
| 69 if (view == tab_view) |
| 70 return true; |
| 71 } |
| 72 |
| 73 return false; |
| 74 } |
| 75 |
| 76 // static |
| 77 bool InstantControllerUtils::NormalizeAndStripPrefix(string16* text, |
| 78 const string16& prefix) { |
| 79 string16 norm_prefix = Normalize(prefix); |
| 80 string16 norm_text = Normalize(*text); |
| 81 if (norm_prefix.size() <= norm_text.size() && |
| 82 norm_text.compare(0, norm_prefix.size(), norm_prefix) == 0) { |
| 83 *text = norm_text.erase(0, norm_prefix.size()); |
| 84 return true; |
| 85 } |
| 86 return false; |
| 87 } |
| 88 |
| 89 // static |
| 90 std::string InstantControllerUtils::CommitTypeToString(InstantCommitType type) { |
| 91 switch (type) { |
| 92 case INSTANT_COMMIT_PRESSED_ENTER: return "enter"; |
| 93 case INSTANT_COMMIT_PRESSED_ALT_ENTER: return "alt-enter"; |
| 94 case INSTANT_COMMIT_FOCUS_LOST: return "focus-lost"; |
| 95 case INSTANT_COMMIT_NAVIGATED: return "navigated"; |
| 96 } |
| 97 |
| 98 NOTREACHED(); |
| 99 return std::string(); |
| 100 } |
| 101 |
| 102 // static |
| 103 std::string InstantControllerUtils::FocusStateToString( |
| 104 OmniboxFocusState state) { |
| 105 switch (state) { |
| 106 case OMNIBOX_FOCUS_NONE: return "none"; |
| 107 case OMNIBOX_FOCUS_VISIBLE: return "visible"; |
| 108 case OMNIBOX_FOCUS_INVISIBLE: return "invisible"; |
| 109 } |
| 110 |
| 111 NOTREACHED(); |
| 112 return std::string(); |
| 113 } |
| 114 |
| 115 // static |
| 116 std::string InstantControllerUtils::FocusChangeReasonToString( |
| 117 OmniboxFocusChangeReason reason) { |
| 118 switch (reason) { |
| 119 case OMNIBOX_FOCUS_CHANGE_EXPLICIT: return "explicit"; |
| 120 case OMNIBOX_FOCUS_CHANGE_TAB_SWITCH: return "tab-switch"; |
| 121 case OMNIBOX_FOCUS_CHANGE_TYPING: return "typing"; |
| 122 } |
| 123 |
| 124 NOTREACHED(); |
| 125 return std::string(); |
| 126 } |
OLD | NEW |