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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "chrome/browser/speech/speech_recognition_bubble.h" | 7 #include "chrome/browser/speech/speech_recognition_bubble.h" |
8 | 8 |
9 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h" | 11 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h" |
12 #include "chrome/browser/ui/cocoa/browser_window_controller.h" | 12 #include "chrome/browser/ui/cocoa/browser_window_controller.h" |
13 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | 13 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
14 #import "chrome/browser/ui/cocoa/speech_recognition_window_controller.h" | 14 #import "chrome/browser/ui/cocoa/speech_recognition_window_controller.h" |
15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
16 #include "content/public/browser/web_contents_view.h" | 16 #include "content/public/browser/web_contents_view.h" |
17 #include "ui/gfx/image/image_skia_util_mac.h" | 17 #include "ui/gfx/image/image_skia_util_mac.h" |
18 | 18 |
19 using content::WebContents; | 19 using content::WebContents; |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // A class to bridge between the speech recognition C++ code and the Objective-C | 23 // A class to bridge between the speech recognition C++ code and the Objective-C |
24 // bubble implementation. See chrome/browser/speech/speech_recognition_bubble.h | 24 // bubble implementation. See chrome/browser/speech/speech_recognition_bubble.h |
25 // for more information on how this gets used. | 25 // for more information on how this gets used. |
26 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase { | 26 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase { |
27 public: | 27 public: |
28 SpeechRecognitionBubbleImpl(WebContents* web_contents, | 28 SpeechRecognitionBubbleImpl(int render_process_id, |
| 29 int render_view_id, |
29 Delegate* delegate, | 30 Delegate* delegate, |
30 const gfx::Rect& element_rect); | 31 const gfx::Rect& element_rect); |
31 virtual ~SpeechRecognitionBubbleImpl(); | 32 virtual ~SpeechRecognitionBubbleImpl(); |
32 virtual void Show() OVERRIDE; | 33 virtual void Show() OVERRIDE; |
33 virtual void Hide() OVERRIDE; | 34 virtual void Hide() OVERRIDE; |
34 virtual void UpdateLayout() OVERRIDE; | 35 virtual void UpdateLayout() OVERRIDE; |
35 virtual void UpdateImage() OVERRIDE; | 36 virtual void UpdateImage() OVERRIDE; |
36 | 37 |
37 private: | 38 private: |
38 base::scoped_nsobject<SpeechRecognitionWindowController> window_; | 39 base::scoped_nsobject<SpeechRecognitionWindowController> window_; |
39 Delegate* delegate_; | 40 Delegate* delegate_; |
40 gfx::Rect element_rect_; | 41 gfx::Rect element_rect_; |
41 }; | 42 }; |
42 | 43 |
43 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( | 44 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( |
44 WebContents* web_contents, | 45 int render_process_id, |
| 46 int render_view_id, |
45 Delegate* delegate, | 47 Delegate* delegate, |
46 const gfx::Rect& element_rect) | 48 const gfx::Rect& element_rect) |
47 : SpeechRecognitionBubbleBase(web_contents), | 49 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), |
48 delegate_(delegate), | 50 delegate_(delegate), |
49 element_rect_(element_rect) { | 51 element_rect_(element_rect) { |
50 } | 52 } |
51 | 53 |
52 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { | 54 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { |
53 if (window_.get()) | 55 if (window_.get()) |
54 [window_.get() close]; | 56 [window_.get() close]; |
55 } | 57 } |
56 | 58 |
57 void SpeechRecognitionBubbleImpl::UpdateImage() { | 59 void SpeechRecognitionBubbleImpl::UpdateImage() { |
58 if (window_.get()) | 60 if (window_.get() && GetWebContents()) |
59 [window_.get() setImage:gfx::NSImageFromImageSkia(icon_image())]; | 61 [window_.get() setImage:gfx::NSImageFromImageSkia(icon_image())]; |
60 } | 62 } |
61 | 63 |
62 void SpeechRecognitionBubbleImpl::Show() { | 64 void SpeechRecognitionBubbleImpl::Show() { |
| 65 if (!GetWebContents()) |
| 66 return; |
| 67 |
63 if (window_.get()) { | 68 if (window_.get()) { |
64 [window_.get() show]; | 69 [window_.get() show]; |
65 return; | 70 return; |
66 } | 71 } |
67 | 72 |
68 // Find the screen coordinates for the given tab and position the bubble's | 73 // Find the screen coordinates for the given tab and position the bubble's |
69 // arrow anchor point inside that to point at the bottom-left of the html | 74 // arrow anchor point inside that to point at the bottom-left of the html |
70 // input element rect if the position is valid, otherwise point it towards | 75 // input element rect if the position is valid, otherwise point it towards |
71 // the page icon in the omnibox. | 76 // the page icon in the omnibox. |
72 gfx::NativeView view = GetWebContents()->GetView()->GetNativeView(); | 77 gfx::NativeView view = GetWebContents()->GetView()->GetNativeView(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 113 |
109 void SpeechRecognitionBubbleImpl::Hide() { | 114 void SpeechRecognitionBubbleImpl::Hide() { |
110 if (!window_.get()) | 115 if (!window_.get()) |
111 return; | 116 return; |
112 | 117 |
113 [window_.get() close]; | 118 [window_.get() close]; |
114 window_.reset(); | 119 window_.reset(); |
115 } | 120 } |
116 | 121 |
117 void SpeechRecognitionBubbleImpl::UpdateLayout() { | 122 void SpeechRecognitionBubbleImpl::UpdateLayout() { |
118 if (!window_.get()) | 123 if (!window_.get() || !GetWebContents()) |
119 return; | 124 return; |
120 | 125 |
121 [window_.get() updateLayout:display_mode() | 126 [window_.get() updateLayout:display_mode() |
122 messageText:message_text() | 127 messageText:message_text() |
123 iconImage:gfx::NSImageFromImageSkia(icon_image())]; | 128 iconImage:gfx::NSImageFromImageSkia(icon_image())]; |
124 } | 129 } |
125 | 130 |
126 } // namespace | 131 } // namespace |
127 | 132 |
128 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( | 133 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( |
129 WebContents* web_contents, | 134 int render_process_id, |
| 135 int render_view_id, |
130 Delegate* delegate, | 136 Delegate* delegate, |
131 const gfx::Rect& element_rect) { | 137 const gfx::Rect& element_rect) { |
132 return new SpeechRecognitionBubbleImpl(web_contents, delegate, element_rect); | 138 return new SpeechRecognitionBubbleImpl(render_process_id, |
| 139 render_view_id, |
| 140 delegate, |
| 141 element_rect); |
133 } | 142 } |
OLD | NEW |