| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/i18n/time_formatting.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 12 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 13 #include "base/string16.h" | 15 #include "base/string16.h" |
| 16 #include "base/stringprintf.h" |
| 14 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/download/download_prefs.h" | 19 #include "chrome/browser/download/download_prefs.h" |
| 20 #include "chrome/browser/prefs/pref_service.h" |
| 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 24 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 18 #include "googleurl/src/url_canon.h" | 26 #include "googleurl/src/url_canon.h" |
| 19 #include "googleurl/src/url_util.h" | 27 #include "googleurl/src/url_util.h" |
| 20 | 28 |
| 21 #if defined(OS_CHROMEOS) | 29 #if defined(OS_CHROMEOS) |
| 22 #include "ash/shell.h" | 30 #include "ash/shell.h" |
| 23 #include "ash/shell_delegate.h" | 31 #include "ash/shell_delegate.h" |
| 24 #include "chrome/browser/chromeos/gdata/drive_file_system_interface.h" | 32 #include "chrome/browser/chromeos/gdata/drive_file_system_interface.h" |
| 25 #include "chrome/browser/chromeos/gdata/drive_system_service.h" | 33 #include "chrome/browser/chromeos/gdata/drive_system_service.h" |
| 26 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 34 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| 27 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 28 #endif | 36 #endif |
| 29 | 37 |
| 30 static const char kCurrentScreenshotFilename[] = "current"; | 38 static const char kCurrentScreenshotFilename[] = "current"; |
| 31 #if defined(OS_CHROMEOS) | 39 #if defined(OS_CHROMEOS) |
| 32 static const char kSavedScreenshotsBasePath[] = "saved/"; | 40 static const char kSavedScreenshotsBasePath[] = "saved/"; |
| 33 #endif | 41 #endif |
| 34 | 42 |
| 43 bool ShouldUse24HourClock() { |
| 44 #if defined(OS_CHROMEOS) |
| 45 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| 46 if (profile) { |
| 47 PrefService* pref_service = profile->GetPrefs(); |
| 48 if (pref_service) { |
| 49 return pref_service->GetBoolean(prefs::kUse24HourClock); |
| 50 } |
| 51 } |
| 52 #endif |
| 53 return base::GetHourClockType() == base::k24HourClock; |
| 54 } |
| 55 |
| 35 ScreenshotSource::ScreenshotSource( | 56 ScreenshotSource::ScreenshotSource( |
| 36 std::vector<unsigned char>* current_screenshot, | 57 std::vector<unsigned char>* current_screenshot, |
| 37 Profile* profile) | 58 Profile* profile) |
| 38 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()), | 59 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()), |
| 39 profile_(profile) { | 60 profile_(profile) { |
| 40 // Setup the last screenshot taken. | 61 // Setup the last screenshot taken. |
| 41 if (current_screenshot) | 62 if (current_screenshot) |
| 42 current_screenshot_.reset(new ScreenshotData(*current_screenshot)); | 63 current_screenshot_.reset(new ScreenshotData(*current_screenshot)); |
| 43 else | 64 else |
| 44 current_screenshot_.reset(new ScreenshotData()); | 65 current_screenshot_.reset(new ScreenshotData()); |
| 45 } | 66 } |
| 46 | 67 |
| 47 ScreenshotSource::~ScreenshotSource() {} | 68 ScreenshotSource::~ScreenshotSource() {} |
| 48 | 69 |
| 70 std::string ScreenshotSource::GetScreenshotBaseFilename() { |
| 71 base::Time::Exploded now; |
| 72 base::Time::Now().LocalExplode(&now); |
| 73 |
| 74 // We don't use base/i18n/time_formatting.h here because it doesn't |
| 75 // support our format. Don't use ICU either to avoid i18n file names |
| 76 // for non-English locales. |
| 77 // TODO(mukai): integrate this logic somewhere time_formatting.h |
| 78 std::string file_name = base::StringPrintf( |
| 79 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month); |
| 80 |
| 81 if (ShouldUse24HourClock()) { |
| 82 file_name.append(base::StringPrintf( |
| 83 "%02d.%02d.%02d", now.hour, now.minute, now.second)); |
| 84 } else { |
| 85 int hour = now.hour; |
| 86 if (hour > 12) { |
| 87 hour -= 12; |
| 88 } else if (hour == 0) { |
| 89 hour = 12; |
| 90 } |
| 91 file_name.append(base::StringPrintf( |
| 92 "%d.%02d.%02d ", hour, now.minute, now.second)); |
| 93 file_name.append((now.hour >= 12) ? "PM" : "AM"); |
| 94 } |
| 95 |
| 96 return file_name; |
| 97 } |
| 98 |
| 99 bool ScreenshotSource::AreScreenshotsDisabled() { |
| 100 return g_browser_process->local_state()->GetBoolean( |
| 101 prefs::kDisableScreenshots); |
| 102 } |
| 103 |
| 104 bool ScreenshotSource::GetScreenshotDirectory(FilePath* directory) { |
| 105 if (ScreenshotSource::AreScreenshotsDisabled()) |
| 106 return false; |
| 107 |
| 108 bool is_logged_in = true; |
| 109 #if defined(OS_CHROMEOS) |
| 110 is_logged_in = chromeos::UserManager::Get()->IsUserLoggedIn(); |
| 111 #endif |
| 112 |
| 113 if (is_logged_in) { |
| 114 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
| 115 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); |
| 116 *directory = download_prefs->DownloadPath(); |
| 117 } else { |
| 118 if (!file_util::GetTempDir(directory)) { |
| 119 LOG(ERROR) << "Failed to find temporary directory."; |
| 120 return false; |
| 121 } |
| 122 } |
| 123 return true; |
| 124 } |
| 125 |
| 49 void ScreenshotSource::StartDataRequest(const std::string& path, bool, | 126 void ScreenshotSource::StartDataRequest(const std::string& path, bool, |
| 50 int request_id) { | 127 int request_id) { |
| 51 SendScreenshot(path, request_id); | 128 SendScreenshot(path, request_id); |
| 52 } | 129 } |
| 53 | 130 |
| 54 std::string ScreenshotSource::GetMimeType(const std::string&) const { | 131 std::string ScreenshotSource::GetMimeType(const std::string&) const { |
| 55 // We need to explicitly return a mime type, otherwise if the user tries to | 132 // We need to explicitly return a mime type, otherwise if the user tries to |
| 56 // drag the image they get no extension. | 133 // drag the image they get no extension. |
| 57 return "image/png"; | 134 return "image/png"; |
| 58 } | 135 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 84 | 161 |
| 85 std::string filename = path.substr(strlen(kSavedScreenshotsBasePath)); | 162 std::string filename = path.substr(strlen(kSavedScreenshotsBasePath)); |
| 86 | 163 |
| 87 url_canon::RawCanonOutputT<char16> decoded; | 164 url_canon::RawCanonOutputT<char16> decoded; |
| 88 url_util::DecodeURLEscapeSequences( | 165 url_util::DecodeURLEscapeSequences( |
| 89 filename.data(), filename.size(), &decoded); | 166 filename.data(), filename.size(), &decoded); |
| 90 // Screenshot filenames don't use non-ascii characters. | 167 // Screenshot filenames don't use non-ascii characters. |
| 91 std::string decoded_filename = UTF16ToASCII(string16( | 168 std::string decoded_filename = UTF16ToASCII(string16( |
| 92 decoded.data(), decoded.length())); | 169 decoded.data(), decoded.length())); |
| 93 | 170 |
| 94 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | 171 FilePath download_path; |
| 95 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); | 172 GetScreenshotDirectory(&download_path); |
| 96 FilePath download_path = download_prefs->DownloadPath(); | |
| 97 if (gdata::util::IsUnderGDataMountPoint(download_path)) { | 173 if (gdata::util::IsUnderGDataMountPoint(download_path)) { |
| 98 gdata::DriveFileSystemInterface* file_system = | 174 gdata::DriveFileSystemInterface* file_system = |
| 99 gdata::DriveSystemServiceFactory::GetForProfile( | 175 gdata::DriveSystemServiceFactory::GetForProfile( |
| 100 profile_)->file_system(); | 176 profile_)->file_system(); |
| 101 file_system->GetFileByResourceId( | 177 file_system->GetFileByResourceId( |
| 102 decoded_filename, | 178 decoded_filename, |
| 103 base::Bind(&ScreenshotSource::GetSavedScreenshotCallback, | 179 base::Bind(&ScreenshotSource::GetSavedScreenshotCallback, |
| 104 base::Unretained(this), screenshot_path, request_id), | 180 base::Unretained(this), screenshot_path, request_id), |
| 105 gdata::GetContentCallback()); | 181 gdata::GetContentCallback()); |
| 106 } else { | 182 } else { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 235 } |
| 160 #endif | 236 #endif |
| 161 | 237 |
| 162 void ScreenshotSource::CacheAndSendScreenshot( | 238 void ScreenshotSource::CacheAndSendScreenshot( |
| 163 const std::string& screenshot_path, | 239 const std::string& screenshot_path, |
| 164 int request_id, | 240 int request_id, |
| 165 ScreenshotDataPtr bytes) { | 241 ScreenshotDataPtr bytes) { |
| 166 cached_screenshots_[screenshot_path] = bytes; | 242 cached_screenshots_[screenshot_path] = bytes; |
| 167 SendResponse(request_id, new base::RefCountedBytes(*bytes)); | 243 SendResponse(request_id, new base::RefCountedBytes(*bytes)); |
| 168 } | 244 } |
| OLD | NEW |