OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #import "chrome/browser/renderer_host/text_input_client_mac.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "base/time.h" |
| 10 #include "chrome/common/text_input_client_messages.h" |
| 11 #include "content/browser/renderer_host/render_widget_host.h" |
| 12 |
| 13 // The amount of time in milliseconds that the browser process will wait for a |
| 14 // response from the renderer. |
| 15 // TODO(rsesek): Using the histogram data, find the best upper-bound for this |
| 16 // value. |
| 17 const float kWaitTimeout = 1500; |
| 18 |
| 19 TextInputClientMac::TextInputClientMac() |
| 20 : character_index_(NSNotFound), |
| 21 lock_(), |
| 22 condition_(&lock_) { |
| 23 } |
| 24 |
| 25 // static |
| 26 TextInputClientMac* TextInputClientMac::GetInstance() { |
| 27 return Singleton<TextInputClientMac>::get(); |
| 28 } |
| 29 |
| 30 NSUInteger TextInputClientMac::GetCharacterIndexAtPoint(RenderWidgetHost* rwh, |
| 31 gfx::Point point) { |
| 32 base::TimeTicks start = base::TimeTicks::Now(); |
| 33 |
| 34 BeforeRequest(); |
| 35 rwh->Send(new TextInputClientMsg_CharacterIndexForPoint(rwh->routing_id(), |
| 36 point)); |
| 37 condition_.TimedWait(base::TimeDelta::FromMilliseconds(kWaitTimeout)); |
| 38 AfterRequest(); |
| 39 |
| 40 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 41 UMA_HISTOGRAM_TIMES("TextInputClientMac.CharacterIndex", |
| 42 delta * base::Time::kMicrosecondsPerMillisecond); |
| 43 |
| 44 return character_index_; |
| 45 } |
| 46 |
| 47 NSRect TextInputClientMac::GetFirstRectForRange(RenderWidgetHost* rwh, |
| 48 NSRange range) { |
| 49 base::TimeTicks start = base::TimeTicks::Now(); |
| 50 |
| 51 BeforeRequest(); |
| 52 rwh->Send(new TextInputClientMsg_FirstRectForCharacterRange(rwh->routing_id(), |
| 53 ui::Range(range))); |
| 54 condition_.TimedWait(base::TimeDelta::FromMilliseconds(kWaitTimeout)); |
| 55 AfterRequest(); |
| 56 |
| 57 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 58 UMA_HISTOGRAM_TIMES("TextInputClientMac.FirstRect", |
| 59 delta * base::Time::kMicrosecondsPerMillisecond); |
| 60 |
| 61 return first_rect_; |
| 62 } |
| 63 |
| 64 NSAttributedString* TextInputClientMac::GetAttributedSubstringFromRange( |
| 65 RenderWidgetHost* rwh, |
| 66 NSRange range) { |
| 67 base::TimeTicks start = base::TimeTicks::Now(); |
| 68 |
| 69 BeforeRequest(); |
| 70 rwh->Send(new TextInputClientMsg_StringForRange(rwh->routing_id(), |
| 71 ui::Range(range))); |
| 72 condition_.TimedWait(base::TimeDelta::FromMilliseconds(kWaitTimeout)); |
| 73 AfterRequest(); |
| 74 |
| 75 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 76 UMA_HISTOGRAM_TIMES("TextInputClientMac.Substring", |
| 77 delta * base::Time::kMicrosecondsPerMillisecond); |
| 78 |
| 79 return substring_.get(); |
| 80 } |
| 81 |
| 82 void TextInputClientMac::SetCharacterIndexAndSignal(NSUInteger index) { |
| 83 lock_.Acquire(); |
| 84 character_index_ = index; |
| 85 lock_.Release(); |
| 86 condition_.Signal(); |
| 87 } |
| 88 |
| 89 void TextInputClientMac::SetFirstRectAndSignal(NSRect first_rect) { |
| 90 lock_.Acquire(); |
| 91 first_rect_ = first_rect; |
| 92 lock_.Release(); |
| 93 condition_.Signal(); |
| 94 } |
| 95 |
| 96 void TextInputClientMac::SetSubstringAndSignal(NSAttributedString* string) { |
| 97 lock_.Acquire(); |
| 98 substring_.reset([string copy]); |
| 99 lock_.Release(); |
| 100 condition_.Signal(); |
| 101 } |
| 102 |
| 103 void TextInputClientMac::BeforeRequest() { |
| 104 lock_.Acquire(); |
| 105 character_index_ = NSNotFound; |
| 106 first_rect_ = NSZeroRect; |
| 107 substring_.reset(); |
| 108 } |
| 109 |
| 110 void TextInputClientMac::AfterRequest() { |
| 111 lock_.Release(); |
| 112 } |
OLD | NEW |