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