OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #include "chrome/browser/ui/ash/launcher/arc_app_window.h" |
| 6 |
| 7 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" |
| 8 #include "chrome/browser/ui/ash/launcher/arc_app_window_launcher_controller.h" |
| 9 #include "chrome/browser/ui/ash/launcher/arc_app_window_launcher_item_controller
.h" |
| 10 #include "components/exo/shell_surface.h" |
| 11 #include "extensions/common/constants.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/gfx/codec/png_codec.h" |
| 14 #include "ui/gfx/image/image_skia_operations.h" |
| 15 #include "ui/gfx/image/image_skia_source.h" |
| 16 #include "ui/views/widget/widget.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Set to true for unit tests to avoid IPC icon decoding. |
| 21 bool disable_safe_icon_decoding = false; |
| 22 |
| 23 // Source of the custom icon of ARC app window. It takes reference bitmap and |
| 24 // resizes to required scale factor on demand. |
| 25 class CustomIconSource : public gfx::ImageSkiaSource { |
| 26 public: |
| 27 explicit CustomIconSource(const SkBitmap& base_image) |
| 28 : base_image_(base_image) {} |
| 29 ~CustomIconSource() override = default; |
| 30 |
| 31 private: |
| 32 // gfx::ImageSkiaSource overrides: |
| 33 gfx::ImageSkiaRep GetImageForScale(float scale) override { |
| 34 const gfx::Size target_pixel_size = |
| 35 gfx::ScaleToCeiledSize(gfx::Size(extension_misc::EXTENSION_ICON_SMALL, |
| 36 extension_misc::EXTENSION_ICON_SMALL), |
| 37 scale); |
| 38 |
| 39 const SkBitmap resized = skia::ImageOperations::Resize( |
| 40 base_image_, skia::ImageOperations::RESIZE_BEST, |
| 41 target_pixel_size.width(), target_pixel_size.height()); |
| 42 return gfx::ImageSkiaRep(resized, scale); |
| 43 } |
| 44 |
| 45 const SkBitmap base_image_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(CustomIconSource); |
| 48 }; |
| 49 |
| 50 } // namespace |
| 51 |
| 52 ArcAppWindow::ArcAppWindow(int task_id, |
| 53 const arc::ArcAppShelfId& app_shelf_id, |
| 54 views::Widget* widget, |
| 55 ArcAppWindowLauncherController* owner) |
| 56 : task_id_(task_id), |
| 57 app_shelf_id_(app_shelf_id), |
| 58 widget_(widget), |
| 59 owner_(owner) { |
| 60 ExtractIconFromWindow(); |
| 61 GetNativeWindow()->AddObserver(this); |
| 62 } |
| 63 |
| 64 ArcAppWindow::~ArcAppWindow() { |
| 65 ImageDecoder::Cancel(this); |
| 66 if (GetNativeWindow()) |
| 67 GetNativeWindow()->RemoveObserver(this); |
| 68 } |
| 69 |
| 70 // static |
| 71 void ArcAppWindow::DisableSafeIconDecodingForTesting() { |
| 72 disable_safe_icon_decoding = true; |
| 73 } |
| 74 |
| 75 void ArcAppWindow::SetController( |
| 76 ArcAppWindowLauncherItemController* controller) { |
| 77 DCHECK(!controller_ && controller); |
| 78 controller_ = controller; |
| 79 } |
| 80 |
| 81 void ArcAppWindow::ResetController() { |
| 82 controller_ = nullptr; |
| 83 } |
| 84 |
| 85 void ArcAppWindow::SetFullscreenMode(FullScreenMode mode) { |
| 86 DCHECK(mode != FullScreenMode::NOT_DEFINED); |
| 87 fullscreen_mode_ = mode; |
| 88 } |
| 89 |
| 90 bool ArcAppWindow::IsActive() const { |
| 91 return widget_->IsActive() && owner_->active_task_id() == task_id_; |
| 92 } |
| 93 |
| 94 bool ArcAppWindow::IsMaximized() const { |
| 95 NOTREACHED(); |
| 96 return false; |
| 97 } |
| 98 |
| 99 bool ArcAppWindow::IsMinimized() const { |
| 100 NOTREACHED(); |
| 101 return false; |
| 102 } |
| 103 |
| 104 bool ArcAppWindow::IsFullscreen() const { |
| 105 NOTREACHED(); |
| 106 return false; |
| 107 } |
| 108 |
| 109 gfx::NativeWindow ArcAppWindow::GetNativeWindow() const { |
| 110 return widget_->GetNativeWindow(); |
| 111 } |
| 112 |
| 113 gfx::Rect ArcAppWindow::GetRestoredBounds() const { |
| 114 NOTREACHED(); |
| 115 return gfx::Rect(); |
| 116 } |
| 117 |
| 118 ui::WindowShowState ArcAppWindow::GetRestoredState() const { |
| 119 NOTREACHED(); |
| 120 return ui::SHOW_STATE_NORMAL; |
| 121 } |
| 122 |
| 123 gfx::Rect ArcAppWindow::GetBounds() const { |
| 124 NOTREACHED(); |
| 125 return gfx::Rect(); |
| 126 } |
| 127 |
| 128 void ArcAppWindow::Show() { |
| 129 widget_->Show(); |
| 130 } |
| 131 |
| 132 void ArcAppWindow::ShowInactive() { |
| 133 NOTREACHED(); |
| 134 } |
| 135 |
| 136 void ArcAppWindow::Hide() { |
| 137 NOTREACHED(); |
| 138 } |
| 139 |
| 140 void ArcAppWindow::Close() { |
| 141 arc::CloseTask(task_id_); |
| 142 } |
| 143 |
| 144 void ArcAppWindow::Activate() { |
| 145 widget_->Activate(); |
| 146 } |
| 147 |
| 148 void ArcAppWindow::Deactivate() { |
| 149 NOTREACHED(); |
| 150 } |
| 151 |
| 152 void ArcAppWindow::Maximize() { |
| 153 NOTREACHED(); |
| 154 } |
| 155 |
| 156 void ArcAppWindow::Minimize() { |
| 157 widget_->Minimize(); |
| 158 } |
| 159 |
| 160 void ArcAppWindow::Restore() { |
| 161 NOTREACHED(); |
| 162 } |
| 163 |
| 164 void ArcAppWindow::SetBounds(const gfx::Rect& bounds) { |
| 165 NOTREACHED(); |
| 166 } |
| 167 |
| 168 void ArcAppWindow::FlashFrame(bool flash) { |
| 169 NOTREACHED(); |
| 170 } |
| 171 |
| 172 bool ArcAppWindow::IsAlwaysOnTop() const { |
| 173 NOTREACHED(); |
| 174 return false; |
| 175 } |
| 176 |
| 177 void ArcAppWindow::SetAlwaysOnTop(bool always_on_top) { |
| 178 NOTREACHED(); |
| 179 } |
| 180 |
| 181 void ArcAppWindow::ResetIcon() { |
| 182 if (image_skia_.isNull()) |
| 183 return; |
| 184 image_skia_ = gfx::ImageSkia(); |
| 185 controller()->OnWindowChanged(this); |
| 186 } |
| 187 |
| 188 void ArcAppWindow::ExtractIconFromWindow() { |
| 189 ImageDecoder::Cancel(this); |
| 190 const std::string* unsafe_icon_data = |
| 191 exo::ShellSurface::GetUnsafeIconPngData(GetNativeWindow()); |
| 192 if (unsafe_icon_data && !unsafe_icon_data->empty()) { |
| 193 if (disable_safe_icon_decoding) { |
| 194 SkBitmap bitmap; |
| 195 if (gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>( |
| 196 &unsafe_icon_data->front()), |
| 197 unsafe_icon_data->length(), &bitmap)) { |
| 198 OnImageDecoded(bitmap); |
| 199 } else { |
| 200 OnDecodeImageFailed(); |
| 201 } |
| 202 } else { |
| 203 ImageDecoder::Start(this, *unsafe_icon_data); |
| 204 } |
| 205 } else { |
| 206 ResetIcon(); |
| 207 } |
| 208 } |
| 209 |
| 210 void ArcAppWindow::OnWindowPropertyChanged(aura::Window* window, |
| 211 const void* key, |
| 212 intptr_t old) { |
| 213 if (!exo::ShellSurface::IsUnsafeIconPngDataKey(key)) |
| 214 return; |
| 215 ExtractIconFromWindow(); |
| 216 } |
| 217 |
| 218 void ArcAppWindow::OnImageDecoded(const SkBitmap& decoded_image) { |
| 219 image_skia_ = gfx::ImageSkia(new CustomIconSource(decoded_image), |
| 220 gfx::Size(extension_misc::EXTENSION_ICON_SMALL, |
| 221 extension_misc::EXTENSION_ICON_SMALL)); |
| 222 controller()->OnWindowChanged(this); |
| 223 } |
| 224 |
| 225 void ArcAppWindow::OnDecodeImageFailed() { |
| 226 ResetIcon(); |
| 227 } |
OLD | NEW |