Commit d5b66843 authored by kinaba@chromium.org's avatar kinaba@chromium.org

gdata: Save screenshot to /drive.

Note that this does not support the full functions of screenshots.
It merely makes possible to save the screenshot .png file to  /drive.
What are *not* yet included in this patch are:
- Show/upload the images from "Report issue" (crbug:140622).
- Taking care of name collisions. For now, when a screenshot is taken
  after a short time period of another screenshot, the new comer will
  overwrite or recorded as ".. (1).png", depending on timing.

Not quite good, but for M22, I hope it is better than nothing.

BUG=138593

Review URL: https://chromiumcodereview.appspot.com/10830179

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@150204 0039d316-1c4b-4281-b951-d872f2087c98
parent 6aee070d
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "ui/aura/window.h" #include "ui/aura/window.h"
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/gdata/gdata_util.h"
#include "chrome/browser/chromeos/login/user_manager.h" #include "chrome/browser/chromeos/login/user_manager.h"
#endif #endif
...@@ -49,8 +50,7 @@ bool ShouldUse24HourClock() { ...@@ -49,8 +50,7 @@ bool ShouldUse24HourClock() {
return base::GetHourClockType() == base::k24HourClock; return base::GetHourClockType() == base::k24HourClock;
} }
FilePath GetScreenshotPath(const FilePath& base_directory, std::string GetScreenShotBaseFilename(bool use_24hour_clock) {
bool use_24hour_clock) {
base::Time::Exploded now; base::Time::Exploded now;
base::Time::Now().LocalExplode(&now); base::Time::Now().LocalExplode(&now);
...@@ -76,41 +76,87 @@ FilePath GetScreenshotPath(const FilePath& base_directory, ...@@ -76,41 +76,87 @@ FilePath GetScreenshotPath(const FilePath& base_directory,
file_name.append((now.hour >= 12) ? "PM" : "AM"); file_name.append((now.hour >= 12) ? "PM" : "AM");
} }
return file_name;
}
FilePath GetScreenshotPath(const FilePath& base_directory,
const std::string& base_name) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
for (int retry = 0; retry < INT_MAX; retry++) { for (int retry = 0; retry < INT_MAX; retry++) {
std::string retry_suffix; std::string retry_suffix;
if (retry > 0) if (retry > 0)
retry_suffix = base::StringPrintf(" (%d)", retry + 1); retry_suffix = base::StringPrintf(" (%d)", retry + 1);
FilePath file_path = base_directory.AppendASCII( FilePath file_path = base_directory.AppendASCII(
file_name + retry_suffix + ".png"); base_name + retry_suffix + ".png");
if (!file_util::PathExists(file_path)) if (!file_util::PathExists(file_path))
return file_path; return file_path;
} }
return FilePath(); return FilePath();
} }
// |is_logged_in| is used only for ChromeOS. Otherwise it is always true. void SaveScreenshotToLocalFile(scoped_refptr<base::RefCountedBytes> png_data,
void SaveScreenshot(const FilePath& screenshot_directory, const FilePath& screenshot_path) {
bool use_24hour_clock,
scoped_refptr<base::RefCountedBytes> png_data) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
if (static_cast<size_t>(file_util::WriteFile(
screenshot_path,
reinterpret_cast<char*>(&(png_data->data()[0])),
png_data->size())) != png_data->size()) {
LOG(ERROR) << "Failed to save to " << screenshot_path.value();
}
}
FilePath screenshot_path = GetScreenshotPath( void SaveScreenshot(const FilePath& screenshot_directory,
screenshot_directory, use_24hour_clock); const std::string& base_name,
scoped_refptr<base::RefCountedBytes> png_data) {
FilePath screenshot_path = GetScreenshotPath(screenshot_directory, base_name);
if (screenshot_path.empty()) { if (screenshot_path.empty()) {
LOG(ERROR) << "Failed to find a screenshot file name."; LOG(ERROR) << "Failed to find a screenshot file name.";
return; return;
} }
SaveScreenshotToLocalFile(png_data, screenshot_path);
}
if (static_cast<size_t>(file_util::WriteFile( // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch.
screenshot_path, #ifdef OS_CHROMEOS
reinterpret_cast<char*>(&(png_data->data()[0])), void SaveScreenshotToGData(scoped_refptr<base::RefCountedBytes> png_data,
png_data->size())) != png_data->size()) { gdata::GDataFileError error,
LOG(ERROR) << "Failed to save to " << screenshot_path.value(); const FilePath& local_path) {
if (error != gdata::GDATA_FILE_OK) {
LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error;
return;
}
SaveScreenshotToLocalFile(png_data, local_path);
}
void PostSaveScreenshotTask(const FilePath& screenshot_directory,
const std::string& base_name,
scoped_refptr<base::RefCountedBytes> png_data) {
if (gdata::util::IsUnderGDataMountPoint(screenshot_directory)) {
Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
if (profile) {
// TODO(kinaba,mukai): crbug.com/140749. Take care of the case
// "base_name.png" already exists.
gdata::util::PrepareWritableFileAndRun(
profile,
screenshot_directory.Append(base_name + ".png"),
base::Bind(&SaveScreenshotToGData, png_data));
}
} else {
content::BrowserThread::PostTask(
content::BrowserThread::FILE, FROM_HERE,
base::Bind(&SaveScreenshot, screenshot_directory, base_name, png_data));
} }
} }
#else
void PostSaveScreenshotTask(const FilePath& screenshot_directory,
const std::string& base_name,
scoped_refptr<base::RefCountedBytes> png_data) {
content::BrowserThread::PostTask(
content::BrowserThread::FILE, FROM_HERE,
base::Bind(&SaveScreenshot, screenshot_directory, base_name, png_data));
}
#endif
bool AreScreenshotsDisabled() { bool AreScreenshotsDisabled() {
return g_browser_process->local_state()->GetBoolean( return g_browser_process->local_state()->GetBoolean(
...@@ -181,15 +227,12 @@ void ScreenshotTaker::HandleTakePartialScreenshot( ...@@ -181,15 +227,12 @@ void ScreenshotTaker::HandleTakePartialScreenshot(
} }
} }
bool use_24hour_clock = ShouldUse24HourClock();
if (GrabWindowSnapshot(window, rect, &png_data->data())) { if (GrabWindowSnapshot(window, rect, &png_data->data())) {
last_screenshot_timestamp_ = base::Time::Now(); last_screenshot_timestamp_ = base::Time::Now();
DisplayVisualFeedback(rect); DisplayVisualFeedback(rect);
content::BrowserThread::PostTask( PostSaveScreenshotTask(screenshot_directory,
content::BrowserThread::FILE, FROM_HERE, GetScreenShotBaseFilename(ShouldUse24HourClock()),
base::Bind(&SaveScreenshot, screenshot_directory, use_24hour_clock, png_data);
png_data));
} else { } else {
LOG(ERROR) << "Failed to grab the window screenshot"; LOG(ERROR) << "Failed to grab the window screenshot";
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment