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 ArcWallpaperService::ArcWallpaperService(ArcBridgeService* bridge_service) |
75 : ArcService(bridge_service), binding_(this) { | |
76 arc_bridge_service()->wallpaper()->AddObserver(this); | |
77 } | |
62 | 78 |
63 ArcWallpaperHandler::~ArcWallpaperHandler() { | 79 ArcWallpaperService::~ArcWallpaperService() { |
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
65 // Make sure the callback is never called after destruction. It is safe to | 81 // Make sure the callback is never called after destruction. It is safe to |
66 // call Cancel() even when there is no in-flight request. | 82 // call Cancel() even when there is no in-flight request. |
67 ImageDecoder::Cancel(this); | 83 ImageDecoder::Cancel(this); |
84 arc_bridge_service()->wallpaper()->RemoveObserver(this); | |
68 } | 85 } |
69 | 86 |
70 void ArcWallpaperHandler::SetWallpaper(std::vector<uint8_t> jpeg_data) { | 87 void ArcWallpaperService::OnInstanceReady() { |
88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
89 mojom::WallpaperInstance* wallpaper_instance = | |
90 arc_bridge_service()->wallpaper()->instance(); | |
91 if (!wallpaper_instance) { | |
92 LOG(ERROR) << "OnWallpaperInstanceReady called, " | |
93 << "but no wallpaper instance found"; | |
94 return; | |
95 } | |
96 wallpaper_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
97 } | |
98 | |
99 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { | |
100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
101 ImageDecoder::Cancel(this); | |
102 ImageDecoder::StartWithOptions(this, std::move(png_data.PassStorage()), | |
Luis Héctor Chávez
2016/09/02 16:23:47
|png_data.PassStorage()| is already an rvalue, the
Luis Héctor Chávez
2016/09/06 20:05:46
ping on the removal of this std::move().
Muyuan
2016/09/06 23:37:59
done.
sry for overlooking it.
| |
103 ImageDecoder::ROBUST_PNG_CODEC, true); | |
104 } | |
105 | |
106 void ArcWallpaperService::GetWallpaper( | |
107 const base::Callback<void(mojo::Array<uint8_t>)>& callback) { | |
108 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
109 ash::WallpaperController* wc = | |
110 ash::Shell::GetInstance()->wallpaper_controller(); | |
111 gfx::ImageSkia wallpaper = wc->GetWallpaper(); | |
112 base::PostTaskAndReplyWithResult( | |
113 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
114 base::Bind(&EncodeImagePNG, wallpaper), callback); | |
115 } | |
116 | |
117 void ArcWallpaperService::SetWallpaperJPEG( | |
118 const std::vector<uint8_t>& jpeg_data) { | |
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 119 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
72 // If there is an in-flight request, cancel it. It is safe to call Cancel() | 120 // If there is an in-flight request, cancel it. It is safe to call Cancel() |
73 // even when there is no in-flight request. | 121 // even when there is no in-flight request. |
74 ImageDecoder::Cancel(this); | 122 ImageDecoder::Cancel(this); |
75 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), | 123 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), |
76 ImageDecoder::ROBUST_JPEG_CODEC, true); | 124 ImageDecoder::ROBUST_JPEG_CODEC, true); |
77 } | 125 } |
78 | 126 |
79 void ArcWallpaperHandler::OnImageDecoded(const SkBitmap& bitmap) { | 127 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
81 SetBitmapAsWallpaper(bitmap); | 129 SetBitmapAsWallpaper(bitmap); |
82 } | 130 } |
83 | 131 |
84 void ArcWallpaperHandler::OnDecodeImageFailed() { | 132 void ArcWallpaperService::OnDecodeImageFailed() { |
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 133 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
86 LOG(ERROR) << "Failed to decode wallpaper image."; | 134 LOG(ERROR) << "Failed to decode wallpaper image."; |
87 } | 135 } |
88 | 136 |
89 } // namespace arc | 137 } // namespace arc |
OLD | NEW |