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_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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 #endif | 88 #endif |
| 84 } else { | 89 } else { |
| 85 CacheAndSendScreenshot( | 90 CacheAndSendScreenshot( |
| 86 path, request_id, ScreenshotDataPtr(new ScreenshotData())); | 91 path, request_id, ScreenshotDataPtr(new ScreenshotData())); |
| 87 } | 92 } |
| 88 } | 93 } |
| 89 | 94 |
| 90 #if defined(OS_CHROMEOS) | 95 #if defined(OS_CHROMEOS) |
| 91 void ScreenshotSource::SendSavedScreenshot(const std::string& screenshot_path, | 96 void ScreenshotSource::SendSavedScreenshot(const std::string& screenshot_path, |
| 92 int request_id) { | 97 int request_id) { |
| 93 ScreenshotDataPtr read_bytes(new ScreenshotData); | |
| 94 std::string filename = screenshot_path.substr( | 98 std::string filename = screenshot_path.substr( |
| 95 strlen(kSavedScreenshotsBasePath)); | 99 strlen(kSavedScreenshotsBasePath)); |
| 96 | 100 |
| 97 url_canon::RawCanonOutputT<char16> decoded; | 101 url_canon::RawCanonOutputT<char16> decoded; |
| 98 url_util::DecodeURLEscapeSequences( | 102 url_util::DecodeURLEscapeSequences( |
| 99 filename.data(), filename.size(), &decoded); | 103 filename.data(), filename.size(), &decoded); |
| 100 // Screenshot filenames don't use non-ascii characters. | 104 // Screenshot filenames don't use non-ascii characters. |
| 101 std::string decoded_filename = UTF16ToASCII(string16( | 105 std::string decoded_filename = UTF16ToASCII(string16( |
| 102 decoded.data(), decoded.length())); | 106 decoded.data(), decoded.length())); |
| 103 | 107 |
| 104 int64 file_size = 0; | |
| 105 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | 108 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
| 106 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); | 109 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); |
| 107 FilePath file = download_prefs->DownloadPath().Append(decoded_filename); | 110 FilePath download_path = download_prefs->DownloadPath(); |
| 108 if (!file_util::GetFileSize(file, &file_size)) { | 111 if (gdata::util::IsUnderGDataMountPoint(download_path)) { |
| 112 gdata::GDataFileSystemInterface* file_system = | |
| 113 gdata::GDataSystemServiceFactory::GetForProfile( | |
| 114 profile_)->file_system(); | |
| 115 file_system->GetFileByResourceId( | |
|
kinaba
2012/08/15 08:19:57
The method is DCHECKED to run on UI or IO thread,
Jun Mukai
2012/08/15 08:30:42
Aagh, right. Modified to do that.
| |
| 116 decoded_filename, | |
| 117 base::Bind(&ScreenshotSource::SendSavedScreenshotInternal, | |
| 118 base::Unretained(this), screenshot_path, request_id), | |
| 119 gdata::GetContentCallback()); | |
| 120 } else { | |
| 121 SendSavedScreenshotInternal(screenshot_path, | |
| 122 request_id, | |
| 123 gdata::GDATA_FILE_OK, | |
| 124 download_path.Append(decoded_filename), | |
| 125 "", | |
| 126 gdata::REGULAR_FILE); | |
| 127 } | |
| 128 | |
| 129 } | |
| 130 | |
| 131 void ScreenshotSource::SendSavedScreenshotInternal( | |
| 132 const std::string& screenshot_path, | |
| 133 int request_id, | |
| 134 gdata::GDataFileError error, | |
| 135 const FilePath& file, | |
| 136 const std::string& mime_type, | |
| 137 gdata::GDataFileType file_type) { | |
| 138 ScreenshotDataPtr read_bytes(new ScreenshotData); | |
| 139 int64 file_size = 0; | |
| 140 | |
| 141 if (error != gdata::GDATA_FILE_OK || | |
| 142 file_type != gdata::REGULAR_FILE || | |
| 143 !file_util::GetFileSize(file, &file_size)) { | |
| 109 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); | 144 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); |
| 110 return; | 145 return; |
| 111 } | 146 } |
| 112 | 147 |
| 113 read_bytes->resize(file_size); | 148 read_bytes->resize(file_size); |
| 114 if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes->front()), | 149 if (!file_util::ReadFile(file, reinterpret_cast<char*>(&read_bytes->front()), |
| 115 static_cast<int>(file_size))) | 150 static_cast<int>(file_size))) |
| 116 read_bytes->clear(); | 151 read_bytes->clear(); |
| 117 | 152 |
| 118 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); | 153 CacheAndSendScreenshot(screenshot_path, request_id, read_bytes); |
| 119 } | 154 } |
| 120 #endif | 155 #endif |
| 121 | 156 |
| 122 void ScreenshotSource::CacheAndSendScreenshot( | 157 void ScreenshotSource::CacheAndSendScreenshot( |
| 123 const std::string& screenshot_path, | 158 const std::string& screenshot_path, |
| 124 int request_id, | 159 int request_id, |
| 125 ScreenshotDataPtr bytes) { | 160 ScreenshotDataPtr bytes) { |
| 126 cached_screenshots_[screenshot_path] = bytes; | 161 cached_screenshots_[screenshot_path] = bytes; |
| 127 SendResponse(request_id, new base::RefCountedBytes(*bytes)); | 162 SendResponse(request_id, new base::RefCountedBytes(*bytes)); |
| 128 } | 163 } |
| OLD | NEW |