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_util.h" | 9 #include "base/file_util.h" |
10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/string16.h" | 13 #include "base/string16.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "chrome/browser/download/download_prefs.h" | 15 #include "chrome/browser/download/download_prefs.h" |
16 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
18 #include "googleurl/src/url_canon.h" | 18 #include "googleurl/src/url_canon.h" |
19 #include "googleurl/src/url_util.h" | 19 #include "googleurl/src/url_util.h" |
20 | 20 |
21 #if defined(OS_CHROMEOS) | 21 #if defined(OS_CHROMEOS) |
22 #include "ash/shell.h" | 22 #include "ash/shell.h" |
23 #include "ash/shell_delegate.h" | 23 #include "ash/shell_delegate.h" |
| 24 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" |
| 25 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" |
| 26 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
24 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
25 #endif | 28 #endif |
26 | 29 |
27 static const char kCurrentScreenshotFilename[] = "current"; | 30 static const char kCurrentScreenshotFilename[] = "current"; |
28 #if defined(OS_CHROMEOS) | 31 #if defined(OS_CHROMEOS) |
29 static const char kSavedScreenshotsBasePath[] = "saved/"; | 32 static const char kSavedScreenshotsBasePath[] = "saved/"; |
30 #endif | 33 #endif |
31 | 34 |
32 ScreenshotSource::ScreenshotSource( | 35 ScreenshotSource::ScreenshotSource( |
33 std::vector<unsigned char>* current_screenshot) | 36 std::vector<unsigned char>* current_screenshot, |
34 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()) { | 37 Profile* profile) |
| 38 : DataSource(chrome::kChromeUIScreenshotPath, MessageLoop::current()), |
| 39 profile_(profile) { |
35 // Setup the last screenshot taken. | 40 // Setup the last screenshot taken. |
36 if (current_screenshot) | 41 if (current_screenshot) |
37 current_screenshot_.reset(new ScreenshotData(*current_screenshot)); | 42 current_screenshot_.reset(new ScreenshotData(*current_screenshot)); |
38 else | 43 else |
39 current_screenshot_.reset(new ScreenshotData()); | 44 current_screenshot_.reset(new ScreenshotData()); |
40 } | 45 } |
41 | 46 |
42 ScreenshotSource::~ScreenshotSource() {} | 47 ScreenshotSource::~ScreenshotSource() {} |
43 | 48 |
44 void ScreenshotSource::StartDataRequest(const std::string& path, bool, | 49 void ScreenshotSource::StartDataRequest(const std::string& path, bool, |
(...skipping 24 matching lines...) Expand all Loading... |
69 // Strip the query param value - we only use it as a hack to ensure our | 74 // Strip the query param value - we only use it as a hack to ensure our |
70 // image gets reloaded instead of being pulled from the browser cache | 75 // image gets reloaded instead of being pulled from the browser cache |
71 std::string path = screenshot_path.substr( | 76 std::string path = screenshot_path.substr( |
72 0, screenshot_path.find_first_of("?")); | 77 0, screenshot_path.find_first_of("?")); |
73 if (path == kCurrentScreenshotFilename) { | 78 if (path == kCurrentScreenshotFilename) { |
74 CacheAndSendScreenshot(path, request_id, current_screenshot_); | 79 CacheAndSendScreenshot(path, request_id, current_screenshot_); |
75 #if defined(OS_CHROMEOS) | 80 #if defined(OS_CHROMEOS) |
76 } else if (path.compare(0, strlen(kSavedScreenshotsBasePath), | 81 } else if (path.compare(0, strlen(kSavedScreenshotsBasePath), |
77 kSavedScreenshotsBasePath) == 0) { | 82 kSavedScreenshotsBasePath) == 0) { |
78 using content::BrowserThread; | 83 using content::BrowserThread; |
79 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 84 |
80 base::Bind(&ScreenshotSource::SendSavedScreenshot, | 85 std::string filename = path.substr(strlen(kSavedScreenshotsBasePath)); |
81 base::Unretained(this), path, | 86 |
82 request_id)); | 87 url_canon::RawCanonOutputT<char16> decoded; |
| 88 url_util::DecodeURLEscapeSequences( |
| 89 filename.data(), filename.size(), &decoded); |
| 90 // Screenshot filenames don't use non-ascii characters. |
| 91 std::string decoded_filename = UTF16ToASCII(string16( |
| 92 decoded.data(), decoded.length())); |
| 93 |
| 94 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
| 95 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); |
| 96 FilePath download_path = download_prefs->DownloadPath(); |
| 97 if (gdata::util::IsUnderGDataMountPoint(download_path)) { |
| 98 gdata::GDataFileSystemInterface* file_system = |
| 99 gdata::GDataSystemServiceFactory::GetForProfile( |
| 100 profile_)->file_system(); |
| 101 file_system->GetFileByResourceId( |
| 102 decoded_filename, |
| 103 base::Bind(&ScreenshotSource::GetSavedScreenshotCallback, |
| 104 base::Unretained(this), screenshot_path, request_id), |
| 105 gdata::GetContentCallback()); |
| 106 } else { |
| 107 BrowserThread::PostTask( |
| 108 BrowserThread::FILE, FROM_HERE, |
| 109 base::Bind(&ScreenshotSource::SendSavedScreenshot, |
| 110 base::Unretained(this), |
| 111 screenshot_path, |
| 112 request_id, download_path.Append(decoded_filename))); |
| 113 } |
83 #endif | 114 #endif |
84 } else { | 115 } else { |
85 CacheAndSendScreenshot( | 116 CacheAndSendScreenshot( |
86 path, request_id, ScreenshotDataPtr(new ScreenshotData())); | 117 path, request_id, ScreenshotDataPtr(new ScreenshotData())); |
87 } | 118 } |
88 } | 119 } |
89 | 120 |
90 #if defined(OS_CHROMEOS) | 121 #if defined(OS_CHROMEOS) |
91 void ScreenshotSource::SendSavedScreenshot(const std::string& screenshot_path, | 122 void ScreenshotSource::SendSavedScreenshot( |
92 int request_id) { | 123 const std::string& screenshot_path, |
| 124 int request_id, |
| 125 const FilePath& file) { |
93 ScreenshotDataPtr read_bytes(new ScreenshotData); | 126 ScreenshotDataPtr read_bytes(new ScreenshotData); |
94 std::string filename = screenshot_path.substr( | 127 int64 file_size = 0; |
95 strlen(kSavedScreenshotsBasePath)); | |
96 | 128 |
97 url_canon::RawCanonOutputT<char16> decoded; | |
98 url_util::DecodeURLEscapeSequences( | |
99 filename.data(), filename.size(), &decoded); | |
100 // Screenshot filenames don't use non-ascii characters. | |
101 std::string decoded_filename = UTF16ToASCII(string16( | |
102 decoded.data(), decoded.length())); | |
103 | |
104 int64 file_size = 0; | |
105 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | |
106 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); | |
107 FilePath file = download_prefs->DownloadPath().Append(decoded_filename); | |
108 if (!file_util::GetFileSize(file, &file_size)) { | 129 if (!file_util::GetFileSize(file, &file_size)) { |
109 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); | 130 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); |
110 return; | 131 return; |
111 } | 132 } |
112 | 133 |
113 read_bytes->resize(file_size); | 134 read_bytes->resize(file_size); |
114 if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes->front()), | 135 if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes->front()), |
115 static_cast<int>(file_size))) | 136 static_cast<int>(file_size))) |
116 read_bytes->clear(); | 137 read_bytes->clear(); |
117 | 138 |
118 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); | 139 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); |
119 } | 140 } |
| 141 |
| 142 void ScreenshotSource::GetSavedScreenshotCallback( |
| 143 const std::string& screenshot_path, |
| 144 int request_id, |
| 145 gdata::GDataFileError error, |
| 146 const FilePath& file, |
| 147 const std::string& unused_mime_type, |
| 148 gdata::GDataFileType file_type) { |
| 149 if (error != gdata::GDATA_FILE_OK || file_type != gdata::REGULAR_FILE) { |
| 150 ScreenshotDataPtr read_bytes(new ScreenshotData); |
| 151 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); |
| 152 return; |
| 153 } |
| 154 |
| 155 content::BrowserThread::PostTask( |
| 156 content::BrowserThread::FILE, FROM_HERE, |
| 157 base::Bind(&ScreenshotSource::SendSavedScreenshot, |
| 158 base::Unretained(this), screenshot_path, request_id, file)); |
| 159 } |
120 #endif | 160 #endif |
121 | 161 |
122 void ScreenshotSource::CacheAndSendScreenshot( | 162 void ScreenshotSource::CacheAndSendScreenshot( |
123 const std::string& screenshot_path, | 163 const std::string& screenshot_path, |
124 int request_id, | 164 int request_id, |
125 ScreenshotDataPtr bytes) { | 165 ScreenshotDataPtr bytes) { |
126 cached_screenshots_[screenshot_path] = bytes; | 166 cached_screenshots_[screenshot_path] = bytes; |
127 SendResponse(request_id, new base::RefCountedBytes(*bytes)); | 167 SendResponse(request_id, new base::RefCountedBytes(*bytes)); |
128 } | 168 } |
OLD | NEW |