1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/extensions/wallpaper_private_api.h"
#include "ash/desktop_background/desktop_background_controller.h"
#include "ash/shell.h"
#include "base/file_util.h"
#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/synchronization/cancellation_flag.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/chromeos/login/wallpaper_manager.h"
#include "chrome/browser/extensions/event_router.h"
#include "chrome/browser/image_decoder.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_status.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
using base::BinaryValue;
using content::BrowserThread;
bool WallpaperStringsFunction::RunImpl() {
DictionaryValue* dict = new DictionaryValue();
SetResult(dict);
#define SET_STRING(id, idr) \
dict->SetString(id, l10n_util::GetStringUTF16(idr))
SET_STRING("searchTextLabel", IDS_WALLPAPER_MANAGER_SEARCH_TEXT_LABEL);
SET_STRING("authorLabel", IDS_WALLPAPER_MANAGER_AUTHOR_LABEL);
SET_STRING("customCategoryLabel",
IDS_WALLPAPER_MANAGER_CUSTOM_CATEGORY_LABEL);
SET_STRING("selectCustomLabel",
IDS_WALLPAPER_MANAGER_SELECT_CUSTOM_LABEL);
SET_STRING("positionLabel", IDS_WALLPAPER_MANAGER_POSITION_LABEL);
SET_STRING("colorLabel", IDS_WALLPAPER_MANAGER_COLOR_LABEL);
SET_STRING("previewLabel", IDS_WALLPAPER_MANAGER_PREVIEW_LABEL);
SET_STRING("downloadingLabel", IDS_WALLPAPER_MANAGER_DOWNLOADING_LABEL);
SET_STRING("setWallpaperDaily", IDS_OPTIONS_SET_WALLPAPER_DAILY);
SET_STRING("searchTextLabel", IDS_WALLPAPER_MANAGER_SEARCH_TEXT_LABEL);
#undef SET_STRING
ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict);
return true;
}
class WallpaperSetWallpaperFunction::WallpaperDecoder
: public ImageDecoder::Delegate {
public:
explicit WallpaperDecoder(
scoped_refptr<WallpaperSetWallpaperFunction> function)
: function_(function) {
}
void Start(const std::string& image_data) {
image_decoder_ = new ImageDecoder(this, image_data,
ImageDecoder::ROBUST_JPEG_CODEC);
image_decoder_->Start();
}
void Cancel() {
cancel_flag_.Set();
function_->SendResponse(false);
}
virtual void OnImageDecoded(const ImageDecoder* decoder,
const SkBitmap& decoded_image) OVERRIDE {
gfx::ImageSkia final_image(decoded_image);
if (cancel_flag_.IsSet()) {
delete this;
return;
}
function_->OnWallpaperDecoded(final_image);
delete this;
}
virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE {
if (cancel_flag_.IsSet()) {
delete this;
return;
}
function_->OnFail();
// TODO(bshe): Dispatches an encoding error event.
delete this;
}
private:
scoped_refptr<WallpaperSetWallpaperFunction> function_;
scoped_refptr<ImageDecoder> image_decoder_;
base::CancellationFlag cancel_flag_;
DISALLOW_COPY_AND_ASSIGN(WallpaperDecoder);
};
WallpaperSetWallpaperFunction::WallpaperDecoder*
WallpaperSetWallpaperFunction::wallpaper_decoder_;
WallpaperSetWallpaperFunction::WallpaperSetWallpaperFunction() {
}
WallpaperSetWallpaperFunction::~WallpaperSetWallpaperFunction() {
}
bool WallpaperSetWallpaperFunction::RunImpl() {
BinaryValue* input = NULL;
if (args_ == NULL || !args_->GetBinary(0, &input)) {
return false;
}
std::string layout_string;
if (!args_->GetString(1, &layout_string) || layout_string.empty()) {
return false;
}
layout_ = ash::GetLayoutEnum(layout_string);
std::string url;
if (!args_->GetString(2, &url) || url.empty()) {
return false;
}
file_name_ = GURL(url).ExtractFileName();
// Gets email address while at UI thread.
email_ = chromeos::UserManager::Get()->GetLoggedInUser().email();
image_data_.assign(input->GetBuffer(), input->GetSize());
if (wallpaper_decoder_)
wallpaper_decoder_->Cancel();
wallpaper_decoder_ = new WallpaperDecoder(this);
wallpaper_decoder_->Start(image_data_);
return true;
}
void WallpaperSetWallpaperFunction::OnWallpaperDecoded(
const gfx::ImageSkia& wallpaper) {
wallpaper_ = wallpaper;
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&WallpaperSetWallpaperFunction::SaveToFile,
this));
}
void WallpaperSetWallpaperFunction::OnFail() {
wallpaper_decoder_ = NULL;
SendResponse(false);
}
void WallpaperSetWallpaperFunction::SaveToFile() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
FilePath wallpaper_dir;
CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir));
if (!file_util::DirectoryExists(wallpaper_dir) &&
!file_util::CreateDirectory(wallpaper_dir)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperSetWallpaperFunction::OnFail,
this));
return;
}
FilePath file_path = wallpaper_dir.Append(file_name_);
if (file_util::PathExists(file_path) ||
file_util::WriteFile(file_path, image_data_.c_str(),
image_data_.size()) != -1 ) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperSetWallpaperFunction::SetDecodedWallpaper,
this));
} else {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&WallpaperSetWallpaperFunction::OnFail,
this));
}
}
void WallpaperSetWallpaperFunction::SetDecodedWallpaper() {
chromeos::WallpaperManager* wallpaper_manager =
chromeos::WallpaperManager::Get();
wallpaper_manager->SetWallpaperFromImageSkia(wallpaper_, layout_);
bool is_persistent =
!chromeos::UserManager::Get()->IsCurrentUserEphemeral();
chromeos::WallpaperInfo info = {
file_name_,
layout_,
chromeos::User::ONLINE,
base::Time::Now().LocalMidnight()
};
wallpaper_manager->SetUserWallpaperInfo(email_, info, is_persistent);
wallpaper_decoder_ = NULL;
SendResponse(true);
}
|