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) { | |
hidehiko
2016/09/07 01:16:25
nit: EncodeImagePng.
cf) https://google.github.io
Muyuan
2016/09/07 02:50:47
Acknowledged.
| |
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_ = nullptr; | |
hidehiko
2016/09/07 01:16:25
Could you define this as a part of components/arc/
Muyuan
2016/09/07 02:50:47
OK. Made it part of ArcIntentHelper temporarily.
| |
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 DCHECK(!instance_); | |
82 instance_ = this; | |
83 } | |
84 | |
85 ArcWallpaperService::~ArcWallpaperService() { | |
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
65 // Make sure the callback is never called after destruction. It is safe to | 87 // Make sure the callback is never called after destruction. It is safe to |
66 // call Cancel() even when there is no in-flight request. | 88 // call Cancel() even when there is no in-flight request. |
67 ImageDecoder::Cancel(this); | 89 ImageDecoder::Cancel(this); |
90 arc_bridge_service()->wallpaper()->RemoveObserver(this); | |
91 DCHECK(instance_ == this); | |
92 instance_ = nullptr; | |
68 } | 93 } |
69 | 94 |
70 void ArcWallpaperHandler::SetWallpaper(std::vector<uint8_t> jpeg_data) { | 95 void ArcWallpaperService::OnInstanceReady() { |
96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
97 mojom::WallpaperInstance* wallpaper_instance = | |
98 arc_bridge_service()->wallpaper()->instance(); | |
99 if (!wallpaper_instance) { | |
100 LOG(ERROR) << "OnWallpaperInstanceReady called, " | |
hidehiko
2016/09/07 01:16:25
Could you use LOG(DFATAL) instead?
This is unexpec
Muyuan
2016/09/07 02:50:47
Acknowledged.
| |
101 << "but no wallpaper instance found"; | |
102 return; | |
103 } | |
104 wallpaper_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
105 } | |
106 | |
107 void ArcWallpaperService::SetWallpaper(mojo::Array<uint8_t> png_data) { | |
108 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
109 ImageDecoder::Cancel(this); | |
110 ImageDecoder::StartWithOptions(this, png_data.PassStorage(), | |
111 ImageDecoder::ROBUST_PNG_CODEC, true); | |
112 } | |
113 | |
114 void ArcWallpaperService::GetWallpaper( | |
115 const base::Callback<void(mojo::Array<uint8_t>)>& callback) { | |
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
117 ash::WallpaperController* wc = | |
118 ash::Shell::GetInstance()->wallpaper_controller(); | |
119 gfx::ImageSkia wallpaper = wc->GetWallpaper(); | |
120 base::PostTaskAndReplyWithResult( | |
121 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
122 base::Bind(&EncodeImagePNG, wallpaper), callback); | |
123 } | |
124 | |
125 void ArcWallpaperService::SetWallpaperJPEG( | |
hidehiko
2016/09/07 01:16:25
Ditto. "SetWallpaperJpeg"
Muyuan
2016/09/07 02:50:47
Acknowledged.
| |
126 const std::vector<uint8_t>& jpeg_data) { | |
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
72 // If there is an in-flight request, cancel it. It is safe to call Cancel() | 128 // If there is an in-flight request, cancel it. It is safe to call Cancel() |
73 // even when there is no in-flight request. | 129 // even when there is no in-flight request. |
74 ImageDecoder::Cancel(this); | 130 ImageDecoder::Cancel(this); |
75 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), | 131 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), |
76 ImageDecoder::ROBUST_JPEG_CODEC, true); | 132 ImageDecoder::ROBUST_JPEG_CODEC, true); |
77 } | 133 } |
78 | 134 |
79 void ArcWallpaperHandler::OnImageDecoded(const SkBitmap& bitmap) { | 135 void ArcWallpaperService::OnImageDecoded(const SkBitmap& bitmap) { |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
81 SetBitmapAsWallpaper(bitmap); | 137 SetBitmapAsWallpaper(bitmap); |
82 } | 138 } |
83 | 139 |
84 void ArcWallpaperHandler::OnDecodeImageFailed() { | 140 void ArcWallpaperService::OnDecodeImageFailed() { |
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
86 LOG(ERROR) << "Failed to decode wallpaper image."; | 142 LOG(ERROR) << "Failed to decode wallpaper image."; |
87 } | 143 } |
88 | 144 |
89 } // namespace arc | 145 } // namespace arc |
OLD | NEW |