Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(831)

Unified Diff: chrome/browser/ui/webui/screenshot_source.cc

Issue 10908081: Refactor screenshot directory source (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Include cleanup Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/screenshot_source.cc
diff --git a/chrome/browser/ui/webui/screenshot_source.cc b/chrome/browser/ui/webui/screenshot_source.cc
index b95ecd0d110efbb95aaa927c6af590ede25764a5..248ab62cacc7d4c78ebd61918b7b77aa4712b0cb 100644
--- a/chrome/browser/ui/webui/screenshot_source.cc
+++ b/chrome/browser/ui/webui/screenshot_source.cc
@@ -6,14 +6,22 @@
#include "base/bind.h"
#include "base/callback.h"
+#include "base/file_path.h"
#include "base/file_util.h"
+#include "base/i18n/time_formatting.h"
#include "base/memory/ref_counted_memory.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string16.h"
+#include "base/stringprintf.h"
#include "base/string_util.h"
+#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_prefs.h"
+#include "chrome/browser/prefs/pref_service.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_paths.h"
+#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "googleurl/src/url_canon.h"
#include "googleurl/src/url_util.h"
@@ -32,6 +40,19 @@ static const char kCurrentScreenshotFilename[] = "current";
static const char kSavedScreenshotsBasePath[] = "saved/";
#endif
+bool ShouldUse24HourClock() {
+#if defined(OS_CHROMEOS)
+ Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
+ if (profile) {
+ PrefService* pref_service = profile->GetPrefs();
+ if (pref_service) {
+ return pref_service->GetBoolean(prefs::kUse24HourClock);
+ }
+ }
+#endif
+ return base::GetHourClockType() == base::k24HourClock;
+}
+
ScreenshotSource::ScreenshotSource(
std::vector<unsigned char>* current_screenshot,
Profile* profile)
@@ -46,6 +67,62 @@ ScreenshotSource::ScreenshotSource(
ScreenshotSource::~ScreenshotSource() {}
+std::string ScreenshotSource::GetScreenshotBaseFilename() {
+ base::Time::Exploded now;
+ base::Time::Now().LocalExplode(&now);
+
+ // We don't use base/i18n/time_formatting.h here because it doesn't
+ // support our format. Don't use ICU either to avoid i18n file names
+ // for non-English locales.
+ // TODO(mukai): integrate this logic somewhere time_formatting.h
+ std::string file_name = base::StringPrintf(
+ "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month);
+
+ if (ShouldUse24HourClock()) {
+ file_name.append(base::StringPrintf(
+ "%02d.%02d.%02d", now.hour, now.minute, now.second));
+ } else {
+ int hour = now.hour;
+ if (hour > 12) {
+ hour -= 12;
+ } else if (hour == 0) {
+ hour = 12;
+ }
+ file_name.append(base::StringPrintf(
+ "%d.%02d.%02d ", hour, now.minute, now.second));
+ file_name.append((now.hour >= 12) ? "PM" : "AM");
+ }
+
+ return file_name;
+}
+
+bool ScreenshotSource::AreScreenshotsDisabled() {
+ return g_browser_process->local_state()->GetBoolean(
+ prefs::kDisableScreenshots);
+}
+
+bool ScreenshotSource::GetScreenshotDirectory(FilePath* directory) {
+ if (ScreenshotSource::AreScreenshotsDisabled())
+ return false;
+
+ bool is_logged_in = true;
+#if defined(OS_CHROMEOS)
+ is_logged_in = chromeos::UserManager::Get()->IsUserLoggedIn();
+#endif
+
+ if (is_logged_in) {
+ DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
+ ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext());
+ *directory = download_prefs->DownloadPath();
+ } else {
+ if (!file_util::GetTempDir(directory)) {
+ LOG(ERROR) << "Failed to find temporary directory.";
+ return false;
+ }
+ }
+ return true;
+}
+
void ScreenshotSource::StartDataRequest(const std::string& path, bool,
int request_id) {
SendScreenshot(path, request_id);
@@ -91,9 +168,8 @@ void ScreenshotSource::SendScreenshot(const std::string& screenshot_path,
std::string decoded_filename = UTF16ToASCII(string16(
decoded.data(), decoded.length()));
- DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
- ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext());
- FilePath download_path = download_prefs->DownloadPath();
+ FilePath download_path;
+ GetScreenshotDirectory(&download_path);
if (gdata::util::IsUnderGDataMountPoint(download_path)) {
gdata::DriveFileSystemInterface* file_system =
gdata::DriveSystemServiceFactory::GetForProfile(
« chrome/browser/ui/webui/screenshot_source.h ('K') | « chrome/browser/ui/webui/screenshot_source.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698