| 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(USE_ASH) |
| 22 #include "ash/shell.h" | 30 #include "ash/shell.h" |
| 23 #include "ash/shell_delegate.h" | 31 #include "ash/shell_delegate.h" |
| 32 #endif |
| 33 |
| 34 #if defined(OS_CHROMEOS) |
| 24 #include "chrome/browser/chromeos/gdata/drive_file_system_interface.h" | 35 #include "chrome/browser/chromeos/gdata/drive_file_system_interface.h" |
| 25 #include "chrome/browser/chromeos/gdata/drive_file_system_util.h" | 36 #include "chrome/browser/chromeos/gdata/drive_file_system_util.h" |
| 26 #include "chrome/browser/chromeos/gdata/drive_system_service.h" | 37 #include "chrome/browser/chromeos/gdata/drive_system_service.h" |
| 38 #include "chrome/browser/chromeos/login/user_manager.h" |
| 27 #include "content/public/browser/browser_thread.h" | 39 #include "content/public/browser/browser_thread.h" |
| 28 #endif | 40 #endif |
| 29 | 41 |
| 30 static const char kCurrentScreenshotFilename[] = "current"; | 42 // static |
| 43 const char ScreenshotSource::kScreenshotUrlRoot[] = "chrome://screenshots/"; |
| 44 // static |
| 45 const char ScreenshotSource::kScreenshotCurrent[] = "current"; |
| 46 // static |
| 47 const char ScreenshotSource::kScreenshotSaved[] = "saved/"; |
| 31 #if defined(OS_CHROMEOS) | 48 #if defined(OS_CHROMEOS) |
| 32 static const char kSavedScreenshotsBasePath[] = "saved/"; | 49 // static |
| 50 const char ScreenshotSource::kScreenshotPrefix[] = "Screenshot "; |
| 51 // static |
| 52 const char ScreenshotSource::kScreenshotSuffix[] = ".png"; |
| 33 #endif | 53 #endif |
| 34 | 54 |
| 55 bool ShouldUse24HourClock() { |
| 56 #if defined(OS_CHROMEOS) |
| 57 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| 58 if (profile) { |
| 59 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); |
| 60 } |
| 61 #endif |
| 62 return base::GetHourClockType() == base::k24HourClock; |
| 63 } |
| 64 |
| 35 ScreenshotSource::ScreenshotSource( | 65 ScreenshotSource::ScreenshotSource( |
| 36 std::vector<unsigned char>* current_screenshot, | 66 std::vector<unsigned char>* current_screenshot, |
| 37 Profile* profile) | 67 Profile* profile) |
| 38 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()), | 68 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()), |
| 39 profile_(profile) { | 69 profile_(profile) { |
| 40 // Setup the last screenshot taken. | 70 // Setup the last screenshot taken. |
| 41 if (current_screenshot) | 71 if (current_screenshot) |
| 42 current_screenshot_.reset(new ScreenshotData(*current_screenshot)); | 72 current_screenshot_.reset(new ScreenshotData(*current_screenshot)); |
| 43 else | 73 else |
| 44 current_screenshot_.reset(new ScreenshotData()); | 74 current_screenshot_.reset(new ScreenshotData()); |
| 45 } | 75 } |
| 46 | 76 |
| 47 ScreenshotSource::~ScreenshotSource() {} | 77 ScreenshotSource::~ScreenshotSource() {} |
| 48 | 78 |
| 79 // static |
| 80 std::string ScreenshotSource::GetScreenshotBaseFilename() { |
| 81 base::Time::Exploded now; |
| 82 base::Time::Now().LocalExplode(&now); |
| 83 |
| 84 // We don't use base/i18n/time_formatting.h here because it doesn't |
| 85 // support our format. Don't use ICU either to avoid i18n file names |
| 86 // for non-English locales. |
| 87 // TODO(mukai): integrate this logic somewhere time_formatting.h |
| 88 std::string file_name = base::StringPrintf( |
| 89 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month); |
| 90 |
| 91 if (ShouldUse24HourClock()) { |
| 92 file_name.append(base::StringPrintf( |
| 93 "%02d.%02d.%02d", now.hour, now.minute, now.second)); |
| 94 } else { |
| 95 int hour = now.hour; |
| 96 if (hour > 12) { |
| 97 hour -= 12; |
| 98 } else if (hour == 0) { |
| 99 hour = 12; |
| 100 } |
| 101 file_name.append(base::StringPrintf( |
| 102 "%d.%02d.%02d ", hour, now.minute, now.second)); |
| 103 file_name.append((now.hour >= 12) ? "PM" : "AM"); |
| 104 } |
| 105 |
| 106 return file_name; |
| 107 } |
| 108 |
| 109 #if defined(USE_ASH) |
| 110 |
| 111 // static |
| 112 bool ScreenshotSource::AreScreenshotsDisabled() { |
| 113 return g_browser_process->local_state()->GetBoolean( |
| 114 prefs::kDisableScreenshots); |
| 115 } |
| 116 |
| 117 // static |
| 118 bool ScreenshotSource::GetScreenshotDirectory(FilePath* directory) { |
| 119 if (ScreenshotSource::AreScreenshotsDisabled()) |
| 120 return false; |
| 121 |
| 122 bool is_logged_in = true; |
| 123 |
| 124 #if defined(OS_CHROMEOS) |
| 125 is_logged_in = chromeos::UserManager::Get()->IsUserLoggedIn(); |
| 126 #endif |
| 127 |
| 128 if (is_logged_in) { |
| 129 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
| 130 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); |
| 131 *directory = download_prefs->DownloadPath(); |
| 132 } else { |
| 133 if (!file_util::GetTempDir(directory)) { |
| 134 LOG(ERROR) << "Failed to find temporary directory."; |
| 135 return false; |
| 136 } |
| 137 } |
| 138 return true; |
| 139 } |
| 140 |
| 141 #endif |
| 142 |
| 49 void ScreenshotSource::StartDataRequest(const std::string& path, bool, | 143 void ScreenshotSource::StartDataRequest(const std::string& path, bool, |
| 50 int request_id) { | 144 int request_id) { |
| 51 SendScreenshot(path, request_id); | 145 SendScreenshot(path, request_id); |
| 52 } | 146 } |
| 53 | 147 |
| 54 std::string ScreenshotSource::GetMimeType(const std::string&) const { | 148 std::string ScreenshotSource::GetMimeType(const std::string&) const { |
| 55 // We need to explicitly return a mime type, otherwise if the user tries to | 149 // We need to explicitly return a mime type, otherwise if the user tries to |
| 56 // drag the image they get no extension. | 150 // drag the image they get no extension. |
| 57 return "image/png"; | 151 return "image/png"; |
| 58 } | 152 } |
| 59 | 153 |
| 60 ScreenshotDataPtr ScreenshotSource::GetCachedScreenshot( | 154 ScreenshotDataPtr ScreenshotSource::GetCachedScreenshot( |
| 61 const std::string& screenshot_path) { | 155 const std::string& screenshot_path) { |
| 62 std::map<std::string, ScreenshotDataPtr>::iterator pos; | 156 std::map<std::string, ScreenshotDataPtr>::iterator pos; |
| 63 std::string path = screenshot_path.substr( | 157 std::string path = screenshot_path.substr( |
| 64 0, screenshot_path.find_first_of("?")); | 158 0, screenshot_path.find_first_of("?")); |
| 65 if ((pos = cached_screenshots_.find(path)) != cached_screenshots_.end()) { | 159 if ((pos = cached_screenshots_.find(path)) != cached_screenshots_.end()) { |
| 66 return pos->second; | 160 return pos->second; |
| 67 } else { | 161 } else { |
| 68 return ScreenshotDataPtr(new ScreenshotData); | 162 return ScreenshotDataPtr(new ScreenshotData); |
| 69 } | 163 } |
| 70 } | 164 } |
| 71 | 165 |
| 72 void ScreenshotSource::SendScreenshot(const std::string& screenshot_path, | 166 void ScreenshotSource::SendScreenshot(const std::string& screenshot_path, |
| 73 int request_id) { | 167 int request_id) { |
| 74 // Strip the query param value - we only use it as a hack to ensure our | 168 // Strip the query param value - we only use it as a hack to ensure our |
| 75 // image gets reloaded instead of being pulled from the browser cache | 169 // image gets reloaded instead of being pulled from the browser cache |
| 76 std::string path = screenshot_path.substr( | 170 std::string path = screenshot_path.substr( |
| 77 0, screenshot_path.find_first_of("?")); | 171 0, screenshot_path.find_first_of("?")); |
| 78 if (path == kCurrentScreenshotFilename) { | 172 if (path == ScreenshotSource::kScreenshotCurrent) { |
| 79 CacheAndSendScreenshot(path, request_id, current_screenshot_); | 173 CacheAndSendScreenshot(path, request_id, current_screenshot_); |
| 80 #if defined(OS_CHROMEOS) | 174 #if defined(OS_CHROMEOS) |
| 81 } else if (path.compare(0, strlen(kSavedScreenshotsBasePath), | 175 } else if (path.compare(0, strlen(ScreenshotSource::kScreenshotSaved), |
| 82 kSavedScreenshotsBasePath) == 0) { | 176 ScreenshotSource::kScreenshotSaved) == 0) { |
| 83 using content::BrowserThread; | 177 using content::BrowserThread; |
| 84 | 178 |
| 85 std::string filename = path.substr(strlen(kSavedScreenshotsBasePath)); | 179 std::string filename = |
| 180 path.substr(strlen(ScreenshotSource::kScreenshotSaved)); |
| 86 | 181 |
| 87 url_canon::RawCanonOutputT<char16> decoded; | 182 url_canon::RawCanonOutputT<char16> decoded; |
| 88 url_util::DecodeURLEscapeSequences( | 183 url_util::DecodeURLEscapeSequences( |
| 89 filename.data(), filename.size(), &decoded); | 184 filename.data(), filename.size(), &decoded); |
| 90 // Screenshot filenames don't use non-ascii characters. | 185 // Screenshot filenames don't use non-ascii characters. |
| 91 std::string decoded_filename = UTF16ToASCII(string16( | 186 std::string decoded_filename = UTF16ToASCII(string16( |
| 92 decoded.data(), decoded.length())); | 187 decoded.data(), decoded.length())); |
| 93 | 188 |
| 94 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | 189 FilePath download_path; |
| 95 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); | 190 GetScreenshotDirectory(&download_path); |
| 96 FilePath download_path = download_prefs->DownloadPath(); | |
| 97 if (gdata::util::IsUnderDriveMountPoint(download_path)) { | 191 if (gdata::util::IsUnderDriveMountPoint(download_path)) { |
| 98 gdata::DriveFileSystemInterface* file_system = | 192 gdata::DriveFileSystemInterface* file_system = |
| 99 gdata::DriveSystemServiceFactory::GetForProfile( | 193 gdata::DriveSystemServiceFactory::GetForProfile( |
| 100 profile_)->file_system(); | 194 profile_)->file_system(); |
| 101 file_system->GetFileByResourceId( | 195 file_system->GetFileByResourceId( |
| 102 decoded_filename, | 196 decoded_filename, |
| 103 base::Bind(&ScreenshotSource::GetSavedScreenshotCallback, | 197 base::Bind(&ScreenshotSource::GetSavedScreenshotCallback, |
| 104 base::Unretained(this), screenshot_path, request_id), | 198 base::Unretained(this), screenshot_path, request_id), |
| 105 gdata::GetContentCallback()); | 199 gdata::GetContentCallback()); |
| 106 } else { | 200 } else { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 void ScreenshotSource::CacheAndSendScreenshot( | 256 void ScreenshotSource::CacheAndSendScreenshot( |
| 163 const std::string& screenshot_path, | 257 const std::string& screenshot_path, |
| 164 int request_id, | 258 int request_id, |
| 165 ScreenshotDataPtr bytes) { | 259 ScreenshotDataPtr bytes) { |
| 166 // Strip the query from the screenshot path. | 260 // Strip the query from the screenshot path. |
| 167 std::string path = screenshot_path.substr( | 261 std::string path = screenshot_path.substr( |
| 168 0, screenshot_path.find_first_of("?")); | 262 0, screenshot_path.find_first_of("?")); |
| 169 cached_screenshots_[path] = bytes; | 263 cached_screenshots_[path] = bytes; |
| 170 SendResponse(request_id, new base::RefCountedBytes(*bytes)); | 264 SendResponse(request_id, new base::RefCountedBytes(*bytes)); |
| 171 } | 265 } |
| OLD | NEW |