Chromium Code Reviews| OLD | NEW |
|---|---|
| 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"; |
| 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, const std::vector<unsigned char> bytes) { | |
|
zel
2011/08/12 13:01:37
const std::vector<unsigned char>& bytes ?
rkc
2011/08/22 13:23:52
Done.
| |
| 24 cached_screenshots_[path] = bytes; | |
| 25 SendResponse(request_id, new RefCountedBytes(bytes)); | |
| 26 } | |
| 27 | |
| 28 std::vector<unsigned char> ScreenshotSource::GetCachedScreenshot( | |
|
zel
2011/08/12 13:01:37
can you avoid data copy here? can't you just retur
rkc
2011/08/22 13:23:52
Done.
| |
| 29 const std::string& full_path) { | |
| 30 std::string path = full_path.substr(0, full_path.find_first_of("?")); | |
| 31 if (cached_screenshots_.find(path) != cached_screenshots_.end()) { | |
| 32 return cached_screenshots_[path]; | |
| 33 } else { | |
| 34 // TODO(rkc): Weird vc bug, return std::vector<unsigned char>() causes | |
| 35 // the object assigned to the return value of this function magically | |
| 36 // change it's address 0x0; look into this eventually. | |
|
Daniel Erat
2011/08/12 14:43:40
nit: "its", not "it's"
rkc
2011/08/22 13:23:52
Done.
| |
| 37 std::vector<unsigned char> ret; | |
| 38 return ret; | |
| 39 } | |
| 40 } | |
| 22 | 41 |
| 23 #if defined(OS_CHROMEOS) | 42 #if defined(OS_CHROMEOS) |
| 24 // Read the file from the screenshots directory into the read_bytes vector. | 43 void ScreenshotSource::SendSavedScreenshot(const std::string& path, |
| 25 void ReadScreenshot(const std::string& filename, | 44 int request_id) { |
| 26 std::vector<unsigned char>* read_bytes, | 45 std::vector<unsigned char> read_bytes; |
| 27 base::WaitableEvent* read_complete) { | 46 std::string filename = path.substr(strlen(kSavedScreenshots)); |
| 28 read_bytes->clear(); | |
| 29 | 47 |
| 30 FilePath fileshelf_path; | 48 FilePath fileshelf_path; |
| 31 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &fileshelf_path)) { | 49 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &fileshelf_path)) { |
| 32 read_complete->Signal(); | 50 CacheAndSendScreenshot(path, request_id, read_bytes); |
| 33 return; | 51 return; |
| 34 } | 52 } |
| 35 | 53 |
| 36 FilePath file(fileshelf_path.value() + std::string(kScreenshotsRelativePath) + | |
| 37 filename); | |
| 38 | |
| 39 int64 file_size = 0; | 54 int64 file_size = 0; |
| 55 FilePath file = fileshelf_path.Append(filename); | |
| 40 if (!file_util::GetFileSize(file, &file_size)) { | 56 if (!file_util::GetFileSize(file, &file_size)) { |
| 41 read_complete->Signal(); | 57 CacheAndSendScreenshot(path, request_id, read_bytes); |
| 42 return; | 58 return; |
| 43 } | 59 } |
| 44 | 60 |
| 45 // expand vector to file size | 61 read_bytes.resize(file_size); |
| 46 read_bytes->resize(file_size); | 62 if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes.front()), |
| 47 // read file into the vector | 63 static_cast<int>(file_size))) |
| 48 int bytes_read = 0; | 64 read_bytes.clear(); |
| 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(); | |
| 54 | 65 |
| 55 // We're done, if successful, read_bytes will have the data | 66 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 } | 67 } |
| 70 #endif | 68 #endif |
| 71 | 69 |
| 72 std::vector<unsigned char> ScreenshotSource::GetScreenshot( | 70 void ScreenshotSource::SendScreenshot(const std::string& full_path, |
| 73 const std::string& full_path) { | 71 int request_id) { |
| 74 // Strip the query param value - we only use it as a hack to ensure our | 72 // 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 | 73 // image gets reloaded instead of being pulled from the browser cache |
| 76 std::string path = full_path.substr(0, full_path.find_first_of("?")); | 74 std::string path = full_path.substr(0, full_path.find_first_of("?")); |
| 77 if (path == kCurrentScreenshot) { | 75 if (path == kCurrentScreenshot) { |
| 78 return current_screenshot_; | 76 CacheAndSendScreenshot(path, request_id, current_screenshot_); |
| 79 #if defined(OS_CHROMEOS) | 77 #if defined(OS_CHROMEOS) |
| 80 } else if (path.compare(0, strlen(kSavedScreenshots), | 78 } else if (path.compare(0, strlen(kSavedScreenshots), |
| 81 kSavedScreenshots) == 0) { | 79 kSavedScreenshots) == 0) { |
| 82 // Split the saved screenshot filename from the path | 80 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 83 std::string filename = path.substr(strlen(kSavedScreenshots)); | 81 base::Bind(&ScreenshotSource::SendSavedScreenshot, |
| 84 | 82 base::Unretained(this), path, |
| 85 return GetSavedScreenshot(filename); | 83 request_id)); |
| 86 #endif | 84 #endif |
| 87 } else { | 85 } else { |
| 88 std::vector<unsigned char> ret; | 86 CacheAndSendScreenshot(path, request_id, std::vector<unsigned char>()); |
| 89 // TODO(rkc): Weird vc bug, return std::vector<unsigned char>() causes | |
| 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 } | 87 } |
| 94 } | 88 } |
| 95 | 89 |
| 96 ScreenshotSource::ScreenshotSource( | 90 ScreenshotSource::ScreenshotSource( |
| 97 std::vector<unsigned char>* current_screenshot) | 91 std::vector<unsigned char>* current_screenshot) |
| 98 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()) { | 92 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()) { |
| 99 // Setup the last screenshot taken. | 93 // Setup the last screenshot taken. |
| 100 if (current_screenshot) | 94 if (current_screenshot) |
| 101 current_screenshot_ = *current_screenshot; | 95 current_screenshot_ = *current_screenshot; |
| 102 else | 96 else |
| 103 current_screenshot_.clear(); | 97 current_screenshot_.clear(); |
| 104 } | 98 } |
| 105 | 99 |
| 106 ScreenshotSource::~ScreenshotSource() {} | 100 ScreenshotSource::~ScreenshotSource() {} |
| 107 | 101 |
| 108 void ScreenshotSource::StartDataRequest(const std::string& path, | 102 void ScreenshotSource::StartDataRequest(const std::string& path, bool, |
| 109 bool is_incognito, | 103 int request_id) { |
| 110 int request_id) { | 104 SendScreenshot(path, request_id); |
| 111 SendResponse(request_id, new RefCountedBytes(GetScreenshot(path))); | |
| 112 } | 105 } |
| 113 | 106 |
| 114 std::string ScreenshotSource::GetMimeType(const std::string&) const { | 107 std::string ScreenshotSource::GetMimeType(const std::string&) const { |
| 115 // We need to explicitly return a mime type, otherwise if the user tries to | 108 // We need to explicitly return a mime type, otherwise if the user tries to |
| 116 // drag the image they get no extension. | 109 // drag the image they get no extension. |
| 117 return "image/png"; | 110 return "image/png"; |
| 118 } | 111 } |
| OLD | NEW |