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 #ifndef CHROME_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ |
| 6 #define CHROME_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ |
| 7 |
| 8 #import <Cocoa/Cocoa.h> |
| 9 |
| 10 #include "base/memory/scoped_nsobject.h" |
| 11 #include "base/synchronization/condition_variable.h" |
| 12 #include "base/synchronization/lock.h" |
| 13 #include "ui/gfx/point.h" |
| 14 |
| 15 template <typename T> struct DefaultSingletonTraits; |
| 16 |
| 17 class RenderWidgetHost; |
| 18 |
| 19 // This class helps with the Mac OS X dictionary popup. For the design overview, |
| 20 // look at this document: |
| 21 // http://dev.chromium.org/developers/design-documents/system-dictionary-pop-u
p-architecture |
| 22 // |
| 23 // This service is used to marshall information for these three methods that are |
| 24 // implemented in RenderWidgetHostViewMac: |
| 25 // -[NSTextInput characterIndexForPoint:] |
| 26 // -[NSTextInput attributedSubstringFromRange:] |
| 27 // -[NSTextInput firstRectForCharacterRange:] |
| 28 // |
| 29 // Because these methods are part of a synchronous system API, implementing them |
| 30 // requires getting information from the renderer synchronously. Rather than |
| 31 // using an actual sync IPC message, a normal async ViewMsg is used with a lock |
| 32 // and condition (managed by this service). |
| 33 class TextInputClientMac { |
| 34 public: |
| 35 // Returns the singleton instance. |
| 36 static TextInputClientMac* GetInstance(); |
| 37 |
| 38 // Each of the three methods mentioned above has an associated pair of methods |
| 39 // to get data from the renderer. The Get*() methods block the calling thread |
| 40 // (always the UI thread) with a short timeout after the async message has |
| 41 // been sent to the renderer to lookup the information needed to respond to |
| 42 // the system. The Set*AndSignal() methods store the looked up information in |
| 43 // this service and signal the condition to allow the Get*() methods to |
| 44 // unlock and return that stored value. |
| 45 // |
| 46 // Returns NSNotFound if the request times out or is not completed. |
| 47 NSUInteger GetCharacterIndexAtPoint(RenderWidgetHost* rwh, gfx::Point point); |
| 48 // Returns nil if the request times out or is completed. |
| 49 NSAttributedString* GetAttributedSubstringFromRange(RenderWidgetHost* rwh, |
| 50 NSRange range); |
| 51 // Returns NSZeroRect if the request times out or is not completed. The result |
| 52 // is in WebKit coordinates. |
| 53 NSRect GetFirstRectForRange(RenderWidgetHost* rwh, NSRange range); |
| 54 |
| 55 // When the renderer sends the ViewHostMsg reply, the RenderMessageFilter will |
| 56 // call the corresponding method on the IO thread to unlock the condition and |
| 57 // allow the Get*() methods to continue/return. |
| 58 void SetCharacterIndexAndSignal(NSUInteger index); |
| 59 void SetFirstRectAndSignal(NSRect first_rect); |
| 60 void SetSubstringAndSignal(NSAttributedString* string); |
| 61 |
| 62 private: |
| 63 friend struct DefaultSingletonTraits<TextInputClientMac>; |
| 64 TextInputClientMac(); |
| 65 virtual ~TextInputClientMac(); |
| 66 |
| 67 // The critical sections that the Condition guards are in Get*() methods. |
| 68 // These methods lock the internal condition for use before the asynchronous |
| 69 // message is sent to the renderer to lookup the required information. These |
| 70 // are only used on the UI thread. |
| 71 void BeforeRequest(); |
| 72 // Called at the end of a critical section. This will release the lock and |
| 73 // condition. |
| 74 void AfterRequest(); |
| 75 |
| 76 NSUInteger character_index_; |
| 77 NSRect first_rect_; |
| 78 scoped_nsobject<NSAttributedString> substring_; |
| 79 |
| 80 base::Lock lock_; |
| 81 base::ConditionVariable condition_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(TextInputClientMac); |
| 84 }; |
| 85 |
| 86 #endif // CHROME_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ |
OLD | NEW |