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_handler.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" |
| 13 #include "base/bind.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/task_runner_util.h" |
13 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | 17 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.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/image/image.h" |
19 #include "ui/gfx/image/image_skia.h" | 24 #include "ui/gfx/image/image_skia.h" |
| 25 #include "ui/gfx/image/image_util.h" |
20 | 26 |
21 using user_manager::UserManager; | 27 using user_manager::UserManager; |
22 | 28 |
23 namespace arc { | 29 namespace arc { |
24 | 30 |
25 namespace { | 31 namespace { |
26 | 32 |
27 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; | 33 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; |
28 | 34 |
29 // Sets a decoded bitmap as the wallpaper. | 35 // Sets a decoded bitmap as the wallpaper. |
(...skipping 19 matching lines...) Expand all Loading... |
49 account_id, wallpaper_files_id, kAndroidWallpaperFilename, | 55 account_id, wallpaper_files_id, kAndroidWallpaperFilename, |
50 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED, | 56 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED, |
51 image, update_wallpaper); | 57 image, update_wallpaper); |
52 | 58 |
53 // TODO(crbug.com/618922): Register the wallpaper to Chrome OS wallpaper | 59 // 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 | 60 // 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 | 61 // make this happen seems to do the same things as wallpaper_api.cc and |
56 // wallpaper_private_api.cc. | 62 // wallpaper_private_api.cc. |
57 } | 63 } |
58 | 64 |
| 65 std::vector<uint8_t> EncodeImageJPEG(const gfx::Image image) { |
| 66 std::vector<uint8_t> result; |
| 67 gfx::JPEG1xEncodedDataFromImage(image, 100, &result); |
| 68 return result; |
| 69 } |
| 70 |
59 } // namespace | 71 } // namespace |
60 | 72 |
61 ArcWallpaperHandler::ArcWallpaperHandler() = default; | 73 ArcWallpaperHandler::ArcWallpaperHandler() = default; |
62 | 74 |
63 ArcWallpaperHandler::~ArcWallpaperHandler() { | 75 ArcWallpaperHandler::~ArcWallpaperHandler() { |
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
65 // Make sure the callback is never called after destruction. It is safe to | 77 // Make sure the callback is never called after destruction. It is safe to |
66 // call Cancel() even when there is no in-flight request. | 78 // call Cancel() even when there is no in-flight request. |
67 ImageDecoder::Cancel(this); | 79 ImageDecoder::Cancel(this); |
68 } | 80 } |
69 | 81 |
70 void ArcWallpaperHandler::SetWallpaper(std::vector<uint8_t> jpeg_data) { | 82 void ArcWallpaperHandler::SetWallpaper(const std::vector<uint8_t>& jpeg_data) { |
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
72 // If there is an in-flight request, cancel it. It is safe to call Cancel() | 84 // If there is an in-flight request, cancel it. It is safe to call Cancel() |
73 // even when there is no in-flight request. | 85 // even when there is no in-flight request. |
74 ImageDecoder::Cancel(this); | 86 ImageDecoder::Cancel(this); |
75 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), | 87 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), |
76 ImageDecoder::ROBUST_JPEG_CODEC, true); | 88 ImageDecoder::ROBUST_JPEG_CODEC, true); |
77 } | 89 } |
78 | 90 |
| 91 void ArcWallpaperHandler::GetWallpaper( |
| 92 const base::Callback<void(const std::vector<uint8_t>& image)>& callback) |
| 93 const { |
| 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 95 ash::WallpaperController* wc = |
| 96 ash::Shell::GetInstance()->wallpaper_controller(); |
| 97 gfx::Image wallpaper(wc->GetWallpaper()); |
| 98 base::PostTaskAndReplyWithResult( |
| 99 content::BrowserThread::GetBlockingPool(), FROM_HERE, |
| 100 base::Bind(&EncodeImageJPEG, wallpaper), callback); |
| 101 } |
| 102 |
79 void ArcWallpaperHandler::OnImageDecoded(const SkBitmap& bitmap) { | 103 void ArcWallpaperHandler::OnImageDecoded(const SkBitmap& bitmap) { |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
81 SetBitmapAsWallpaper(bitmap); | 105 SetBitmapAsWallpaper(bitmap); |
82 } | 106 } |
83 | 107 |
84 void ArcWallpaperHandler::OnDecodeImageFailed() { | 108 void ArcWallpaperHandler::OnDecodeImageFailed() { |
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
86 LOG(ERROR) << "Failed to decode wallpaper image."; | 110 LOG(ERROR) << "Failed to decode wallpaper image."; |
87 } | 111 } |
88 | 112 |
89 } // namespace arc | 113 } // namespace arc |
OLD | NEW |