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