Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ |
| 7 | 7 |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 | 9 |
| 10 #include "base/mac/scoped_block.h" | |
| 10 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/synchronization/condition_variable.h" | 12 #include "base/synchronization/condition_variable.h" |
| 12 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 13 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 14 #include "ui/gfx/point.h" | 15 #include "ui/gfx/point.h" |
| 15 | 16 |
| 16 template <typename T> struct DefaultSingletonTraits; | 17 template <typename T> struct DefaultSingletonTraits; |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 class RenderWidgetHost; | 20 class RenderWidgetHost; |
| 20 | 21 |
| 21 // This class helps with the Mac OS X dictionary popup. For the design overview, | 22 // This class helps with the Mac OS X dictionary popup. For the design overview, |
| 22 // look at this document: | 23 // look at this document: |
| 23 // http://dev.chromium.org/developers/design-documents/system-dictionary-pop-u p-architecture | 24 // http://dev.chromium.org/developers/design-documents/system-dictionary-pop-u p-architecture |
| 24 // | 25 // |
| 25 // This service is used to marshall information for these three methods that are | 26 // This service is used to marshall information for these three methods that are |
| 26 // implemented in RenderWidgetHostViewMac: | 27 // implemented in RenderWidgetHostViewMac: |
| 27 // -[NSTextInput characterIndexForPoint:] | 28 // -[NSTextInput characterIndexForPoint:] |
| 28 // -[NSTextInput attributedSubstringFromRange:] | 29 // -[NSTextInput attributedSubstringFromRange:] |
| 29 // -[NSTextInput firstRectForCharacterRange:] | 30 // -[NSTextInput firstRectForCharacterRange:] |
| 30 // | 31 // |
| 31 // Because these methods are part of a synchronous system API, implementing them | 32 // Because these methods are part of a synchronous system API, implementing them |
| 32 // requires getting information from the renderer synchronously. Rather than | 33 // requires getting information from the renderer synchronously. Rather than |
| 33 // using an actual sync IPC message, a normal async ViewMsg is used with a lock | 34 // using an actual sync IPC message, a normal async ViewMsg is used with a lock |
| 34 // and condition (managed by this service). | 35 // and condition (managed by this service). |
| 36 // | |
| 37 // Mac OS 10.8 introduced -[NSResponder quickLookWithEvent:]. | |
| 38 // We can use it to implement asynchronous dictionary lookup when the user | |
| 39 // taps a word using three fingers. | |
| 40 // But currently the "Look Up in Dictionary" context menu item still goes throug h | |
|
Avi (use Gerrit)
2014/03/12 17:10:54
80 col
Andre
2014/03/12 17:17:51
Done.
| |
| 41 // the above synchronous IPC. | |
| 35 class CONTENT_EXPORT TextInputClientMac { | 42 class CONTENT_EXPORT TextInputClientMac { |
| 36 public: | 43 public: |
| 37 // Returns the singleton instance. | 44 // Returns the singleton instance. |
| 38 static TextInputClientMac* GetInstance(); | 45 static TextInputClientMac* GetInstance(); |
| 39 | 46 |
| 40 // Each of the three methods mentioned above has an associated pair of methods | 47 // Each of the three methods mentioned above has an associated pair of methods |
| 41 // to get data from the renderer. The Get*() methods block the calling thread | 48 // to get data from the renderer. The Get*() methods block the calling thread |
| 42 // (always the UI thread) with a short timeout after the async message has | 49 // (always the UI thread) with a short timeout after the async message has |
| 43 // been sent to the renderer to lookup the information needed to respond to | 50 // been sent to the renderer to lookup the information needed to respond to |
| 44 // the system. The Set*AndSignal() methods store the looked up information in | 51 // the system. The Set*AndSignal() methods store the looked up information in |
| 45 // this service and signal the condition to allow the Get*() methods to | 52 // this service and signal the condition to allow the Get*() methods to |
| 46 // unlock and return that stored value. | 53 // unlock and return that stored value. |
| 47 // | 54 // |
| 48 // Returns NSNotFound if the request times out or is not completed. | 55 // Returns NSNotFound if the request times out or is not completed. |
| 49 NSUInteger GetCharacterIndexAtPoint(RenderWidgetHost* rwh, gfx::Point point); | 56 NSUInteger GetCharacterIndexAtPoint(RenderWidgetHost* rwh, gfx::Point point); |
| 50 // Returns nil if the request times out or is completed. | 57 // Returns nil if the request times out or is completed. |
| 51 NSAttributedString* GetAttributedSubstringFromRange( | 58 NSAttributedString* GetAttributedSubstringFromRange( |
| 52 RenderWidgetHost* rwh, NSRange range); | 59 RenderWidgetHost* rwh, NSRange range); |
| 53 // Returns NSZeroRect if the request times out or is not completed. The result | 60 // Returns NSZeroRect if the request times out or is not completed. The result |
| 54 // is in WebKit coordinates. | 61 // is in WebKit coordinates. |
| 55 NSRect GetFirstRectForRange(RenderWidgetHost* rwh, NSRange range); | 62 NSRect GetFirstRectForRange(RenderWidgetHost* rwh, NSRange range); |
| 56 | 63 |
| 57 // When the renderer sends the ViewHostMsg reply, the RenderMessageFilter will | 64 // When the renderer sends the ViewHostMsg reply, the RenderMessageFilter will |
| 58 // call the corresponding method on the IO thread to unlock the condition and | 65 // call the corresponding method on the IO thread to unlock the condition and |
| 59 // allow the Get*() methods to continue/return. | 66 // allow the Get*() methods to continue/return. |
| 60 void SetCharacterIndexAndSignal(NSUInteger index); | 67 void SetCharacterIndexAndSignal(NSUInteger index); |
| 61 void SetFirstRectAndSignal(NSRect first_rect); | 68 void SetFirstRectAndSignal(NSRect first_rect); |
| 62 void SetSubstringAndSignal(NSAttributedString* string); | 69 void SetSubstringAndSignal(NSAttributedString* string); |
| 63 | 70 |
| 71 // This async method is invoked from -quickLookWithEvent: (tap with 3 fingers) . | |
|
Avi (use Gerrit)
2014/03/12 17:10:54
80 col
Andre
2014/03/12 17:17:51
Done.
| |
| 72 // The reply callback will be invoked from the IO thread, the caller is | |
| 73 // responsible for bouncing to the main thread if necessary. | |
| 74 // The callback parameters provide the attributed word under the point and | |
| 75 // the lower left baseline point of the text. | |
| 76 void GetStringAtPoint(RenderWidgetHost* rwh, | |
| 77 gfx::Point point, | |
| 78 void (^replyHandler)(NSAttributedString*, NSPoint)); | |
| 79 // This is called on the IO thread when we get the renderer's reply for | |
| 80 // GetStringAtPoint. | |
| 81 void GetStringAtPointReply(NSAttributedString*, NSPoint); | |
| 82 | |
| 64 private: | 83 private: |
| 65 friend struct DefaultSingletonTraits<TextInputClientMac>; | 84 friend struct DefaultSingletonTraits<TextInputClientMac>; |
| 66 TextInputClientMac(); | 85 TextInputClientMac(); |
| 67 ~TextInputClientMac(); | 86 ~TextInputClientMac(); |
| 68 | 87 |
| 69 // The critical sections that the Condition guards are in Get*() methods. | 88 // The critical sections that the Condition guards are in Get*() methods. |
| 70 // These methods lock the internal condition for use before the asynchronous | 89 // These methods lock the internal condition for use before the asynchronous |
| 71 // message is sent to the renderer to lookup the required information. These | 90 // message is sent to the renderer to lookup the required information. These |
| 72 // are only used on the UI thread. | 91 // are only used on the UI thread. |
| 73 void BeforeRequest(); | 92 void BeforeRequest(); |
| 74 // Called at the end of a critical section. This will release the lock and | 93 // Called at the end of a critical section. This will release the lock and |
| 75 // condition. | 94 // condition. |
| 76 void AfterRequest(); | 95 void AfterRequest(); |
| 77 | 96 |
| 78 NSUInteger character_index_; | 97 NSUInteger character_index_; |
| 79 NSRect first_rect_; | 98 NSRect first_rect_; |
| 80 base::scoped_nsobject<NSAttributedString> substring_; | 99 base::scoped_nsobject<NSAttributedString> substring_; |
| 81 | 100 |
| 82 base::Lock lock_; | 101 base::Lock lock_; |
| 83 base::ConditionVariable condition_; | 102 base::ConditionVariable condition_; |
| 84 | 103 |
| 104 base::mac::ScopedBlock<void(^)(NSAttributedString*, NSPoint)> replyHandler_; | |
| 105 | |
| 85 DISALLOW_COPY_AND_ASSIGN(TextInputClientMac); | 106 DISALLOW_COPY_AND_ASSIGN(TextInputClientMac); |
| 86 }; | 107 }; |
| 87 | 108 |
| 88 } // namespace content | 109 } // namespace content |
| 89 | 110 |
| 90 #endif // CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ | 111 #endif // CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_CLIENT_MAC_H_ |
| OLD | NEW |