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/metrics/histogram.h" |
| 8 #include "base/singleton.h" |
| 9 #include "base/time.h" |
| 10 #include "chrome/browser/renderer_host/render_widget_host.h" |
| 11 #include "chrome/common/text_input_client_messages.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 range.location, |
| 54 range.length)); |
| 55 condition_.TimedWait(base::TimeDelta::FromMilliseconds(kWaitTimeout)); |
| 56 AfterRequest(); |
| 57 |
| 58 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 59 UMA_HISTOGRAM_TIMES("TextInputClientMac.FirstRect", |
| 60 delta * base::Time::kMicrosecondsPerMillisecond); |
| 61 |
| 62 return first_rect_; |
| 63 } |
| 64 |
| 65 NSAttributedString* TextInputClientMac::GetAttributedSubstringFromRange( |
| 66 RenderWidgetHost* rwh, |
| 67 NSRange range) { |
| 68 base::TimeTicks start = base::TimeTicks::Now(); |
| 69 |
| 70 BeforeRequest(); |
| 71 rwh->Send(new TextInputClientMsg_StringForRange(rwh->routing_id(), |
| 72 range.location, |
| 73 range.length)); |
| 74 condition_.TimedWait(base::TimeDelta::FromMilliseconds(kWaitTimeout)); |
| 75 AfterRequest(); |
| 76 |
| 77 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 78 UMA_HISTOGRAM_TIMES("TextInputClientMac.Substring", |
| 79 delta * base::Time::kMicrosecondsPerMillisecond); |
| 80 |
| 81 return substring_.get(); |
| 82 } |
| 83 |
| 84 void TextInputClientMac::SetCharacterIndexAndSignal(NSUInteger index) { |
| 85 lock_.Acquire(); |
| 86 character_index_ = index; |
| 87 lock_.Release(); |
| 88 condition_.Signal(); |
| 89 } |
| 90 |
| 91 void TextInputClientMac::SetFirstRectAndSignal(NSRect first_rect) { |
| 92 lock_.Acquire(); |
| 93 first_rect_ = first_rect; |
| 94 lock_.Release(); |
| 95 condition_.Signal(); |
| 96 } |
| 97 |
| 98 void TextInputClientMac::SetSubstringAndSignal(NSAttributedString* string) { |
| 99 lock_.Acquire(); |
| 100 substring_.reset([string copy]); |
| 101 lock_.Release(); |
| 102 condition_.Signal(); |
| 103 } |
| 104 |
| 105 void TextInputClientMac::BeforeRequest() { |
| 106 lock_.Acquire(); |
| 107 character_index_ = NSNotFound; |
| 108 first_rect_ = NSZeroRect; |
| 109 substring_.reset(); |
| 110 } |
| 111 |
| 112 void TextInputClientMac::AfterRequest() { |
| 113 lock_.Release(); |
| 114 } |
OLD | NEW |