OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/arc/arc_wallpaper_handler.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 #include <vector> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" | |
14 #include "components/signin/core/account_id/account_id.h" | |
15 #include "components/user_manager/user_manager.h" | |
16 #include "components/wallpaper/wallpaper_files_id.h" | |
17 #include "components/wallpaper/wallpaper_layout.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "ui/gfx/image/image_skia.h" | |
20 | |
21 using user_manager::UserManager; | |
22 | |
23 namespace arc { | |
24 | |
25 namespace { | |
26 | |
27 constexpr char kAndroidWallpaperFilename[] = "android.jpg"; | |
28 | |
29 // Sets a decoded bitmap as the wallpaper. | |
30 void SetBitmapAsWallpaper(const SkBitmap& bitmap) { | |
31 // Make the SkBitmap immutable as we won't modify it. This is important | |
32 // because otherwise it gets duplicated during painting, wasting memory. | |
33 SkBitmap immutable_bitmap(bitmap); | |
34 immutable_bitmap.setImmutable(); | |
35 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(immutable_bitmap); | |
36 image.MakeThreadSafe(); | |
37 | |
38 chromeos::WallpaperManager* wallpaper_manager = | |
39 chromeos::WallpaperManager::Get(); | |
40 | |
41 const AccountId& account_id = | |
42 UserManager::Get()->GetPrimaryUser()->GetAccountId(); | |
43 wallpaper::WallpaperFilesId wallpaper_files_id = | |
44 wallpaper_manager->GetFilesId(account_id); | |
45 const bool update_wallpaper = | |
46 account_id == UserManager::Get()->GetActiveUser()->GetAccountId(); | |
47 // TODO(crbug.com/618922): Allow specifying layout. | |
48 wallpaper_manager->SetCustomWallpaper( | |
49 account_id, wallpaper_files_id, kAndroidWallpaperFilename, | |
50 wallpaper::WALLPAPER_LAYOUT_STRETCH, user_manager::User::CUSTOMIZED, | |
51 image, update_wallpaper); | |
52 | |
53 // 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 | |
55 // make this happen seems to do the same things as wallpaper_api.cc and | |
56 // wallpaper_private_api.cc. | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 ArcWallpaperHandler::ArcWallpaperHandler() = default; | |
62 | |
63 ArcWallpaperHandler::~ArcWallpaperHandler() { | |
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
65 // Make sure the callback is never called after destruction. It is safe to | |
66 // call Cancel() even when there is no in-flight request. | |
67 ImageDecoder::Cancel(this); | |
68 } | |
69 | |
70 void ArcWallpaperHandler::SetWallpaper(std::vector<uint8_t> jpeg_data) { | |
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
72 // If there is an in-flight request, cancel it. It is safe to call Cancel() | |
73 // even when there is no in-flight request. | |
74 ImageDecoder::Cancel(this); | |
75 ImageDecoder::StartWithOptions(this, std::move(jpeg_data), | |
76 ImageDecoder::ROBUST_JPEG_CODEC, true); | |
77 } | |
78 | |
79 void ArcWallpaperHandler::OnImageDecoded(const SkBitmap& bitmap) { | |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
81 SetBitmapAsWallpaper(bitmap); | |
82 } | |
83 | |
84 void ArcWallpaperHandler::OnDecodeImageFailed() { | |
85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
86 LOG(ERROR) << "Failed to decode wallpaper image."; | |
87 } | |
88 | |
89 } // namespace arc | |
OLD | NEW |