| 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 #include "chrome/browser/speech/speech_recognition_bubble.h" | 5 #include "chrome/browser/speech/speech_recognition_bubble.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/tab_contents/tab_util.h" |
| 10 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/browser/web_contents_view.h" | 12 #include "content/public/browser/web_contents_view.h" |
| 12 #include "grit/generated_resources.h" | 13 #include "grit/generated_resources.h" |
| 13 #include "grit/theme_resources.h" | 14 #include "grit/theme_resources.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 15 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 16 #include "ui/gfx/display.h" | 17 #include "ui/gfx/display.h" |
| 17 #include "ui/gfx/image/image_skia_operations.h" | 18 #include "ui/gfx/image/image_skia_operations.h" |
| 18 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
| 19 #include "ui/gfx/screen.h" | 20 #include "ui/gfx/screen.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 116 |
| 116 base::LazyInstance<SpeechRecognitionBubbleImages> g_images = | 117 base::LazyInstance<SpeechRecognitionBubbleImages> g_images = |
| 117 LAZY_INSTANCE_INITIALIZER; | 118 LAZY_INSTANCE_INITIALIZER; |
| 118 | 119 |
| 119 } // namespace | 120 } // namespace |
| 120 | 121 |
| 121 SpeechRecognitionBubble::FactoryMethod SpeechRecognitionBubble::factory_ = NULL; | 122 SpeechRecognitionBubble::FactoryMethod SpeechRecognitionBubble::factory_ = NULL; |
| 122 const int SpeechRecognitionBubble::kBubbleTargetOffsetX = 10; | 123 const int SpeechRecognitionBubble::kBubbleTargetOffsetX = 10; |
| 123 | 124 |
| 124 SpeechRecognitionBubble* SpeechRecognitionBubble::Create( | 125 SpeechRecognitionBubble* SpeechRecognitionBubble::Create( |
| 125 WebContents* web_contents, Delegate* delegate, | 126 WebContents* test_web_contents, |
| 127 Delegate* delegate, |
| 126 const gfx::Rect& element_rect) { | 128 const gfx::Rect& element_rect) { |
| 129 // Has the tab already closed before bubble create request was processed? |
| 130 if (!test_web_contents) |
| 131 return NULL; |
| 132 |
| 133 return CreateNativeBubble(test_web_contents, delegate, element_rect); |
| 134 } |
| 135 |
| 136 SpeechRecognitionBubble* SpeechRecognitionBubble::Create( |
| 137 int render_process_id, int render_view_id, Delegate* delegate, |
| 138 const gfx::Rect& element_rect) { |
| 139 WebContents* web_contents = |
| 140 tab_util::GetWebContentsByID(render_process_id, render_view_id); |
| 141 |
| 127 if (factory_) | 142 if (factory_) |
| 128 return (*factory_)(web_contents, delegate, element_rect); | 143 return (*factory_)(web_contents, delegate, element_rect); |
| 129 | 144 |
| 130 // Has the tab already closed before bubble create request was processed? | 145 // Has the tab already closed before bubble create request was processed? |
| 131 if (!web_contents) | 146 if (!web_contents) |
| 132 return NULL; | 147 return NULL; |
| 133 | 148 |
| 134 return CreateNativeBubble(web_contents, delegate, element_rect); | 149 return CreateNativeBubble(render_process_id, render_view_id, |
| 150 delegate, element_rect); |
| 135 } | 151 } |
| 136 | 152 |
| 137 SpeechRecognitionBubbleBase::SpeechRecognitionBubbleBase( | 153 SpeechRecognitionBubbleBase::SpeechRecognitionBubbleBase( |
| 138 WebContents* web_contents) | 154 WebContents* test_web_contents) |
| 139 : weak_factory_(this), | 155 : weak_factory_(this), |
| 140 animation_step_(0), | 156 animation_step_(0), |
| 141 display_mode_(DISPLAY_MODE_RECORDING), | 157 display_mode_(DISPLAY_MODE_RECORDING), |
| 142 web_contents_(web_contents), | 158 test_web_contents_(test_web_contents), |
| 159 render_process_id_(-1), |
| 160 render_view_id_(-1), |
| 143 scale_(1.0f) { | 161 scale_(1.0f) { |
| 162 Initialize(); |
| 163 } |
| 164 |
| 165 SpeechRecognitionBubbleBase::SpeechRecognitionBubbleBase( |
| 166 int render_process_id, int render_view_id) |
| 167 : weak_factory_(this), |
| 168 animation_step_(0), |
| 169 display_mode_(DISPLAY_MODE_RECORDING), |
| 170 test_web_contents_(NULL), |
| 171 render_process_id_(render_process_id), |
| 172 render_view_id_(render_view_id), |
| 173 scale_(1.0f) { |
| 174 Initialize(); |
| 175 } |
| 176 |
| 177 SpeechRecognitionBubbleBase::~SpeechRecognitionBubbleBase() { |
| 178 // This destructor is added to make sure members such as the scoped_ptr |
| 179 // get destroyed here and the derived classes don't have to care about such |
| 180 // member variables which they don't use. |
| 181 } |
| 182 |
| 183 void SpeechRecognitionBubbleBase::Initialize() { |
| 184 WebContents* web_contents = GetWebContents(); |
| 144 gfx::NativeView view = | 185 gfx::NativeView view = |
| 145 web_contents_ ? web_contents_->GetView()->GetNativeView() : NULL; | 186 web_contents ? web_contents->GetView()->GetNativeView() : NULL; |
| 146 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); | 187 gfx::Screen* screen = gfx::Screen::GetScreenFor(view); |
| 147 gfx::Display display = screen->GetDisplayNearestWindow(view); | 188 gfx::Display display = screen->GetDisplayNearestWindow(view); |
| 148 scale_ = display.device_scale_factor(); | 189 scale_ = display.device_scale_factor(); |
| 149 | 190 |
| 150 const gfx::ImageSkiaRep& rep = | 191 const gfx::ImageSkiaRep& rep = |
| 151 g_images.Get().mic_empty()->GetRepresentation(scale_); | 192 g_images.Get().mic_empty()->GetRepresentation(scale_); |
| 152 mic_image_.reset(new SkBitmap()); | 193 mic_image_.reset(new SkBitmap()); |
| 153 mic_image_->setConfig(SkBitmap::kARGB_8888_Config, | 194 mic_image_->setConfig(SkBitmap::kARGB_8888_Config, |
| 154 rep.pixel_width(), rep.pixel_height()); | 195 rep.pixel_width(), rep.pixel_height()); |
| 155 mic_image_->allocPixels(); | 196 mic_image_->allocPixels(); |
| 156 | 197 |
| 157 buffer_image_.reset(new SkBitmap()); | 198 buffer_image_.reset(new SkBitmap()); |
| 158 buffer_image_->setConfig(SkBitmap::kARGB_8888_Config, | 199 buffer_image_->setConfig(SkBitmap::kARGB_8888_Config, |
| 159 rep.pixel_width(), rep.pixel_height()); | 200 rep.pixel_width(), rep.pixel_height()); |
| 160 buffer_image_->allocPixels(); | 201 buffer_image_->allocPixels(); |
| 161 } | 202 } |
| 162 | 203 |
| 163 SpeechRecognitionBubbleBase::~SpeechRecognitionBubbleBase() { | |
| 164 // This destructor is added to make sure members such as the scoped_ptr | |
| 165 // get destroyed here and the derived classes don't have to care about such | |
| 166 // member variables which they don't use. | |
| 167 } | |
| 168 | |
| 169 void SpeechRecognitionBubbleBase::SetWarmUpMode() { | 204 void SpeechRecognitionBubbleBase::SetWarmUpMode() { |
| 170 weak_factory_.InvalidateWeakPtrs(); | 205 weak_factory_.InvalidateWeakPtrs(); |
| 171 display_mode_ = DISPLAY_MODE_WARM_UP; | 206 display_mode_ = DISPLAY_MODE_WARM_UP; |
| 172 animation_step_ = 0; | 207 animation_step_ = 0; |
| 173 DoWarmingUpAnimationStep(); | 208 DoWarmingUpAnimationStep(); |
| 174 UpdateLayout(); | 209 UpdateLayout(); |
| 175 } | 210 } |
| 176 | 211 |
| 177 void SpeechRecognitionBubbleBase::DoWarmingUpAnimationStep() { | 212 void SpeechRecognitionBubbleBase::DoWarmingUpAnimationStep() { |
| 178 SetImage(g_images.Get().warm_up()[animation_step_]); | 213 SetImage(g_images.Get().warm_up()[animation_step_]); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 g_images.Get().mic_empty()->GetRepresentation(scale_).sk_bitmap(), | 291 g_images.Get().mic_empty()->GetRepresentation(scale_).sk_bitmap(), |
| 257 0, 0); | 292 0, 0); |
| 258 DrawVolumeOverlay(&canvas, *g_images.Get().mic_full(), volume); | 293 DrawVolumeOverlay(&canvas, *g_images.Get().mic_full(), volume); |
| 259 DrawVolumeOverlay(&canvas, *g_images.Get().mic_noise(), noise_volume); | 294 DrawVolumeOverlay(&canvas, *g_images.Get().mic_noise(), noise_volume); |
| 260 | 295 |
| 261 gfx::ImageSkia image(gfx::ImageSkiaRep(*mic_image_.get(), scale_)); | 296 gfx::ImageSkia image(gfx::ImageSkiaRep(*mic_image_.get(), scale_)); |
| 262 SetImage(image); | 297 SetImage(image); |
| 263 } | 298 } |
| 264 | 299 |
| 265 WebContents* SpeechRecognitionBubbleBase::GetWebContents() { | 300 WebContents* SpeechRecognitionBubbleBase::GetWebContents() { |
| 266 return web_contents_; | 301 if (test_web_contents_) |
| 302 return test_web_contents_; |
| 303 return tab_util::GetWebContentsByID(render_process_id_, render_view_id_); |
| 267 } | 304 } |
| 268 | 305 |
| 269 void SpeechRecognitionBubbleBase::SetImage(const gfx::ImageSkia& image) { | 306 void SpeechRecognitionBubbleBase::SetImage(const gfx::ImageSkia& image) { |
| 270 icon_image_ = image; | 307 icon_image_ = image; |
| 271 UpdateImage(); | 308 UpdateImage(); |
| 272 } | 309 } |
| 273 | 310 |
| 274 gfx::ImageSkia SpeechRecognitionBubbleBase::icon_image() { | 311 gfx::ImageSkia SpeechRecognitionBubbleBase::icon_image() { |
| 275 return icon_image_; | 312 return icon_image_; |
| 276 } | 313 } |
| OLD | NEW |