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

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