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