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

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

Issue 7635017: Fix saved screenshots for feedback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Style fix. Created 9 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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"
8 #include "base/callback.h"
7 #include "base/file_util.h" 9 #include "base/file_util.h"
8 #include "base/memory/ref_counted_memory.h" 10 #include "base/memory/ref_counted_memory.h"
9 #include "base/path_service.h" 11 #include "base/path_service.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/task.h" 12 #include "base/task.h"
12 #include "chrome/common/chrome_paths.h" 13 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/url_constants.h" 14 #include "chrome/common/url_constants.h"
14 #include "content/browser/browser_thread.h" 15 #include "content/browser/browser_thread.h"
15 16
16 static const char kCurrentScreenshot[] = "current"; 17 static const char kCurrentScreenshot[] = "current";
Daniel Erat 2011/08/22 16:42:02 nit: it'd be better to give this variable and kSav
rkc 2011/08/23 10:25:20 Done.
17 #if defined(OS_CHROMEOS) 18 #if defined(OS_CHROMEOS)
18 static const char kSavedScreenshots[] = "saved/"; 19 static const char kSavedScreenshots[] = "saved/";
19 #endif 20 #endif
20 21
21 static const char kScreenshotsRelativePath[] = "/Screenshots/"; 22 void ScreenshotSource::CacheAndSendScreenshot(const std::string& path,
23 int request_id,
24 ScreenshotDataPtr bytes) {
25 cached_screenshots_[path] = bytes;
26 SendResponse(request_id, new RefCountedBytes(*bytes));
27 }
28
29 ScreenshotDataPtr ScreenshotSource::GetCachedScreenshot(
30 const std::string& full_path) {
Daniel Erat 2011/08/22 16:42:02 make the parameter name match the name from the he
rkc 2011/08/23 10:25:20 Changed header to full_path. Done.
31 std::string path = full_path.substr(0, full_path.find_first_of("?"));
32 if (cached_screenshots_.find(path) != cached_screenshots_.end()) {
Daniel Erat 2011/08/22 16:42:02 nit: it's a better practice to save find()'s retur
rkc 2011/08/23 10:25:20 Done.
33 return cached_screenshots_[path];
34 } else {
35 return ScreenshotDataPtr(new ScreenshotData);
36 }
37 }
22 38
23 #if defined(OS_CHROMEOS) 39 #if defined(OS_CHROMEOS)
24 // Read the file from the screenshots directory into the read_bytes vector. 40 void ScreenshotSource::SendSavedScreenshot(const std::string& path,
Daniel Erat 2011/08/22 16:42:02 make function definition order match the declarati
rkc 2011/08/23 10:25:20 Done.
25 void ReadScreenshot(const std::string& filename, 41 int request_id) {
26 std::vector<unsigned char>* read_bytes, 42 ScreenshotDataPtr read_bytes(new ScreenshotData);
27 base::WaitableEvent* read_complete) { 43 std::string filename = path.substr(strlen(kSavedScreenshots));
28 read_bytes->clear();
29 44
30 FilePath fileshelf_path; 45 FilePath fileshelf_path;
31 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &fileshelf_path)) { 46 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &fileshelf_path)) {
32 read_complete->Signal(); 47 CacheAndSendScreenshot(path, request_id, read_bytes);
33 return; 48 return;
34 } 49 }
35 50
36 FilePath file(fileshelf_path.value() + std::string(kScreenshotsRelativePath) +
37 filename);
38
39 int64 file_size = 0; 51 int64 file_size = 0;
52 FilePath file = fileshelf_path.Append(filename);
40 if (!file_util::GetFileSize(file, &file_size)) { 53 if (!file_util::GetFileSize(file, &file_size)) {
41 read_complete->Signal(); 54 CacheAndSendScreenshot(path, request_id, read_bytes);
42 return; 55 return;
43 } 56 }
44 57
45 // expand vector to file size
46 read_bytes->resize(file_size); 58 read_bytes->resize(file_size);
47 // read file into the vector 59 if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes->front()),
48 int bytes_read = 0; 60 static_cast<int>(file_size)))
49 if (!(bytes_read = file_util::ReadFile(file,
50 reinterpret_cast<char*>(
51 &read_bytes->front()),
52 static_cast<int>(file_size))))
53 read_bytes->clear(); 61 read_bytes->clear();
Daniel Erat 2011/08/22 16:42:02 shouldn't you abort without sending the screenshot
rkc 2011/08/23 10:25:20 This accomplishes the same thing, while avoiding t
54 62
55 // We're done, if successful, read_bytes will have the data 63 CacheAndSendScreenshot(path, request_id, read_bytes);
56 // otherwise, it'll be empty.
57 read_complete->Signal();
58 }
59
60 // Get a saved screenshot - read on the FILE thread.
61 std::vector<unsigned char> GetSavedScreenshot(std::string filename) {
62 base::WaitableEvent read_complete(true, false);
63 std::vector<unsigned char> bytes;
64 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
65 NewRunnableFunction(&ReadScreenshot, filename,
66 &bytes, &read_complete));
67 read_complete.Wait();
68 return bytes;
69 } 64 }
70 #endif 65 #endif
71 66
72 std::vector<unsigned char> ScreenshotSource::GetScreenshot( 67 void ScreenshotSource::SendScreenshot(const std::string& full_path,
73 const std::string& full_path) { 68 int request_id) {
74 // Strip the query param value - we only use it as a hack to ensure our 69 // Strip the query param value - we only use it as a hack to ensure our
75 // image gets reloaded instead of being pulled from the browser cache 70 // image gets reloaded instead of being pulled from the browser cache
76 std::string path = full_path.substr(0, full_path.find_first_of("?")); 71 std::string path = full_path.substr(0, full_path.find_first_of("?"));
77 if (path == kCurrentScreenshot) { 72 if (path == kCurrentScreenshot) {
78 return current_screenshot_; 73 CacheAndSendScreenshot(path, request_id, current_screenshot_);
79 #if defined(OS_CHROMEOS) 74 #if defined(OS_CHROMEOS)
80 } else if (path.compare(0, strlen(kSavedScreenshots), 75 } else if (path.compare(0, strlen(kSavedScreenshots),
81 kSavedScreenshots) == 0) { 76 kSavedScreenshots) == 0) {
82 // Split the saved screenshot filename from the path 77 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
83 std::string filename = path.substr(strlen(kSavedScreenshots)); 78 base::Bind(&ScreenshotSource::SendSavedScreenshot,
84 79 base::Unretained(this), path,
85 return GetSavedScreenshot(filename); 80 request_id));
86 #endif 81 #endif
87 } else { 82 } else {
88 std::vector<unsigned char> ret; 83 CacheAndSendScreenshot(
89 // TODO(rkc): Weird vc bug, return std::vector<unsigned char>() causes 84 path, request_id, ScreenshotDataPtr(new ScreenshotData()));
90 // the object assigned to the return value of this function magically
91 // change it's address 0x0; look into this eventually.
92 return ret;
93 } 85 }
94 } 86 }
95 87
96 ScreenshotSource::ScreenshotSource( 88 ScreenshotSource::ScreenshotSource(
97 std::vector<unsigned char>* current_screenshot) 89 std::vector<unsigned char>* current_screenshot)
98 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()) { 90 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()) {
99 // Setup the last screenshot taken. 91 // Setup the last screenshot taken.
100 if (current_screenshot) 92 if (current_screenshot)
101 current_screenshot_ = *current_screenshot; 93 current_screenshot_.reset(new ScreenshotData(*current_screenshot));
102 else 94 else
103 current_screenshot_.clear(); 95 current_screenshot_.reset(new ScreenshotData());
104 } 96 }
105 97
106 ScreenshotSource::~ScreenshotSource() {} 98 ScreenshotSource::~ScreenshotSource() {}
107 99
108 void ScreenshotSource::StartDataRequest(const std::string& path, 100 void ScreenshotSource::StartDataRequest(const std::string& path, bool,
109 bool is_incognito, 101 int request_id) {
110 int request_id) { 102 SendScreenshot(path, request_id);
111 SendResponse(request_id, new RefCountedBytes(GetScreenshot(path)));
112 } 103 }
113 104
114 std::string ScreenshotSource::GetMimeType(const std::string&) const { 105 std::string ScreenshotSource::GetMimeType(const std::string&) const {
115 // We need to explicitly return a mime type, otherwise if the user tries to 106 // We need to explicitly return a mime type, otherwise if the user tries to
116 // drag the image they get no extension. 107 // drag the image they get no extension.
117 return "image/png"; 108 return "image/png";
118 } 109 }
OLDNEW
« chrome/browser/ui/webui/screenshot_source.h ('K') | « chrome/browser/ui/webui/screenshot_source.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698