OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/dom_ui/dom_ui_screenshot_source.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/ref_counted_memory.h" |
| 11 #include "base/task.h" |
| 12 #include "base/thread.h" |
| 13 #include "base/waitable_event.h" |
| 14 #include "chrome/browser/chrome_thread.h" |
| 15 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/url_constants.h" |
| 17 #include "gfx/codec/jpeg_codec.h" |
| 18 #include "googleurl/src/gurl.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 |
| 21 static const char kCurrentScreenshot[] = "current"; |
| 22 #if defined(OS_CHROMEOS) |
| 23 static const char kSavedScreenshots[] = "saved/"; |
| 24 #endif |
| 25 |
| 26 static const char kScreenshotsRelativePath[] = "/Screenshots/"; |
| 27 |
| 28 #if defined(OS_CHROMEOS) |
| 29 // Read the file from the screenshots directory into the read_bytes vector. |
| 30 void ReadScreenshot(const std::string& filename, |
| 31 std::vector<unsigned char>* read_bytes, |
| 32 base::WaitableEvent* read_complete) { |
| 33 read_bytes->clear(); |
| 34 |
| 35 FilePath fileshelf_path; |
| 36 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &fileshelf_path)) { |
| 37 read_complete->Signal(); |
| 38 return; |
| 39 } |
| 40 |
| 41 FilePath file(fileshelf_path.value() + std::string(kScreenshotsRelativePath) + |
| 42 filename); |
| 43 |
| 44 int64 file_size = 0; |
| 45 if (!file_util::GetFileSize(file, &file_size)) { |
| 46 read_complete->Signal(); |
| 47 return; |
| 48 } |
| 49 |
| 50 // expand vector to file size |
| 51 read_bytes->resize(file_size); |
| 52 // read file into the vector |
| 53 int bytes_read = 0; |
| 54 if (!(bytes_read = file_util::ReadFile(file, |
| 55 reinterpret_cast<char*>( |
| 56 &read_bytes->front()), |
| 57 static_cast<int>(file_size)))) |
| 58 read_bytes->clear(); |
| 59 |
| 60 // We're done, if successful, read_bytes will have the data |
| 61 // otherwise, it'll be empty. |
| 62 read_complete->Signal(); |
| 63 } |
| 64 |
| 65 // Get a saved screenshot - read on the FILE thread. |
| 66 std::vector<unsigned char> GetSavedScreenshot(std::string filename) { |
| 67 base::WaitableEvent read_complete(true, false); |
| 68 std::vector<unsigned char> bytes; |
| 69 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, |
| 70 NewRunnableFunction(&ReadScreenshot, filename, |
| 71 &bytes, &read_complete)); |
| 72 read_complete.Wait(); |
| 73 return bytes; |
| 74 } |
| 75 #endif |
| 76 |
| 77 std::vector<unsigned char> DOMUIScreenshotSource::GetScreenshot( |
| 78 const std::string& path) { |
| 79 if (path == kCurrentScreenshot) { |
| 80 return current_screenshot_; |
| 81 #if defined(OS_CHROMEOS) |
| 82 } else if (path.compare(0, strlen(kSavedScreenshots), |
| 83 kSavedScreenshots) == 0) { |
| 84 // Split the saved screenshot filename from the path |
| 85 std::string filename = path.substr(strlen(kSavedScreenshots)); |
| 86 |
| 87 return GetSavedScreenshot(filename); |
| 88 #endif |
| 89 } else { |
| 90 std::vector<unsigned char> ret; |
| 91 // TODO(rkc): Weird vc bug, return std::vector<unsigned char>() causes |
| 92 // the object assigned to the return value of this function magically |
| 93 // change it's address 0x0; look into this eventually. |
| 94 return ret; |
| 95 } |
| 96 } |
| 97 |
| 98 DOMUIScreenshotSource::DOMUIScreenshotSource( |
| 99 std::vector<unsigned char>* current_screenshot) |
| 100 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()) { |
| 101 // Setup the last screenshot taken. |
| 102 if (current_screenshot) |
| 103 current_screenshot_ = *current_screenshot; |
| 104 else |
| 105 current_screenshot_.clear(); |
| 106 } |
| 107 |
| 108 void DOMUIScreenshotSource::StartDataRequest(const std::string& path, |
| 109 bool is_off_the_record, |
| 110 int request_id) { |
| 111 SendResponse(request_id, new RefCountedBytes(GetScreenshot(path))); |
| 112 } |
OLD | NEW |