OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_manager_api.h" | 5 #include "chrome/browser/chromeos/extensions/wallpaper_manager_api.h" |
6 | 6 |
7 #include "ash/shell.h" | |
8 #include "ash/shell_window_ids.h" | |
7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
8 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/json/json_writer.h" | |
9 #include "base/path_service.h" | 12 #include "base/path_service.h" |
10 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/chromeos/login/user_manager.h" | 14 #include "chrome/browser/chromeos/login/user_manager.h" |
15 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | |
16 #include "chrome/browser/extensions/extension_event_router.h" | |
12 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
15 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_finder.h" | 21 #include "chrome/browser/ui/browser_finder.h" |
22 #include "chrome/browser/ui/browser_list.h" | |
17 #include "chrome/browser/ui/chrome_pages.h" | 23 #include "chrome/browser/ui/chrome_pages.h" |
18 #include "chrome/browser/ui/extensions/application_launch.h" | 24 #include "chrome/browser/ui/extensions/application_launch.h" |
25 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
19 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
20 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
22 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
30 #include "net/url_request/url_fetcher.h" | |
31 #include "net/url_request/url_fetcher_delegate.h" | |
32 #include "net/url_request/url_request_status.h" | |
33 #include "grit/generated_resources.h" | |
34 #include "ui/aura/root_window.h" | |
35 #include "ui/aura/window.h" | |
36 #include "ui/base/l10n/l10n_util.h" | |
23 | 37 |
24 using content::BrowserThread; | 38 using content::BrowserThread; |
25 | 39 |
26 const char kWallpaperManagerDomain[] = "obklkkbkpaoaejdabbfldmcfplpdgolj"; | 40 const char kWallpaperManagerDomain[] = "obklkkbkpaoaejdabbfldmcfplpdgolj"; |
27 | 41 |
42 namespace events { | |
43 | |
44 const char kDownloadProgressEvent[] = | |
45 "experimental.wallpaperManager.onDownloadProgress"; | |
46 const char kDownloadDoneEvent[] = | |
47 "experimental.wallpaperManager.onDownloadDone"; | |
48 const char kDownloadErrorEvent[] = | |
49 "experimental.wallpaperManager.onDownloadError"; | |
50 | |
51 } // namespace events | |
52 | |
28 namespace wallpaper_manager_util { | 53 namespace wallpaper_manager_util { |
29 | 54 |
30 void OpenWallpaperManager() { | 55 void OpenWallpaperManager() { |
31 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 56 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
32 // Hides the new UI container behind a flag. | 57 // Hides the new UI container behind a flag. |
33 if (CommandLine::ForCurrentProcess()->HasSwitch( | 58 if (CommandLine::ForCurrentProcess()->HasSwitch( |
34 switches::kExperimentalWallpaperUI)) { | 59 switches::kExperimentalWallpaperUI)) { |
35 std::string url = chrome::kChromeUIWallpaperURL; | 60 std::string url = chrome::kChromeUIWallpaperURL; |
36 ExtensionService* service = profile->GetExtensionService(); | 61 ExtensionService* service = profile->GetExtensionService(); |
37 if (!service) | 62 if (!service) |
38 return; | 63 return; |
39 | 64 |
40 const extensions::Extension* extension = | 65 const extensions::Extension* extension = |
41 service->GetExtensionById(kWallpaperManagerDomain, false); | 66 service->GetExtensionById(kWallpaperManagerDomain, false); |
42 if (!extension) | 67 if (!extension) |
43 return; | 68 return; |
44 | 69 |
45 application_launch::OpenApplication(profile, extension, | 70 application_launch::OpenApplication(profile, extension, |
46 extension_misc::LAUNCH_WINDOW, GURL(url), NEW_FOREGROUND_TAB, NULL); | 71 extension_misc::LAUNCH_WINDOW, GURL(url), NEW_FOREGROUND_TAB, NULL); |
47 } else { | 72 } else { |
48 Browser* browser = browser::FindOrCreateTabbedBrowser( | 73 Browser* browser = browser::FindOrCreateTabbedBrowser( |
49 ProfileManager::GetDefaultProfileOrOffTheRecord()); | 74 ProfileManager::GetDefaultProfileOrOffTheRecord()); |
50 chrome::ShowSettingsSubPage(browser, "setWallpaper"); | 75 chrome::ShowSettingsSubPage(browser, "setWallpaper"); |
51 } | 76 } |
52 } | 77 } |
53 | 78 |
54 } // namespace wallpaper_manager_util | 79 } //namespace wallpaper_manager_util |
80 | |
81 bool WallpaperManagerStringsFunction::RunImpl() { | |
82 result_.reset(new DictionaryValue()); | |
83 DictionaryValue* dict = reinterpret_cast<DictionaryValue*>(result_.get()); | |
flackr
2012/07/12 19:26:12
Perhaps swap the order of these and result_.reset(
bshe
2012/07/13 19:28:57
Done. Seems result_ is removed and SetResult is us
| |
84 | |
85 #define SET_STRING(ns, id) \ | |
86 dict->SetString(#id, l10n_util::GetStringUTF16(ns##_##id)) | |
87 SET_STRING(IDS_WALLPAPER_MANAGER, SEARCH_TEXT_LABEL); | |
88 SET_STRING(IDS_WALLPAPER_MANAGER, AUTHOR_LABEL); | |
89 SET_STRING(IDS_WALLPAPER_MANAGER, CUSTOM_CATEGORY_LABEL); | |
90 SET_STRING(IDS_WALLPAPER_MANAGER, SELECT_CUSTOM_LABEL); | |
91 SET_STRING(IDS_WALLPAPER_MANAGER, POSITION_LABEL); | |
92 SET_STRING(IDS_WALLPAPER_MANAGER, COLOR_LABEL); | |
93 SET_STRING(IDS_WALLPAPER_MANAGER, PREVIEW_LABEL); | |
94 SET_STRING(IDS_OPTIONS, SET_WALLPAPER_DAILY); | |
95 #undef SET_STRING | |
96 | |
97 ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict); | |
98 | |
99 return true; | |
100 } | |
101 | |
102 WallpaperManagerSetWallpaperFunction::WallpaperFetcher::WallpaperFetcher( | |
103 scoped_refptr<WallpaperManagerSetWallpaperFunction> function) | |
104 : function_(function) { | |
105 } | |
106 | |
107 void WallpaperManagerSetWallpaperFunction::WallpaperFetcher::Start( | |
108 const GURL& wallpaper_url, const FilePath& file_path) { | |
109 fetcher_.reset(net::URLFetcher::Create(wallpaper_url, | |
110 net::URLFetcher::GET, | |
111 this)); | |
112 fetcher_->SetRequestContext( | |
113 g_browser_process->system_request_context()); | |
114 | |
115 fetcher_->SaveResponseToFileAtPath( | |
116 file_path, | |
117 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | |
118 fetcher_->Start(); | |
119 } | |
120 | |
121 void WallpaperManagerSetWallpaperFunction::WallpaperFetcher::OnURLFetchComplete( | |
122 const net::URLFetcher* source) { | |
123 function_->OnDownloadComplete(source); | |
124 } | |
125 | |
126 void WallpaperManagerSetWallpaperFunction::WallpaperFetcher | |
127 ::OnURLFetchDownloadProgress(const net::URLFetcher* source, | |
128 int64 current, | |
129 int64 total) { | |
130 if (source == fetcher_.get()) | |
131 function_->OnDownloadProgress(source, current, total); | |
132 } | |
133 | |
134 WallpaperManagerSetWallpaperFunction::~WallpaperManagerSetWallpaperFunction() { | |
135 } | |
136 | |
137 bool WallpaperManagerSetWallpaperFunction::RunImpl() { | |
138 // First param is url of the selected wallpaper. | |
139 std::string url; | |
140 if (!args_->GetString(0, &url) || url.empty()) | |
141 return false; | |
142 // Second param is the default layout of the selected wallpaper. | |
143 std::string layout_string; | |
144 if (!args_->GetString(1, &layout_string) || layout_string.empty()) { | |
145 return false; | |
146 } | |
147 layout_ = ash::GetLayoutFromString(layout_string); | |
148 | |
149 // Gets email address while at UI thread. | |
150 email_ = chromeos::UserManager::Get()->GetLoggedInUser().email(); | |
151 | |
152 BrowserThread::PostTask( | |
153 BrowserThread::FILE, FROM_HERE, | |
154 base::Bind( | |
155 &WallpaperManagerSetWallpaperFunction::RequestOnFileThread, | |
156 this, url)); | |
157 // Will finish asynchronously. | |
158 return true; | |
159 } | |
160 | |
161 void WallpaperManagerSetWallpaperFunction::OnDownloadComplete( | |
162 const net::URLFetcher* source) { | |
163 WallpaperErrorCode code = GetErrorCode(source); | |
164 if (code != HTTP_SUCCESS) { | |
165 DispatchErrorEvent(code); | |
166 return; | |
167 } | |
168 FilePath temp_file; | |
169 if (code == HTTP_SUCCESS && | |
170 !source->GetResponseAsFilePath(true, &temp_file)) { | |
171 code = WALLPAPER_FILE_ERROR; | |
172 DispatchErrorEvent(code); | |
173 return; | |
174 } | |
175 | |
176 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
177 std::string(kWallpaperManagerDomain), | |
178 events::kDownloadDoneEvent, | |
179 "[]", profile_, GURL()); | |
180 | |
181 FilePath path = wallpaper_dir_.Append( | |
182 source->GetOriginalURL().ExtractFileName() + ".jpg"); | |
flackr
2012/07/12 19:26:12
Can we copy the extension from the source file rat
bshe
2012/07/13 19:28:57
Removed the extension name since it's unnecessary
| |
183 | |
184 chromeos::WallpaperManager::Get()->SetWallpaperFromFile(email_, | |
185 path.value(), | |
186 layout_); | |
187 } | |
188 | |
189 void WallpaperManagerSetWallpaperFunction::OnDownloadProgress( | |
190 const net::URLFetcher* source, | |
191 int64 current, | |
192 int64 total) { | |
193 if (current >= bytes_wallpaper_download_progress_last_reported_ + | |
194 kBytesWallpaperDownloadProgressReportInterval) { | |
195 bytes_wallpaper_download_progress_last_reported_ = current; | |
196 base::TimeDelta time_remaining; | |
197 if (current > 0) { | |
198 const base::TimeDelta diff = | |
199 base::TimeTicks::Now() - tick_wallpaper_download_start_; | |
200 time_remaining = diff*(total - current)/current; | |
201 | |
202 DictionaryValue* dict = new DictionaryValue(); | |
203 dict->SetInteger("progress", time_remaining.InMilliseconds()); | |
204 ListValue args; | |
205 args.Append(dict); | |
206 std::string json_args; | |
207 base::JSONWriter::Write(&args, &json_args); | |
208 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
209 std::string(kWallpaperManagerDomain), | |
210 events::kDownloadProgressEvent, | |
211 json_args, profile_, GURL()); | |
212 } | |
213 } | |
214 } | |
215 | |
216 void WallpaperManagerSetWallpaperFunction::RequestOnFileThread( | |
217 const std::string& source_url) { | |
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
219 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir_)); | |
220 | |
221 GURL wallpaper_url(source_url); | |
222 if (!file_util::DirectoryExists(wallpaper_dir_) && | |
223 !file_util::CreateDirectory(wallpaper_dir_)) { | |
224 DispatchErrorEvent(WALLPAPER_FILE_ERROR); | |
225 return; | |
226 } | |
227 | |
228 FilePath file_path = wallpaper_dir_.Append(wallpaper_url.ExtractFileName() + | |
229 ".jpg"); | |
230 | |
231 // If the wallpaper already downloaded, uses it directly. | |
flackr
2012/07/12 19:26:12
// If the wallpaper was already downloaded it, use
bshe
2012/07/13 19:28:57
Done.
| |
232 if (file_util::PathExists(file_path)) { | |
233 chromeos::WallpaperManager::Get()->SetWallpaperFromFile(email_, | |
234 file_path.value(), | |
235 layout_); | |
236 return; | |
237 } | |
238 | |
239 BrowserThread::PostTask( | |
240 BrowserThread::UI, FROM_HERE, | |
241 base::Bind( | |
242 &WallpaperManagerSetWallpaperFunction::SetupFetcherOnUIThread, | |
243 this, | |
244 wallpaper_url)); | |
245 } | |
246 | |
247 void WallpaperManagerSetWallpaperFunction::SetupFetcherOnUIThread( | |
248 const GURL& wallpaper_url) { | |
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
250 tick_wallpaper_download_start_ = base::TimeTicks::Now(); | |
251 bytes_wallpaper_download_progress_last_reported_ = 0; | |
252 | |
253 // If the wallpaper already downloaded, uses it directly. | |
flackr
2012/07/12 19:26:12
This comment doesn't seem to correspond to the cod
bshe
2012/07/13 19:28:57
Removed.
| |
254 FilePath file_path = wallpaper_dir_.Append(wallpaper_url.ExtractFileName() + | |
255 ".jpg"); | |
256 | |
257 fetcher_.reset(new WallpaperFetcher(this)); | |
258 fetcher_->Start(wallpaper_url, file_path); | |
259 } | |
260 | |
261 WallpaperErrorCode WallpaperManagerSetWallpaperFunction::GetErrorCode( | |
262 const net::URLFetcher* source) const { | |
263 WallpaperErrorCode code = static_cast<WallpaperErrorCode>( | |
264 source->GetResponseCode()); | |
265 if (code == HTTP_SUCCESS && !source->GetStatus().is_success()) { | |
266 // If the HTTP response code is SUCCESS yet the URL request failed, it is | |
267 // likely that the failure is due to loss of connection. | |
268 code = WALLPAPER_NO_CONNECTION; | |
269 } | |
270 return code; | |
271 } | |
272 | |
273 void WallpaperManagerSetWallpaperFunction::DispatchErrorEvent( | |
274 WallpaperErrorCode code) { | |
flackr
2012/07/12 19:26:12
Are you eventually going to do something with this
bshe
2012/07/13 19:28:57
Yes. I am going to listen to the dispatched event
| |
275 DictionaryValue* dict = new DictionaryValue(); | |
276 dict->SetString("message", | |
277 l10n_util::GetStringUTF16(IDS_WALLPAPER_MANAGER_DOWNLOAD_ERROR_MESSAGE)); | |
278 ListValue args; | |
279 args.Append(dict); | |
280 std::string json_args; | |
281 base::JSONWriter::Write(&args, &json_args); | |
282 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
283 std::string(kWallpaperManagerDomain), | |
284 events::kDownloadErrorEvent, | |
285 json_args, profile_, GURL()); | |
286 } | |
287 | |
288 bool WallpaperManagerSetWallpaperFunction::CreateDirectory( | |
289 const FilePath& path) { | |
290 bool success = false; | |
291 if (!file_util::DirectoryExists(path)) | |
flackr
2012/07/12 19:26:12
You already check this before calling CreateDirect
bshe
2012/07/13 19:28:57
You are absolutely right. This function actually w
| |
292 success = file_util::CreateDirectory(path); | |
293 else | |
294 success = true; | |
295 return success; | |
296 } | |
OLD | NEW |