OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/extensions/wallpaper_private_api.h" |
| 6 |
| 7 #include "ash/desktop_background/desktop_background_controller.h" |
| 8 #include "ash/shell.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/json/json_writer.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/path_service.h" |
| 13 #include "base/synchronization/cancellation_flag.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chromeos/login/user_manager.h" |
| 16 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
| 17 #include "chrome/browser/extensions/extension_event_router.h" |
| 18 #include "chrome/browser/image_decoder.h" |
| 19 #include "chrome/common/chrome_paths.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "net/url_request/url_fetcher.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" |
| 23 #include "net/url_request/url_request_status.h" |
| 24 #include "googleurl/src/gurl.h" |
| 25 #include "grit/generated_resources.h" |
| 26 #include "ui/base/l10n/l10n_util.h" |
| 27 |
| 28 using base::BinaryValue; |
| 29 using content::BrowserThread; |
| 30 |
| 31 bool WallpaperStringsFunction::RunImpl() { |
| 32 DictionaryValue* dict = new DictionaryValue(); |
| 33 SetResult(dict); |
| 34 |
| 35 #define SET_STRING(ns, id) \ |
| 36 dict->SetString(#id, l10n_util::GetStringUTF16(ns##_##id)) |
| 37 SET_STRING(IDS_WALLPAPER_MANAGER, SEARCH_TEXT_LABEL); |
| 38 SET_STRING(IDS_WALLPAPER_MANAGER, AUTHOR_LABEL); |
| 39 SET_STRING(IDS_WALLPAPER_MANAGER, CUSTOM_CATEGORY_LABEL); |
| 40 SET_STRING(IDS_WALLPAPER_MANAGER, SELECT_CUSTOM_LABEL); |
| 41 SET_STRING(IDS_WALLPAPER_MANAGER, POSITION_LABEL); |
| 42 SET_STRING(IDS_WALLPAPER_MANAGER, COLOR_LABEL); |
| 43 SET_STRING(IDS_WALLPAPER_MANAGER, PREVIEW_LABEL); |
| 44 SET_STRING(IDS_OPTIONS, SET_WALLPAPER_DAILY); |
| 45 #undef SET_STRING |
| 46 |
| 47 ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict); |
| 48 |
| 49 return true; |
| 50 } |
| 51 |
| 52 class WallpaperSetWallpaperFunction::WallpaperDecoder |
| 53 : public ImageDecoder::Delegate { |
| 54 public: |
| 55 WallpaperDecoder(scoped_refptr<WallpaperSetWallpaperFunction> function) |
| 56 : function_(function) { |
| 57 } |
| 58 |
| 59 void Start(const std::string& image_data) { |
| 60 image_decoder_ = new ImageDecoder(this, image_data); |
| 61 image_decoder_->Start(); |
| 62 } |
| 63 |
| 64 void Cancel() { |
| 65 cancel_flag_.Set(); |
| 66 function_->SendResponse(false); |
| 67 } |
| 68 |
| 69 virtual void OnImageDecoded(const ImageDecoder* decoder, |
| 70 const SkBitmap& decoded_image) OVERRIDE { |
| 71 gfx::ImageSkia final_image(decoded_image); |
| 72 if (cancel_flag_.IsSet()) { |
| 73 delete this; |
| 74 return; |
| 75 } |
| 76 function_->OnWallpaperDecoded(final_image); |
| 77 delete this; |
| 78 } |
| 79 |
| 80 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { |
| 81 if (cancel_flag_.IsSet()) { |
| 82 delete this; |
| 83 return; |
| 84 } |
| 85 function_->OnFail(); |
| 86 // TODO(bshe): Dispatches an encoding error event. |
| 87 delete this; |
| 88 } |
| 89 |
| 90 private: |
| 91 scoped_refptr<WallpaperSetWallpaperFunction> function_; |
| 92 scoped_refptr<ImageDecoder> image_decoder_; |
| 93 base::CancellationFlag cancel_flag_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder); |
| 96 }; |
| 97 |
| 98 WallpaperSetWallpaperFunction::WallpaperDecoder* |
| 99 WallpaperSetWallpaperFunction::wallpaper_decoder_; |
| 100 |
| 101 bool WallpaperSetWallpaperFunction::RunImpl() { |
| 102 BinaryValue* input = NULL; |
| 103 if (args_ == NULL || !args_->GetBinary(0, &input)) { |
| 104 return false; |
| 105 } |
| 106 std::string layout_string; |
| 107 if (!args_->GetString(1, &layout_string) || layout_string.empty()) { |
| 108 return false; |
| 109 } |
| 110 layout_ = ash::GetLayoutEnum(layout_string.c_str()); |
| 111 std::string url; |
| 112 if (!args_->GetString(2, &url) || url.empty()) { |
| 113 return false; |
| 114 } |
| 115 file_name_ = GURL(url).ExtractFileName(); |
| 116 |
| 117 // Gets email address while at UI thread. |
| 118 email_ = chromeos::UserManager::Get()->GetLoggedInUser().email(); |
| 119 |
| 120 image_data_.assign(input->GetBuffer(), input->GetSize()); |
| 121 if (wallpaper_decoder_) |
| 122 wallpaper_decoder_->Cancel(); |
| 123 wallpaper_decoder_ = new WallpaperDecoder(this); |
| 124 wallpaper_decoder_->Start(image_data_); |
| 125 |
| 126 return true; |
| 127 } |
| 128 |
| 129 void WallpaperSetWallpaperFunction::OnWallpaperDecoded( |
| 130 const gfx::ImageSkia& wallpaper) { |
| 131 wallpaper_ = wallpaper; |
| 132 BrowserThread::PostTask( |
| 133 BrowserThread::FILE, FROM_HERE, |
| 134 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile, |
| 135 this)); |
| 136 } |
| 137 |
| 138 void WallpaperSetWallpaperFunction::OnFail() { |
| 139 wallpaper_decoder_ = NULL; |
| 140 SendResponse(false); |
| 141 } |
| 142 |
| 143 void WallpaperSetWallpaperFunction::SaveToFile() { |
| 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 145 FilePath wallpaper_dir; |
| 146 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
| 147 if (!file_util::DirectoryExists(wallpaper_dir) && |
| 148 !file_util::CreateDirectory(wallpaper_dir)) { |
| 149 BrowserThread::PostTask( |
| 150 BrowserThread::UI, FROM_HERE, |
| 151 base::Bind(&WallpaperSetWallpaperFunction::OnFail, |
| 152 this)); |
| 153 return; |
| 154 } |
| 155 FilePath file_path = wallpaper_dir.Append(file_name_); |
| 156 if (file_util::PathExists(file_path) || |
| 157 file_util::WriteFile(file_path, image_data_.c_str(), |
| 158 image_data_.size()) != -1 ) { |
| 159 BrowserThread::PostTask( |
| 160 BrowserThread::UI, FROM_HERE, |
| 161 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper, |
| 162 this)); |
| 163 } else { |
| 164 BrowserThread::PostTask( |
| 165 BrowserThread::UI, FROM_HERE, |
| 166 base::Bind(&WallpaperSetWallpaperFunction::OnFail, |
| 167 this)); |
| 168 } |
| 169 } |
| 170 |
| 171 void WallpaperSetWallpaperFunction::SetDecodedWallpaper() { |
| 172 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 173 SetCustomWallpaper(wallpaper_, layout_); |
| 174 wallpaper_decoder_ = NULL; |
| 175 SendResponse(true); |
| 176 } |
OLD | NEW |