OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/wallpaper_function_base.h" | 5 #include "chrome/browser/chromeos/extensions/wallpaper_function_base.h" |
6 | 6 |
7 #include "base/synchronization/cancellation_flag.h" | 7 #include "base/synchronization/cancellation_flag.h" |
8 #include "chrome/browser/image_decoder.h" | 8 #include "chrome/browser/image_decoder.h" |
9 #include "chrome/grit/generated_resources.h" | 9 #include "chrome/grit/generated_resources.h" |
10 #include "chromeos/login/login_state.h" | 10 #include "chromeos/login/login_state.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 if (layout.compare(kWallpaperLayoutArrays[i]) == 0) | 35 if (layout.compare(kWallpaperLayoutArrays[i]) == 0) |
36 return static_cast<wallpaper::WallpaperLayout>(i); | 36 return static_cast<wallpaper::WallpaperLayout>(i); |
37 } | 37 } |
38 // Default to use CENTER layout. | 38 // Default to use CENTER layout. |
39 return wallpaper::WALLPAPER_LAYOUT_CENTER; | 39 return wallpaper::WALLPAPER_LAYOUT_CENTER; |
40 } | 40 } |
41 | 41 |
42 } // namespace wallpaper_api_util | 42 } // namespace wallpaper_api_util |
43 | 43 |
44 class WallpaperFunctionBase::UnsafeWallpaperDecoder | 44 class WallpaperFunctionBase::UnsafeWallpaperDecoder |
45 : public ImageDecoder::Delegate { | 45 : public ImageDecoder::ImageRequest { |
46 public: | 46 public: |
47 explicit UnsafeWallpaperDecoder(scoped_refptr<WallpaperFunctionBase> function) | 47 explicit UnsafeWallpaperDecoder(scoped_refptr<WallpaperFunctionBase> function) |
48 : function_(function) { | 48 : ImageRequest( |
49 } | 49 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)), |
| 50 function_(function) {} |
50 | 51 |
51 void Start(const std::vector<char>& image_data) { | 52 void Start(const std::vector<char>& image_data) { |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
53 | 54 |
54 // This function can only be called after user login. It is fine to use | 55 // This function can only be called after user login. It is fine to use |
55 // unsafe image decoder here. Before user login, a robust jpeg decoder will | 56 // unsafe image decoder here. Before user login, a robust jpeg decoder will |
56 // be used. | 57 // be used. |
57 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn()); | 58 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn()); |
58 unsafe_image_decoder_ = new ImageDecoder(this, image_data, | 59 std::string image_data_str(image_data.begin(), image_data.end()); |
59 ImageDecoder::DEFAULT_CODEC); | 60 ImageDecoder::StartWithOptions(this, image_data_str, |
60 unsafe_image_decoder_->set_shrink_to_fit(true); | 61 ImageDecoder::DEFAULT_CODEC, true); |
61 | |
62 scoped_refptr<base::MessageLoopProxy> task_runner = | |
63 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | |
64 unsafe_image_decoder_->Start(task_runner); | |
65 } | 62 } |
66 | 63 |
67 void Cancel() { | 64 void Cancel() { |
68 cancel_flag_.Set(); | 65 cancel_flag_.Set(); |
69 } | 66 } |
70 | 67 |
71 void OnImageDecoded(const ImageDecoder* decoder, | 68 void OnImageDecoded(const SkBitmap& decoded_image) override { |
72 const SkBitmap& decoded_image) override { | |
73 // Make the SkBitmap immutable as we won't modify it. This is important | 69 // Make the SkBitmap immutable as we won't modify it. This is important |
74 // because otherwise it gets duplicated during painting, wasting memory. | 70 // because otherwise it gets duplicated during painting, wasting memory. |
75 SkBitmap immutable(decoded_image); | 71 SkBitmap immutable(decoded_image); |
76 immutable.setImmutable(); | 72 immutable.setImmutable(); |
77 gfx::ImageSkia final_image = gfx::ImageSkia::CreateFrom1xBitmap(immutable); | 73 gfx::ImageSkia final_image = gfx::ImageSkia::CreateFrom1xBitmap(immutable); |
78 final_image.MakeThreadSafe(); | 74 final_image.MakeThreadSafe(); |
79 if (cancel_flag_.IsSet()) { | 75 if (cancel_flag_.IsSet()) { |
80 function_->OnCancel(); | 76 function_->OnCancel(); |
81 delete this; | 77 delete this; |
82 return; | 78 return; |
83 } | 79 } |
84 function_->OnWallpaperDecoded(final_image); | 80 function_->OnWallpaperDecoded(final_image); |
85 delete this; | 81 delete this; |
86 } | 82 } |
87 | 83 |
88 void OnDecodeImageFailed(const ImageDecoder* decoder) override { | 84 void OnDecodeImageFailed() override { |
89 function_->OnFailure( | 85 function_->OnFailure( |
90 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER)); | 86 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER)); |
91 delete this; | 87 delete this; |
92 } | 88 } |
93 | 89 |
94 private: | 90 private: |
95 scoped_refptr<WallpaperFunctionBase> function_; | 91 scoped_refptr<WallpaperFunctionBase> function_; |
96 scoped_refptr<ImageDecoder> unsafe_image_decoder_; | |
97 base::CancellationFlag cancel_flag_; | 92 base::CancellationFlag cancel_flag_; |
98 | 93 |
99 DISALLOW_COPY_AND_ASSIGN(UnsafeWallpaperDecoder); | 94 DISALLOW_COPY_AND_ASSIGN(UnsafeWallpaperDecoder); |
100 }; | 95 }; |
101 | 96 |
102 WallpaperFunctionBase::UnsafeWallpaperDecoder* | 97 WallpaperFunctionBase::UnsafeWallpaperDecoder* |
103 WallpaperFunctionBase::unsafe_wallpaper_decoder_; | 98 WallpaperFunctionBase::unsafe_wallpaper_decoder_; |
104 | 99 |
105 WallpaperFunctionBase::WallpaperFunctionBase() { | 100 WallpaperFunctionBase::WallpaperFunctionBase() { |
106 } | 101 } |
(...skipping 13 matching lines...) Expand all Loading... |
120 unsafe_wallpaper_decoder_ = NULL; | 115 unsafe_wallpaper_decoder_ = NULL; |
121 SetError(wallpaper_api_util::kCancelWallpaperMessage); | 116 SetError(wallpaper_api_util::kCancelWallpaperMessage); |
122 SendResponse(false); | 117 SendResponse(false); |
123 } | 118 } |
124 | 119 |
125 void WallpaperFunctionBase::OnFailure(const std::string& error) { | 120 void WallpaperFunctionBase::OnFailure(const std::string& error) { |
126 unsafe_wallpaper_decoder_ = NULL; | 121 unsafe_wallpaper_decoder_ = NULL; |
127 SetError(error); | 122 SetError(error); |
128 SendResponse(false); | 123 SendResponse(false); |
129 } | 124 } |
OLD | NEW |