| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 : function_(function) { | 48 : function_(function) { |
| 49 } | 49 } |
| 50 | 50 |
| 51 void Start(const std::vector<char>& image_data) { | 51 void Start(const std::vector<char>& image_data) { |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 53 | 53 |
| 54 // This function can only be called after user login. It is fine to use | 54 // 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 | 55 // unsafe image decoder here. Before user login, a robust jpeg decoder will |
| 56 // be used. | 56 // be used. |
| 57 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn()); | 57 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn()); |
| 58 unsafe_image_decoder_ = new ImageDecoder(this, image_data, | |
| 59 ImageDecoder::DEFAULT_CODEC); | |
| 60 unsafe_image_decoder_->set_shrink_to_fit(true); | |
| 61 | |
| 62 scoped_refptr<base::MessageLoopProxy> task_runner = | 58 scoped_refptr<base::MessageLoopProxy> task_runner = |
| 63 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 59 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 64 unsafe_image_decoder_->Start(task_runner); | 60 std::string image_data_str(image_data.begin(), image_data.end()); |
| 61 ImageDecoder::GetInstance()->Start( |
| 62 this, image_data_str, ImageDecoder::DEFAULT_CODEC, task_runner, true); |
| 65 } | 63 } |
| 66 | 64 |
| 67 void Cancel() { | 65 void Cancel() { |
| 68 cancel_flag_.Set(); | 66 cancel_flag_.Set(); |
| 69 } | 67 } |
| 70 | 68 |
| 71 void OnImageDecoded(const ImageDecoder* decoder, | 69 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 | 70 // Make the SkBitmap immutable as we won't modify it. This is important |
| 74 // because otherwise it gets duplicated during painting, wasting memory. | 71 // because otherwise it gets duplicated during painting, wasting memory. |
| 75 SkBitmap immutable(decoded_image); | 72 SkBitmap immutable(decoded_image); |
| 76 immutable.setImmutable(); | 73 immutable.setImmutable(); |
| 77 gfx::ImageSkia final_image = gfx::ImageSkia::CreateFrom1xBitmap(immutable); | 74 gfx::ImageSkia final_image = gfx::ImageSkia::CreateFrom1xBitmap(immutable); |
| 78 final_image.MakeThreadSafe(); | 75 final_image.MakeThreadSafe(); |
| 79 if (cancel_flag_.IsSet()) { | 76 if (cancel_flag_.IsSet()) { |
| 80 function_->OnCancel(); | 77 function_->OnCancel(); |
| 81 delete this; | 78 delete this; |
| 82 return; | 79 return; |
| 83 } | 80 } |
| 84 function_->OnWallpaperDecoded(final_image); | 81 function_->OnWallpaperDecoded(final_image); |
| 85 delete this; | 82 delete this; |
| 86 } | 83 } |
| 87 | 84 |
| 88 void OnDecodeImageFailed(const ImageDecoder* decoder) override { | 85 void OnDecodeImageFailed() override { |
| 89 function_->OnFailure( | 86 function_->OnFailure( |
| 90 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER)); | 87 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER)); |
| 91 delete this; | 88 delete this; |
| 92 } | 89 } |
| 93 | 90 |
| 94 private: | 91 private: |
| 95 scoped_refptr<WallpaperFunctionBase> function_; | 92 scoped_refptr<WallpaperFunctionBase> function_; |
| 96 scoped_refptr<ImageDecoder> unsafe_image_decoder_; | |
| 97 base::CancellationFlag cancel_flag_; | 93 base::CancellationFlag cancel_flag_; |
| 98 | 94 |
| 99 DISALLOW_COPY_AND_ASSIGN(UnsafeWallpaperDecoder); | 95 DISALLOW_COPY_AND_ASSIGN(UnsafeWallpaperDecoder); |
| 100 }; | 96 }; |
| 101 | 97 |
| 102 WallpaperFunctionBase::UnsafeWallpaperDecoder* | 98 WallpaperFunctionBase::UnsafeWallpaperDecoder* |
| 103 WallpaperFunctionBase::unsafe_wallpaper_decoder_; | 99 WallpaperFunctionBase::unsafe_wallpaper_decoder_; |
| 104 | 100 |
| 105 WallpaperFunctionBase::WallpaperFunctionBase() { | 101 WallpaperFunctionBase::WallpaperFunctionBase() { |
| 106 } | 102 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 120 unsafe_wallpaper_decoder_ = NULL; | 116 unsafe_wallpaper_decoder_ = NULL; |
| 121 SetError(wallpaper_api_util::kCancelWallpaperMessage); | 117 SetError(wallpaper_api_util::kCancelWallpaperMessage); |
| 122 SendResponse(false); | 118 SendResponse(false); |
| 123 } | 119 } |
| 124 | 120 |
| 125 void WallpaperFunctionBase::OnFailure(const std::string& error) { | 121 void WallpaperFunctionBase::OnFailure(const std::string& error) { |
| 126 unsafe_wallpaper_decoder_ = NULL; | 122 unsafe_wallpaper_decoder_ = NULL; |
| 127 SetError(error); | 123 SetError(error); |
| 128 SendResponse(false); | 124 SendResponse(false); |
| 129 } | 125 } |
| OLD | NEW |