OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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_private_api.h" | 5 #include "chrome/browser/chromeos/extensions/wallpaper_private_api.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "ash/wm/window_cycle_controller.h" | 10 #include "ash/wm/window_cycle_controller.h" |
11 #include "ash/wm/window_util.h" | 11 #include "ash/wm/window_util.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/files/file_enumerator.h" | 13 #include "base/files/file_enumerator.h" |
14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/synchronization/cancellation_flag.h" | 19 #include "base/synchronization/cancellation_flag.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
21 #include "base/threading/worker_pool.h" | 21 #include "base/threading/worker_pool.h" |
22 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/chromeos/login/user.h" | 23 #include "chrome/browser/chromeos/login/user.h" |
24 #include "chrome/browser/chromeos/login/user_image.h" | 24 #include "chrome/browser/chromeos/login/user_image.h" |
25 #include "chrome/browser/chromeos/login/user_manager.h" | 25 #include "chrome/browser/chromeos/login/user_manager.h" |
26 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | 26 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
27 #include "chrome/browser/extensions/event_router.h" | 27 #include "chrome/browser/extensions/event_router.h" |
28 #include "chrome/browser/image_decoder.h" | 28 #include "chrome/browser/image_decoder.h" |
29 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
| 30 #include "chromeos/login/login_state.h" |
30 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
31 #include "googleurl/src/gurl.h" | 32 #include "googleurl/src/gurl.h" |
32 #include "grit/app_locale_settings.h" | 33 #include "grit/app_locale_settings.h" |
33 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
34 #include "grit/platform_locale_settings.h" | 35 #include "grit/platform_locale_settings.h" |
35 #include "net/url_request/url_fetcher.h" | 36 #include "net/url_request/url_fetcher.h" |
36 #include "net/url_request/url_fetcher_delegate.h" | 37 #include "net/url_request/url_fetcher_delegate.h" |
37 #include "net/url_request/url_request_status.h" | 38 #include "net/url_request/url_request_status.h" |
38 #include "ui/aura/window_observer.h" | 39 #include "ui/aura/window_observer.h" |
39 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 } | 230 } |
230 | 231 |
231 class WallpaperFunctionBase::WallpaperDecoder : public ImageDecoder::Delegate { | 232 class WallpaperFunctionBase::WallpaperDecoder : public ImageDecoder::Delegate { |
232 public: | 233 public: |
233 explicit WallpaperDecoder(scoped_refptr<WallpaperFunctionBase> function) | 234 explicit WallpaperDecoder(scoped_refptr<WallpaperFunctionBase> function) |
234 : function_(function) { | 235 : function_(function) { |
235 } | 236 } |
236 | 237 |
237 void Start(const std::string& image_data) { | 238 void Start(const std::string& image_data) { |
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
239 image_decoder_ = new ImageDecoder(this, image_data, | 240 |
240 ImageDecoder::ROBUST_JPEG_CODEC); | 241 // This function can only be called after user login. It is fine to use |
| 242 // unsafe image decoder here. Before user login, a robust jpeg decoder will |
| 243 // be used. |
| 244 CHECK(chromeos::LoginState::Get()->IsUserLoggedIn()); |
| 245 unsafe_image_decoder_ = new ImageDecoder(this, image_data, |
| 246 ImageDecoder::DEFAULT_CODEC); |
241 scoped_refptr<base::MessageLoopProxy> task_runner = | 247 scoped_refptr<base::MessageLoopProxy> task_runner = |
242 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 248 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
243 image_decoder_->Start(task_runner); | 249 unsafe_image_decoder_->Start(task_runner); |
244 } | 250 } |
245 | 251 |
246 void Cancel() { | 252 void Cancel() { |
247 cancel_flag_.Set(); | 253 cancel_flag_.Set(); |
248 } | 254 } |
249 | 255 |
250 virtual void OnImageDecoded(const ImageDecoder* decoder, | 256 virtual void OnImageDecoded(const ImageDecoder* decoder, |
251 const SkBitmap& decoded_image) OVERRIDE { | 257 const SkBitmap& decoded_image) OVERRIDE { |
252 // Make the SkBitmap immutable as we won't modify it. This is important | 258 // Make the SkBitmap immutable as we won't modify it. This is important |
253 // because otherwise it gets duplicated during painting, wasting memory. | 259 // because otherwise it gets duplicated during painting, wasting memory. |
(...skipping 12 matching lines...) Expand all Loading... |
266 } | 272 } |
267 | 273 |
268 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { | 274 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { |
269 function_->OnFailureOrCancel( | 275 function_->OnFailureOrCancel( |
270 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER)); | 276 l10n_util::GetStringUTF8(IDS_WALLPAPER_MANAGER_INVALID_WALLPAPER)); |
271 delete this; | 277 delete this; |
272 } | 278 } |
273 | 279 |
274 private: | 280 private: |
275 scoped_refptr<WallpaperFunctionBase> function_; | 281 scoped_refptr<WallpaperFunctionBase> function_; |
276 scoped_refptr<ImageDecoder> image_decoder_; | 282 scoped_refptr<ImageDecoder> unsafe_image_decoder_; |
277 base::CancellationFlag cancel_flag_; | 283 base::CancellationFlag cancel_flag_; |
278 | 284 |
279 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); | 285 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); |
280 }; | 286 }; |
281 | 287 |
282 WallpaperFunctionBase::WallpaperDecoder* | 288 WallpaperFunctionBase::WallpaperDecoder* |
283 WallpaperFunctionBase::wallpaper_decoder_; | 289 WallpaperFunctionBase::wallpaper_decoder_; |
284 | 290 |
285 WallpaperFunctionBase::WallpaperFunctionBase() { | 291 WallpaperFunctionBase::WallpaperFunctionBase() { |
286 } | 292 } |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 this, email, source)); | 881 this, email, source)); |
876 return true; | 882 return true; |
877 } | 883 } |
878 | 884 |
879 void WallpaperPrivateGetOfflineWallpaperListFunction::GetList( | 885 void WallpaperPrivateGetOfflineWallpaperListFunction::GetList( |
880 const std::string& email, | 886 const std::string& email, |
881 const std::string& source) { | 887 const std::string& source) { |
882 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( | 888 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
883 sequence_token_)); | 889 sequence_token_)); |
884 std::vector<std::string> file_list; | 890 std::vector<std::string> file_list; |
| 891 // TODO(bshe): This api function is only used for ONLINE wallpapers. Remove |
| 892 // source. |
885 if (source == kOnlineSource) { | 893 if (source == kOnlineSource) { |
886 base::FilePath wallpaper_dir; | 894 base::FilePath wallpaper_dir; |
887 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); | 895 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
888 if (file_util::DirectoryExists(wallpaper_dir)) { | 896 if (file_util::DirectoryExists(wallpaper_dir)) { |
889 base::FileEnumerator files(wallpaper_dir, false, | 897 base::FileEnumerator files(wallpaper_dir, false, |
890 base::FileEnumerator::FILES); | 898 base::FileEnumerator::FILES); |
891 for (base::FilePath current = files.Next(); !current.empty(); | 899 for (base::FilePath current = files.Next(); !current.empty(); |
892 current = files.Next()) { | 900 current = files.Next()) { |
893 std::string file_name = current.BaseName().RemoveExtension().value(); | 901 std::string file_name = current.BaseName().RemoveExtension().value(); |
894 // Do not add file name of small resolution wallpaper to the list. | 902 // Do not add file name of small resolution wallpaper to the list. |
895 if (!EndsWith(file_name, chromeos::kSmallWallpaperSuffix, true)) | 903 if (!EndsWith(file_name, chromeos::kSmallWallpaperSuffix, true)) |
896 file_list.push_back(current.BaseName().value()); | 904 file_list.push_back(current.BaseName().value()); |
897 } | 905 } |
898 } | 906 } |
899 } else { | |
900 base::FilePath custom_thumbnails_dir = chromeos::WallpaperManager::Get()-> | |
901 GetCustomWallpaperPath(chromeos::kThumbnailWallpaperSubDir, email, ""); | |
902 if (file_util::DirectoryExists(custom_thumbnails_dir)) { | |
903 base::FileEnumerator files(custom_thumbnails_dir, false, | |
904 base::FileEnumerator::FILES); | |
905 std::set<std::string> file_name_set; | |
906 for (base::FilePath current = files.Next(); !current.empty(); | |
907 current = files.Next()) { | |
908 file_name_set.insert(current.BaseName().value()); | |
909 } | |
910 for (std::set<std::string>::reverse_iterator rit = file_name_set.rbegin(); | |
911 rit != file_name_set.rend(); ++rit) { | |
912 file_list.push_back(*rit); | |
913 } | |
914 } | |
915 } | 907 } |
916 BrowserThread::PostTask( | 908 BrowserThread::PostTask( |
917 BrowserThread::UI, FROM_HERE, | 909 BrowserThread::UI, FROM_HERE, |
918 base::Bind(&WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete, | 910 base::Bind(&WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete, |
919 this, file_list)); | 911 this, file_list)); |
920 } | 912 } |
921 | 913 |
922 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( | 914 void WallpaperPrivateGetOfflineWallpaperListFunction::OnComplete( |
923 const std::vector<std::string>& file_list) { | 915 const std::vector<std::string>& file_list) { |
924 ListValue* results = new ListValue(); | 916 ListValue* results = new ListValue(); |
925 results->AppendStrings(file_list); | 917 results->AppendStrings(file_list); |
926 SetResult(results); | 918 SetResult(results); |
927 SendResponse(true); | 919 SendResponse(true); |
928 } | 920 } |
OLD | NEW |