OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chromeos/arc/arc_wallpaper_handler.h" | 5 #include "chrome/browser/chromeos/arc/arc_wallpaper_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "ash/shell.h" | |
12 #include "ash/wallpaper/wallpaper_controller.h" | |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/task_runner_util.h" | |
13 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | 16 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" |
17 #include "components/arc/arc_bridge_service.h" | |
14 #include "components/signin/core/account_id/account_id.h" | 18 #include "components/signin/core/account_id/account_id.h" |
15 #include "components/user_manager/user_manager.h" | 19 #include "components/user_manager/user_manager.h" |
16 #include "components/wallpaper/wallpaper_files_id.h" | 20 #include "components/wallpaper/wallpaper_files_id.h" |
17 #include "components/wallpaper/wallpaper_layout.h" | 21 #include "components/wallpaper/wallpaper_layout.h" |
18 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
23 #include "ui/gfx/codec/png_codec.h" | |
24 #include "ui/gfx/image/image.h" | |
19 #include "ui/gfx/image/image_skia.h" | 25 #include "ui/gfx/image/image_skia.h" |
26 #include "ui/gfx/image/image_util.h" | |
20 | 27 |
21 using user_manager::UserManager; | 28 using user_manager::UserManager; |
22 | 29 |
23 namespace arc { | 30 namespace arc { |
24 | 31 |
25 namespace { | 32 namespace { |
26 | 33 |
27 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; | 34 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; |
28 | 35 |
29 // Sets a decoded bitmap as the wallpaper. | 36 // Sets a decoded bitmap as the wallpaper. |
(...skipping 19 matching lines...) Expand all Loading... | |
49 account_id, wallpaper_files_id, kAndroidWallpaperFilename, | 56 account_id, wallpaper_files_id, kAndroidWallpaperFilename, |
50 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED, | 57 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED, |
51 image, update_wallpaper); | 58 image, update_wallpaper); |
52 | 59 |
53 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper | 60 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper |
54 // picker. Currently the new wallpaper does not appear there. The best way to | 61 // picker. Currently the new wallpaper does not appear there. The best way to |
55 // make this happen seems to do the same things as wallpaper_api.cc and | 62 // make this happen seems to do the same things as wallpaper_api.cc and |
56 // wallpaper_private_api.cc. | 63 // wallpaper_private_api.cc. |
57 } | 64 } |
58 | 65 |
66 std::vector<uint8_t> EncodeImagePNG(const gfx::ImageSkia image) { | |
67 std::vector<uint8_t> result; | |
68 gfx::PNGCodec::FastEncodeBGRASkBitmap(*image.bitmap(), true, &result); | |
69 return result; | |
70 } | |
71 | |
59 } // namespace | 72 } // namespace |
60 | 73 |
61 ArcWallpaperHandler::ArcWallpaperHandler() = default; | 74 // TODO(muyuanli): This will be removed once SetWallpaperDelegate class is |
75 // removed. | |
76 SetWallpaperDelegate* SetWallpaperDelegate::instance_; | |
Luis Héctor Chávez
2016/09/06 20:05:46
nit: initialize to nullptr.
Muyuan
2016/09/06 23:38:00
Done.
| |
62 | 77 |
63 ArcWallpaperHandler::~ArcWallpaperHandler() { | 78 ArcWallpaperService::ArcWallpaperService(ArcBridgeService* bridge_service) |
79 : ArcService(bridge_service), binding_(this) { | |
80 arc_bridge_service()->wallpaper()->AddObserver(this); | |
81 instance_ = (SetWallpaperDelegate*)this; | |
Luis Héctor Chávez
2016/09/06 20:05:46
nit: use C++-style casts. (actually, is it needed?
Muyuan
2016/09/06 23:37:59
Done.
| |
82 } | |
83 | |
84 ArcWallpaperService::~ArcWallpaperService() { | |
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
65 // Make sure the callback is never called after destruction. It is safe to | 86 // Make sure the callback is never called after destruction. It is safe to |
66 // call Cancel() even when there is no in-flight request. | 87 // call Cancel() even when there is no in-flight request. |
67 ImageDecoder::Cancel(this); | 88 ImageDecoder::Cancel(this); |
89 arc_bridge_service()->wallpaper()->RemoveObserver(this); | |
90 instance_ = nullptr; | |
Luis Héctor Chávez
2016/09/06 20:05:46
nit: DCHECK(instance_ == this);
Muyuan
2016/09/06 23:38:00
Done.
| |
68 } | 91 } |
69 | 92 |
70 void ArcWallpaperHandler::SetWallpaper(std::vector<uint8_t> jpeg_data) { | 93 void ArcWallpaperService::OnInstanceReady() { |
94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
95 mojom::WallpaperInstance* wallpaper_instance = | |
96 arc_bridge_service()->wallpaper()->instance(); | |
97 if (!wallpaper_instance) { | |
98 LOG(ERROR) << "OnWallpaperInstanceReady called, " | |
99 << "but no wallpaper instance found"; | |
100 return; | |
101 } | |
102 wallpaper_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
103 } | |
104 | |
105 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { | |
106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
107 ImageDecoder::Cancel(this); | |
108 ImageDecoder::StartWithOptions(this, std::move(png_data.PassStorage()), | |
109 ImageDecoder::ROBUST_PNG_CODEC, true); | |
110 } | |
111 | |
112 void ArcWallpaperService::GetWallpaper( | |
113 const base::Callback<void(mojo::Array<uint8_t>)>& callback) { | |
114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
115 ash::WallpaperController* wc = | |
116 ash::Shell::GetInstance()->wallpaper_controller(); | |
117 gfx::ImageSkia wallpaper = wc->GetWallpaper(); | |
118 base::PostTaskAndReplyWithResult( | |
119 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
120 base::Bind(&EncodeImagePNG, wallpaper), callback); | |
121 } | |
122 | |
123 void ArcWallpaperService::SetWallpaperJPEG( | |
124 const std::vector<uint8_t>& jpeg_data) { | |
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 125 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
72 // If there is an in-flight request, cancel it. It is safe to call Cancel() | 126 // If there is an in-flight request, cancel it. It is safe to call Cancel() |
73 // even when there is no in-flight request. | 127 // even when there is no in-flight request. |
74 ImageDecoder::Cancel(this); | 128 ImageDecoder::Cancel(this); |
75 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), | 129 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), |
76 ImageDecoder::ROBUST_JPEG_CODEC, true); | 130 ImageDecoder::ROBUST_JPEG_CODEC, true); |
77 } | 131 } |
78 | 132 |
79 void ArcWallpaperHandler::OnImageDecoded(const SkBitmap& bitmap) { | 133 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 134 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
81 SetBitmapAsWallpaper(bitmap); | 135 SetBitmapAsWallpaper(bitmap); |
82 } | 136 } |
83 | 137 |
84 void ArcWallpaperHandler::OnDecodeImageFailed() { | 138 void ArcWallpaperService::OnDecodeImageFailed() { |
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 139 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
86 LOG(ERROR) << "Failed to decode wallpaper image."; | 140 LOG(ERROR) << "Failed to decode wallpaper image."; |
87 } | 141 } |
88 | 142 |
89 } // namespace arc | 143 } // namespace arc |
OLD | NEW |