Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: chrome/browser/chromeos/extensions/wallpaper_private_api.cc

Issue 10754014: Wallpaper manager backend APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile error on linux_chromeos_clang and format nits Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 explicit WallpaperDecoder(
56 scoped_refptr<WallpaperSetWallpaperFunction> function)
57 : function_(function) {
58 }
59
60 void Start(const std::string& image_data) {
61 image_decoder_ = new ImageDecoder(this, image_data);
62 image_decoder_->Start();
63 }
64
65 void Cancel() {
66 cancel_flag_.Set();
67 function_->SendResponse(false);
68 }
69
70 virtual void OnImageDecoded(const ImageDecoder* decoder,
71 const SkBitmap& decoded_image) OVERRIDE {
72 gfx::ImageSkia final_image(decoded_image);
73 if (cancel_flag_.IsSet()) {
74 delete this;
75 return;
76 }
77 function_->OnWallpaperDecoded(final_image);
78 delete this;
79 }
80
81 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE {
82 if (cancel_flag_.IsSet()) {
83 delete this;
84 return;
85 }
86 function_->OnFail();
87 // TODO(bshe): Dispatches an encoding error event.
88 delete this;
89 }
90
91 private:
92 scoped_refptr<WallpaperSetWallpaperFunction> function_;
93 scoped_refptr<ImageDecoder> image_decoder_;
94 base::CancellationFlag cancel_flag_;
95
96 DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder);
97 };
98
99 WallpaperSetWallpaperFunction::WallpaperDecoder*
100 WallpaperSetWallpaperFunction::wallpaper_decoder_;
101
102 WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction(){
103 }
104
105 WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction(){
106 }
107
108 bool WallpaperSetWallpaperFunction::RunImpl() {
109 BinaryValue* input = NULL;
110 if (args_ == NULL || !args_->GetBinary(0, &input)) {
111 return false;
112 }
113 std::string layout_string;
114 if (!args_->GetString(1, &layout_string) || layout_string.empty()) {
115 return false;
116 }
117 layout_ = ash::GetLayoutEnum(layout_string.c_str());
118 std::string url;
119 if (!args_->GetString(2, &url) || url.empty()) {
120 return false;
121 }
122 file_name_ = GURL(url).ExtractFileName();
123
124 // Gets email address while at UI thread.
125 email_ = chromeos::UserManager::Get()->GetLoggedInUser().email();
126
127 image_data_.assign(input->GetBuffer(), input->GetSize());
128 if (wallpaper_decoder_)
129 wallpaper_decoder_->Cancel();
130 wallpaper_decoder_ = new WallpaperDecoder(this);
131 wallpaper_decoder_->Start(image_data_);
132
133 return true;
134 }
135
136 void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
137 const gfx::ImageSkia& wallpaper) {
138 wallpaper_ = wallpaper;
139 BrowserThread::PostTask(
140 BrowserThread::FILE, FROM_HERE,
141 base::Bind(&WallpaperSetWallpaperFunction::SaveToFile,
142 this));
143 }
144
145 void WallpaperSetWallpaperFunction::OnFail() {
146 wallpaper_decoder_ = NULL;
147 SendResponse(false);
148 }
149
150 void WallpaperSetWallpaperFunction::SaveToFile() {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
152 FilePath wallpaper_dir;
153 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir));
154 if (!file_util::DirectoryExists(wallpaper_dir) &&
155 !file_util::CreateDirectory(wallpaper_dir)) {
156 BrowserThread::PostTask(
157 BrowserThread::UI, FROM_HERE,
158 base::Bind(&WallpaperSetWallpaperFunction::OnFail,
159 this));
160 return;
161 }
162 FilePath file_path = wallpaper_dir.Append(file_name_);
163 if (file_util::PathExists(file_path) ||
164 file_util::WriteFile(file_path, image_data_.c_str(),
165 image_data_.size()) != -1 ) {
166 BrowserThread::PostTask(
167 BrowserThread::UI, FROM_HERE,
168 base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper,
169 this));
170 } else {
171 BrowserThread::PostTask(
172 BrowserThread::UI, FROM_HERE,
173 base::Bind(&WallpaperSetWallpaperFunction::OnFail,
174 this));
175 }
176 }
177
178 void WallpaperSetWallpaperFunction::SetDecodedWallpaper() {
179 ash::Shell::GetInstance()->desktop_background_controller()->
180 SetCustomWallpaper(wallpaper_, layout_);
181 wallpaper_decoder_ = NULL;
182 SendResponse(true);
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698