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

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

Issue 10754014: Wallpaper manager backend APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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
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/desktop_background/desktop_background_resources.h"
8 #include "ash/shell.h"
9 #include "ash/shell_window_ids.h"
7 #include "base/command_line.h" 10 #include "base/command_line.h"
8 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/json/json_writer.h"
9 #include "base/path_service.h" 13 #include "base/path_service.h"
10 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/login/user_manager.h" 15 #include "chrome/browser/chromeos/login/user_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 const char kDownloadProgressEvent[] = "experimental.wallpaperManager.onDownloa dProgress";
44 const char kDownloadDoneEvent[] = "experimental.wallpaperManager.onDownloadDon e";
45 const char kDownloadErrorEvent[] = "experimental.wallpaperManager.onDownloadEr ror";
46 } // namespace events
47
28 namespace wallpaper_manager_util { 48 namespace wallpaper_manager_util {
29 49
30 void OpenWallpaperManager() { 50 void OpenWallpaperManager() {
31 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 51 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
32 // Hides the new UI container behind a flag. 52 // Hides the new UI container behind a flag.
33 if (CommandLine::ForCurrentProcess()->HasSwitch( 53 if (CommandLine::ForCurrentProcess()->HasSwitch(
34 switches::kExperimentalWallpaperUI)) { 54 switches::kExperimentalWallpaperUI)) {
35 std::string url = chrome::kChromeUIWallpaperURL; 55 std::string url = chrome::kChromeUIWallpaperURL;
36 ExtensionService* service = profile->GetExtensionService(); 56 ExtensionService* service = profile->GetExtensionService();
37 if (!service) 57 if (!service)
38 return; 58 return;
39 59
40 const extensions::Extension* extension = 60 const extensions::Extension* extension =
41 service->GetExtensionById(kWallpaperManagerDomain, false); 61 service->GetExtensionById(kWallpaperManagerDomain, false);
42 if (!extension) 62 if (!extension)
43 return; 63 return;
44 64
45 application_launch::OpenApplication(profile, extension, 65 application_launch::OpenApplication(profile, extension,
46 extension_misc::LAUNCH_WINDOW, GURL(url), NEW_FOREGROUND_TAB, NULL); 66 extension_misc::LAUNCH_WINDOW, GURL(url), NEW_FOREGROUND_TAB, NULL);
47 } else { 67 } else {
48 Browser* browser = browser::FindOrCreateTabbedBrowser( 68 Browser* browser = browser::FindOrCreateTabbedBrowser(
49 ProfileManager::GetDefaultProfileOrOffTheRecord()); 69 ProfileManager::GetDefaultProfileOrOffTheRecord());
50 chrome::ShowSettingsSubPage(browser, "setWallpaper"); 70 chrome::ShowSettingsSubPage(browser, "setWallpaper");
51 } 71 }
52 } 72 }
53 73
54 } // namespace wallpaper_manager_util 74 } //namespace wallpaper_manager_util
75
76 bool WallpaperManagerStringsFunction::RunImpl() {
77 result_.reset(new DictionaryValue());
78 DictionaryValue* dict = reinterpret_cast<DictionaryValue*>(result_.get());
79
80 #define SET_STRING(ns, id) \
81 dict->SetString(#id, l10n_util::GetStringUTF16(ns##_##id))
82 SET_STRING(IDS_WALLPAPER_MANAGER, SEARCH_TEXT_LABEL);
83 SET_STRING(IDS_WALLPAPER_MANAGER, AUTHOR_LABEL);
84 SET_STRING(IDS_WALLPAPER_MANAGER, CUSTOM_CATEGORY_LABEL);
85 SET_STRING(IDS_WALLPAPER_MANAGER, SELECT_CUSTOM_LABEL);
86 SET_STRING(IDS_WALLPAPER_MANAGER, POSITION_LABEL);
87 SET_STRING(IDS_WALLPAPER_MANAGER, COLOR_LABEL);
88 SET_STRING(IDS_WALLPAPER_MANAGER, PREVIEW_LABEL);
89 SET_STRING(IDS_OPTIONS, SET_WALLPAPER_DAILY);
90 #undef SET_STRING
91
92 ChromeURLDataManager::DataSource::SetFontAndTextDirection(dict);
93
94 return true;
95 }
96
97 WallpaperManagerSetWallpaperFunction::~WallpaperManagerSetWallpaperFunction() {
98 }
99
100 bool WallpaperManagerSetWallpaperFunction::RunImpl() {
101 // TODO(bshe): Add layout parameter.
102 // First param is url of the selected wallpaper.
103 std::string url;
104 if (!args_->GetString(0, &url) || url.empty())
105 return false;
106
107 BrowserThread::PostTask(
108 BrowserThread::FILE, FROM_HERE,
109 base::Bind(
110 &WallpaperManagerSetWallpaperFunction::RequestOnFileThread,
111 this,
112 url));
113 // Will finish asynchronously.
114 return true;
115 }
116
117 void WallpaperManagerSetWallpaperFunction::OnURLFetchComplete(
118 const net::URLFetcher* source) {
119 WallpaperErrorCode code = GetErrorCode(source);
120 if (code != HTTP_SUCCESS) {
121 DispatchErrorEvent(code);
122 return;
123 }
124 FilePath temp_file;
125 if (code == HTTP_SUCCESS &&
126 !source->GetResponseAsFilePath(true, &temp_file)) {
127 code = WALLPAPER_FILE_ERROR;
128 DispatchErrorEvent(code);
129 return;
130 }
131
132 profile_->GetExtensionEventRouter()->DispatchEventToExtension(
133 std::string(kWallpaperManagerDomain),
134 events::kDownloadDoneEvent,
135 "[]", profile_, GURL());
136
137 FilePath path = wallpaper_dir_.Append(
138 source->GetOriginalURL().ExtractFileName() + ".jpg");
139
140 // TODO(bshe): Call function to set wallpaper to the image which file_path
141 // points to. And save the file_path and layout to local state.
142 NOTIMPLEMENTED();
143 this->Release();
144 }
145
146 void WallpaperManagerSetWallpaperFunction::OnURLFetchDownloadProgress(
147 const net::URLFetcher* source,
148 int64 current,
149 int64 total) {
150 if (source == fetcher_.get()) {
151 if (current >= bytes_wallpaper_download_progress_last_reported_ +
152 kBytesWallpaperDownloadProgressReportInterval) {
153 bytes_wallpaper_download_progress_last_reported_ = current;
154 base::TimeDelta time_remaining;
155 if (current > 0) {
156 const base::TimeDelta diff =
157 base::TimeTicks::Now() - tick_wallpaper_download_start_;
158 time_remaining = diff*(total - current)/current;
159
160 DictionaryValue* dict = new DictionaryValue();
161 dict->SetInteger("progress", time_remaining.InMilliseconds());
162 ListValue args;
163 args.Append(dict);
164 std::string json_args;
165 base::JSONWriter::Write(&args, &json_args);
166 profile_->GetExtensionEventRouter()->DispatchEventToExtension(
167 std::string(kWallpaperManagerDomain),
168 events::kDownloadProgressEvent,
169 json_args, profile_, GURL());
170 }
171 }
172 }
173 }
174
175 void WallpaperManagerSetWallpaperFunction::RequestOnFileThread(
176 const std::string& source_url) {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
178 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir_));
179
180 GURL wallpaper_url(source_url);
181 if (!file_util::DirectoryExists(wallpaper_dir_) &&
182 !file_util::CreateDirectory(wallpaper_dir_)) {
183 DispatchErrorEvent(WALLPAPER_FILE_ERROR);
184 return;
185 }
186
187 FilePath file_path = wallpaper_dir_.Append(wallpaper_url.ExtractFileName() +
188 ".jpg");
189
190 // If the wallpaper already downloaded, uses it directly.
191 if (file_util::PathExists(file_path)) {
192 // TODO(bshe): Call function to set wallpaper to the image which file_path
193 // points to. And save the file_path and layout to local state.
194 NOTIMPLEMENTED();
195 return;
196 }
197
198 BrowserThread::PostTask(
199 BrowserThread::UI, FROM_HERE,
200 base::Bind(
201 &WallpaperManagerSetWallpaperFunction::SetupFetcherOnUIThread,
202 this,
203 wallpaper_url));
204 }
205
206 void WallpaperManagerSetWallpaperFunction::SetupFetcherOnUIThread(
207 const GURL& wallpaper_url) {
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
209 tick_wallpaper_download_start_ = base::TimeTicks::Now();
210 bytes_wallpaper_download_progress_last_reported_ = 0;
211
212 // If the wallpaper already downloaded, uses it directly.
213 FilePath file_path = wallpaper_dir_.Append(wallpaper_url.ExtractFileName() +
214 ".jpg");
215
216 this->AddRef();
bshe 2012/07/10 02:03:58 Here I use a hack to make it work. Do you have any
217 fetcher_.reset(net::URLFetcher::Create(wallpaper_url,
218 net::URLFetcher::GET,
219 this));
220 fetcher_->SetRequestContext(
221 g_browser_process->system_request_context());
222
223 fetcher_->SaveResponseToFileAtPath(
224 file_path,
225 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
226 fetcher_->Start();
227 }
228
229 WallpaperErrorCode WallpaperManagerSetWallpaperFunction::GetErrorCode(
230 const net::URLFetcher* source) const {
231 WallpaperErrorCode code = static_cast<WallpaperErrorCode>(
232 source->GetResponseCode());
233 if (code == HTTP_SUCCESS && !source->GetStatus().is_success()) {
234 // If the HTTP response code is SUCCESS yet the URL request failed, it is
235 // likely that the failure is due to loss of connection.
236 code = WALLPAPER_NO_CONNECTION;
237 }
238 return code;
239 }
240
241 void WallpaperManagerSetWallpaperFunction::DispatchErrorEvent(
242 WallpaperErrorCode code) {
243 DictionaryValue* dict = new DictionaryValue();
244 dict->SetString("message",
245 l10n_util::GetStringUTF16(IDS_WALLPAPER_MANAGER_SEARCH_TEXT_LABEL));
246 ListValue args;
247 args.Append(dict);
248 std::string json_args;
249 base::JSONWriter::Write(&args, &json_args);
250 profile_->GetExtensionEventRouter()->DispatchEventToExtension(
251 std::string(kWallpaperManagerDomain),
252 events::kDownloadErrorEvent,
253 json_args, profile_, GURL());
254 }
255
256 bool WallpaperManagerSetWallpaperFunction::CreateDirectory(const FilePath& path) {
257 bool success = false;
258 if (!file_util::DirectoryExists(path))
259 success = file_util::CreateDirectory(path);
260 else
261 success = true;
262 return success;
263 }
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/extensions/wallpaper_manager_api.h ('k') | chrome/browser/extensions/extension_function_registry.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698