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 TextInputClientMac::~TextInputClientMac() { |
| 26 } |
| 27 |
| 28 // static |
| 29 TextInputClientMac* TextInputClientMac::GetInstance() { |
| 30 return Singleton<TextInputClientMac>::get(); |
| 31 } |
| 32 |
| 33 NSUInteger TextInputClientMac::GetCharacterIndexAtPoint(RenderWidgetHost* rwh, |
| 34 gfx::Point point) { |
| 35 base::TimeTicks start = base::TimeTicks::Now(); |
| 36 |
| 37 BeforeRequest(); |
| 38 rwh->Send(new TextInputClientMsg_CharacterIndexForPoint(rwh->routing_id(), |
| 39 point)); |
| 40 condition_.TimedWait(base::TimeDelta::FromMilliseconds(kWaitTimeout)); |
| 41 AfterRequest(); |
| 42 |
| 43 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 44 UMA_HISTOGRAM_TIMES("TextInputClient.CharacterIndex", |
| 45 delta * base::Time::kMicrosecondsPerMillisecond); |
| 46 |
| 47 return character_index_; |
| 48 } |
| 49 |
| 50 NSRect TextInputClientMac::GetFirstRectForRange(RenderWidgetHost* rwh, |
| 51 NSRange range) { |
| 52 base::TimeTicks start = base::TimeTicks::Now(); |
| 53 |
| 54 BeforeRequest(); |
| 55 rwh->Send(new TextInputClientMsg_FirstRectForCharacterRange(rwh->routing_id(), |
| 56 ui::Range(range))); |
| 57 condition_.TimedWait(base::TimeDelta::FromMilliseconds(kWaitTimeout)); |
| 58 AfterRequest(); |
| 59 |
| 60 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 61 UMA_HISTOGRAM_TIMES("TextInputClient.FirstRect", |
| 62 delta * base::Time::kMicrosecondsPerMillisecond); |
| 63 |
| 64 return first_rect_; |
| 65 } |
| 66 |
| 67 NSAttributedString* TextInputClientMac::GetAttributedSubstringFromRange( |
| 68 RenderWidgetHost* rwh, |
| 69 NSRange range) { |
| 70 base::TimeTicks start = base::TimeTicks::Now(); |
| 71 |
| 72 BeforeRequest(); |
| 73 rwh->Send(new TextInputClientMsg_StringForRange(rwh->routing_id(), |
| 74 ui::Range(range))); |
| 75 condition_.TimedWait(base::TimeDelta::FromMilliseconds(kWaitTimeout)); |
| 76 AfterRequest(); |
| 77 |
| 78 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 79 UMA_HISTOGRAM_TIMES("TextInputClient.Substring", |
| 80 delta * base::Time::kMicrosecondsPerMillisecond); |
| 81 |
| 82 return substring_.get(); |
| 83 } |
| 84 |
| 85 void TextInputClientMac::SetCharacterIndexAndSignal(NSUInteger index) { |
| 86 lock_.Acquire(); |
| 87 character_index_ = index; |
| 88 lock_.Release(); |
| 89 condition_.Signal(); |
| 90 } |
| 91 |
| 92 void TextInputClientMac::SetFirstRectAndSignal(NSRect first_rect) { |
| 93 lock_.Acquire(); |
| 94 first_rect_ = first_rect; |
| 95 lock_.Release(); |
| 96 condition_.Signal(); |
| 97 } |
| 98 |
| 99 void TextInputClientMac::SetSubstringAndSignal(NSAttributedString* string) { |
| 100 lock_.Acquire(); |
| 101 substring_.reset([string copy]); |
| 102 lock_.Release(); |
| 103 condition_.Signal(); |
| 104 } |
| 105 |
| 106 void TextInputClientMac::BeforeRequest() { |
| 107 base::TimeTicks start = base::TimeTicks::Now(); |
| 108 |
| 109 lock_.Acquire(); |
| 110 |
| 111 base::TimeDelta delta(base::TimeTicks::Now() - start); |
| 112 UMA_HISTOGRAM_TIMES("TextInputClient.LockWait", |
| 113 delta * base::Time::kMicrosecondsPerMillisecond); |
| 114 |
| 115 character_index_ = NSNotFound; |
| 116 first_rect_ = NSZeroRect; |
| 117 substring_.reset(); |
| 118 } |
| 119 |
| 120 void TextInputClientMac::AfterRequest() { |
| 121 lock_.Release(); |
| 122 } |
OLD | NEW |