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

Side by Side Diff: chrome/browser/ui/webui/screenshot_source.cc

Issue 11881055: Simplify WebUI data sources. Currently WebUI data sources implement a URLDataSourceDelegate interfa… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix issue in about_ui exposed by cros tests Created 7 years, 11 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/ui/webui/screenshot_source.h" 5 #include "chrome/browser/ui/webui/screenshot_source.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/i18n/time_formatting.h" 11 #include "base/i18n/time_formatting.h"
12 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
13 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/string16.h" 15 #include "base/string16.h"
16 #include "base/stringprintf.h" 16 #include "base/stringprintf.h"
17 #include "base/string_util.h" 17 #include "base/string_util.h"
18 #include "chrome/browser/browser_process.h" 18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/download/download_prefs.h" 19 #include "chrome/browser/download/download_prefs.h"
20 #include "chrome/browser/prefs/pref_service.h" 20 #include "chrome/browser/prefs/pref_service.h"
21 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_manager.h" 22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
24 #include "chrome/common/chrome_paths.h" 23 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h" 25 #include "chrome/common/url_constants.h"
27 #include "googleurl/src/url_canon.h" 26 #include "googleurl/src/url_canon.h"
28 #include "googleurl/src/url_util.h" 27 #include "googleurl/src/url_util.h"
29 28
30 #if defined(USE_ASH) 29 #if defined(USE_ASH)
31 #include "ash/shell.h" 30 #include "ash/shell.h"
32 #include "ash/shell_delegate.h" 31 #include "ash/shell_delegate.h"
33 #endif 32 #endif
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 } 136 }
138 return true; 137 return true;
139 } 138 }
140 139
141 #endif 140 #endif
142 141
143 std::string ScreenshotSource::GetSource() { 142 std::string ScreenshotSource::GetSource() {
144 return chrome::kChromeUIScreenshotPath; 143 return chrome::kChromeUIScreenshotPath;
145 } 144 }
146 145
147 void ScreenshotSource::StartDataRequest(const std::string& path, bool, 146 void ScreenshotSource::StartDataRequest(
148 int request_id) { 147 const std::string& path,
149 SendScreenshot(path, request_id); 148 bool is_incognito,
149 const content::URLDataSource::GotDataCallback& callback) {
150 SendScreenshot(path, callback);
150 } 151 }
151 152
152 std::string ScreenshotSource::GetMimeType(const std::string&) const { 153 std::string ScreenshotSource::GetMimeType(const std::string&) const {
153 // We need to explicitly return a mime type, otherwise if the user tries to 154 // We need to explicitly return a mime type, otherwise if the user tries to
154 // drag the image they get no extension. 155 // drag the image they get no extension.
155 return "image/png"; 156 return "image/png";
156 } 157 }
157 158
158 ScreenshotDataPtr ScreenshotSource::GetCachedScreenshot( 159 ScreenshotDataPtr ScreenshotSource::GetCachedScreenshot(
159 const std::string& screenshot_path) { 160 const std::string& screenshot_path) {
160 std::map<std::string, ScreenshotDataPtr>::iterator pos; 161 std::map<std::string, ScreenshotDataPtr>::iterator pos;
161 std::string path = screenshot_path.substr( 162 std::string path = screenshot_path.substr(
162 0, screenshot_path.find_first_of("?")); 163 0, screenshot_path.find_first_of("?"));
163 if ((pos = cached_screenshots_.find(path)) != cached_screenshots_.end()) { 164 if ((pos = cached_screenshots_.find(path)) != cached_screenshots_.end()) {
164 return pos->second; 165 return pos->second;
165 } else { 166 } else {
166 return ScreenshotDataPtr(new ScreenshotData); 167 return ScreenshotDataPtr(new ScreenshotData);
167 } 168 }
168 } 169 }
169 170
170 void ScreenshotSource::SendScreenshot(const std::string& screenshot_path, 171 void ScreenshotSource::SendScreenshot(
171 int request_id) { 172 const std::string& screenshot_path,
173 const content::URLDataSource::GotDataCallback& callback) {
172 // Strip the query param value - we only use it as a hack to ensure our 174 // Strip the query param value - we only use it as a hack to ensure our
173 // image gets reloaded instead of being pulled from the browser cache 175 // image gets reloaded instead of being pulled from the browser cache
174 std::string path = screenshot_path.substr( 176 std::string path = screenshot_path.substr(
175 0, screenshot_path.find_first_of("?")); 177 0, screenshot_path.find_first_of("?"));
176 if (path == ScreenshotSource::kScreenshotCurrent) { 178 if (path == ScreenshotSource::kScreenshotCurrent) {
177 CacheAndSendScreenshot(path, request_id, current_screenshot_); 179 CacheAndSendScreenshot(path, callback, current_screenshot_);
178 #if defined(OS_CHROMEOS) 180 #if defined(OS_CHROMEOS)
179 } else if (path.compare(0, strlen(ScreenshotSource::kScreenshotSaved), 181 } else if (path.compare(0, strlen(ScreenshotSource::kScreenshotSaved),
180 ScreenshotSource::kScreenshotSaved) == 0) { 182 ScreenshotSource::kScreenshotSaved) == 0) {
181 using content::BrowserThread; 183 using content::BrowserThread;
182 184
183 std::string filename = 185 std::string filename =
184 path.substr(strlen(ScreenshotSource::kScreenshotSaved)); 186 path.substr(strlen(ScreenshotSource::kScreenshotSaved));
185 187
186 url_canon::RawCanonOutputT<char16> decoded; 188 url_canon::RawCanonOutputT<char16> decoded;
187 url_util::DecodeURLEscapeSequences( 189 url_util::DecodeURLEscapeSequences(
188 filename.data(), filename.size(), &decoded); 190 filename.data(), filename.size(), &decoded);
189 // Screenshot filenames don't use non-ascii characters. 191 // Screenshot filenames don't use non-ascii characters.
190 std::string decoded_filename = UTF16ToASCII(string16( 192 std::string decoded_filename = UTF16ToASCII(string16(
191 decoded.data(), decoded.length())); 193 decoded.data(), decoded.length()));
192 194
193 FilePath download_path; 195 FilePath download_path;
194 GetScreenshotDirectory(&download_path); 196 GetScreenshotDirectory(&download_path);
195 if (drive::util::IsUnderDriveMountPoint(download_path)) { 197 if (drive::util::IsUnderDriveMountPoint(download_path)) {
196 drive::DriveFileSystemInterface* file_system = 198 drive::DriveFileSystemInterface* file_system =
197 drive::DriveSystemServiceFactory::GetForProfile( 199 drive::DriveSystemServiceFactory::GetForProfile(
198 profile_)->file_system(); 200 profile_)->file_system();
199 file_system->GetFileByResourceId( 201 file_system->GetFileByResourceId(
200 decoded_filename, 202 decoded_filename,
201 base::Bind(&ScreenshotSource::GetSavedScreenshotCallback, 203 base::Bind(&ScreenshotSource::GetSavedScreenshotCallback,
202 base::Unretained(this), screenshot_path, request_id), 204 base::Unretained(this), screenshot_path, callback),
203 google_apis::GetContentCallback()); 205 google_apis::GetContentCallback());
204 } else { 206 } else {
205 BrowserThread::PostTask( 207 BrowserThread::PostTask(
206 BrowserThread::FILE, FROM_HERE, 208 BrowserThread::FILE, FROM_HERE,
207 base::Bind(&ScreenshotSource::SendSavedScreenshot, 209 base::Bind(&ScreenshotSource::SendSavedScreenshot,
208 base::Unretained(this), 210 base::Unretained(this),
209 screenshot_path, 211 screenshot_path,
210 request_id, download_path.Append(decoded_filename))); 212 callback, download_path.Append(decoded_filename)));
211 } 213 }
212 #endif 214 #endif
213 } else { 215 } else {
214 CacheAndSendScreenshot( 216 CacheAndSendScreenshot(
215 path, request_id, ScreenshotDataPtr(new ScreenshotData())); 217 path, callback, ScreenshotDataPtr(new ScreenshotData()));
216 } 218 }
217 } 219 }
218 220
219 #if defined(OS_CHROMEOS) 221 #if defined(OS_CHROMEOS)
220 void ScreenshotSource::SendSavedScreenshot( 222 void ScreenshotSource::SendSavedScreenshot(
221 const std::string& screenshot_path, 223 const std::string& screenshot_path,
222 int request_id, 224 const content::URLDataSource::GotDataCallback& callback,
223 const FilePath& file) { 225 const FilePath& file) {
224 ScreenshotDataPtr read_bytes(new ScreenshotData); 226 ScreenshotDataPtr read_bytes(new ScreenshotData);
225 int64 file_size = 0; 227 int64 file_size = 0;
226 228
227 if (!file_util::GetFileSize(file, &file_size)) { 229 if (!file_util::GetFileSize(file, &file_size)) {
228 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); 230 CacheAndSendScreenshot(screenshot_path, callback, read_bytes);
229 return; 231 return;
230 } 232 }
231 233
232 read_bytes->resize(file_size); 234 read_bytes->resize(file_size);
233 if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes->front()), 235 if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes->front()),
234 static_cast<int>(file_size))) 236 static_cast<int>(file_size)))
235 read_bytes->clear(); 237 read_bytes->clear();
236 238
237 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); 239 CacheAndSendScreenshot(screenshot_path, callback, read_bytes);
238 } 240 }
239 241
240 void ScreenshotSource::GetSavedScreenshotCallback( 242 void ScreenshotSource::GetSavedScreenshotCallback(
241 const std::string& screenshot_path, 243 const std::string& screenshot_path,
242 int request_id, 244 const content::URLDataSource::GotDataCallback& callback,
243 drive::DriveFileError error, 245 drive::DriveFileError error,
244 const FilePath& file, 246 const FilePath& file,
245 const std::string& unused_mime_type, 247 const std::string& unused_mime_type,
246 drive::DriveFileType file_type) { 248 drive::DriveFileType file_type) {
247 if (error != drive::DRIVE_FILE_OK || file_type != drive::REGULAR_FILE) { 249 if (error != drive::DRIVE_FILE_OK || file_type != drive::REGULAR_FILE) {
248 ScreenshotDataPtr read_bytes(new ScreenshotData); 250 ScreenshotDataPtr read_bytes(new ScreenshotData);
249 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); 251 CacheAndSendScreenshot(screenshot_path, callback, read_bytes);
250 return; 252 return;
251 } 253 }
252 254
253 content::BrowserThread::PostTask( 255 content::BrowserThread::PostTask(
254 content::BrowserThread::FILE, FROM_HERE, 256 content::BrowserThread::FILE, FROM_HERE,
255 base::Bind(&ScreenshotSource::SendSavedScreenshot, 257 base::Bind(&ScreenshotSource::SendSavedScreenshot,
256 base::Unretained(this), screenshot_path, request_id, file)); 258 base::Unretained(this), screenshot_path, callback, file));
257 } 259 }
258 #endif 260 #endif
259 261
260 void ScreenshotSource::CacheAndSendScreenshot( 262 void ScreenshotSource::CacheAndSendScreenshot(
261 const std::string& screenshot_path, 263 const std::string& screenshot_path,
262 int request_id, 264 const content::URLDataSource::GotDataCallback& callback,
263 ScreenshotDataPtr bytes) { 265 ScreenshotDataPtr bytes) {
264 // Strip the query from the screenshot path. 266 // Strip the query from the screenshot path.
265 std::string path = screenshot_path.substr( 267 std::string path = screenshot_path.substr(
266 0, screenshot_path.find_first_of("?")); 268 0, screenshot_path.find_first_of("?"));
267 cached_screenshots_[path] = bytes; 269 cached_screenshots_[path] = bytes;
268 url_data_source()->SendResponse( 270 callback.Run(new base::RefCountedBytes(*bytes));
269 request_id, new base::RefCountedBytes(*bytes));
270 } 271 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698