Chromium Code Reviews| Index: chrome/test/webdriver/session.cc |
| diff --git a/chrome/test/webdriver/session.cc b/chrome/test/webdriver/session.cc |
| index 42cfdc1905f69b4759c2343427b63b02b5494e29..545688daaced28611a74278cee4bdea266661f28 100644 |
| --- a/chrome/test/webdriver/session.cc |
| +++ b/chrome/test/webdriver/session.cc |
| @@ -6,6 +6,7 @@ |
| #include <vector> |
| +#include "base/base64.h" |
| #include "base/command_line.h" |
| #include "base/file_path.h" |
| #include "base/file_util.h" |
| @@ -14,6 +15,7 @@ |
| #include "base/process.h" |
| #include "base/process_util.h" |
| #include "base/scoped_ptr.h" |
| +#include "base/scoped_temp_dir.h" |
| #include "base/stringprintf.h" |
| #include "base/string_number_conversions.h" |
| #include "base/string_util.h" |
| @@ -59,8 +61,10 @@ bool Session::Init(const FilePath& browser_dir) { |
| } else { |
| LOG(ERROR) << "Cannot start session thread"; |
| } |
| + |
| if (!success) |
| delete this; |
| + |
| return success; |
| } |
| @@ -349,6 +353,61 @@ bool Session::GetWindowIds(std::vector<int>* window_ids) { |
| return success; |
| } |
| +bool Session::CaptureEntirePageAsPNG(const FilePath& path) { |
| + bool success = false; |
| + RunSessionTask(NewRunnableMethod( |
| + automation_.get(), |
| + &Automation::CaptureEntirePageAsPNG, |
| + current_window_id_, |
| + path, |
| + &success)); |
| + return success; |
| +} |
| + |
| +bool Session::ScreenshotAsBase64(std::string* screenshot) { |
| + ScopedTempDir screenshots_dir; |
| + |
| + // Create a temp directory for screenshots. |
| + if (!screenshots_dir.CreateUniqueTempDir()) { |
| + LOG(ERROR) << "Could not make a temp directory for screenshots"; |
| + return false; |
| + } |
| + |
| + // Generate a temp file name for the screenshot |
|
kkania
2011/02/24 22:05:02
we no longer need a temp file name if we are using
Joe
2011/03/02 02:21:25
Done.
|
| + scoped_ptr<char> file(tempnam(screenshots_dir.path().value().c_str(), |
| + "screencapture")); |
| + if (NULL == file.get()) { |
| + LOG(ERROR) << "Temp file name was not allocated"; |
| + return false; |
| + } |
| + |
| +#ifdef OS_POSIX |
| + FilePath path = FilePath(file.get()); |
| +#elif OS_WIN |
| + FilePath path = FilePath(ASCIIToWide(file.get())); |
| +#endif |
| + |
| + // Capture the current tab as a PNG file. |
| + if (!CaptureEntirePageAsPNG(path)) { |
| + LOG(ERROR) << "Capturing tab page as PNG failed"; |
| + return false; |
| + } |
| + |
| + std::string raw_bytes; |
| + if (!file_util::ReadFileToString(path, &raw_bytes)) { |
| + LOG(ERROR) << "Opening PNG file failed"; |
| + return false; |
| + } |
| + |
| + // Make a base64 encoded string for the raw PNG data |
| + if (!base::Base64Encode(raw_bytes, screenshot)) { |
| + LOG(ERROR) << "Encoding the PNG to base64 format failed"; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| ErrorCode Session::SwitchToWindow(const std::string& name) { |
| int switch_to_id = 0; |
| int name_no = 0; |