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