[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | fd42ac30f | 2011-02-27 19:33:36 | [diff] [blame] | 5 | #include "chrome/browser/ui/webui/fileicon_source.h" |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 6 | |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 7 | #include "base/basictypes.h" |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 8 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 9 | #include "base/callback.h" |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 10 | #include "base/file_path.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/ref_counted_memory.h" |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 12 | #include "base/message_loop.h" |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 13 | #include "base/string_split.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 16 | #include "chrome/common/time_format.h" |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 17 | #include "googleurl/src/gurl.h" |
[email protected] | 6c5a7c7 | 2009-02-25 00:57:43 | [diff] [blame] | 18 | #include "grit/generated_resources.h" |
[email protected] | 4343769 | 2009-03-04 04:07:34 | [diff] [blame] | 19 | #include "net/base/escape.h" |
[email protected] | 66171ab | 2011-03-03 15:50:07 | [diff] [blame] | 20 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 21 | #include "ui/gfx/codec/png_codec.h" |
[email protected] | f08e051 | 2011-06-13 18:10:44 | [diff] [blame] | 22 | #include "ui/gfx/image/image.h" |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 23 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 24 | #include "ui/webui/web_ui_util.h" |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 25 | |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap; |
| 29 | |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 30 | // The path used in internal URLs to file icon data. |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 31 | const char kFileIconPath[] = "fileicon"; |
| 32 | |
| 33 | // URL parameter specifying icon size. |
| 34 | const char kIconSize[] = "iconsize"; |
| 35 | |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 36 | // URL parameter specifying scale factor. |
| 37 | const char kScaleFactor[] = "scale"; |
| 38 | |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 39 | // Assuming the url is of the form '/path?query', convert the path portion into |
| 40 | // a FilePath and return the resulting |file_path| and |query|. The path |
| 41 | // portion may have been encoded using encodeURIComponent(). |
| 42 | void GetFilePathAndQuery(const std::string& url, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame^] | 43 | base::FilePath* file_path, |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 44 | std::string* query) { |
| 45 | // We receive the url with chrome://fileicon/ stripped but GURL expects it. |
| 46 | const GURL gurl("chrome://fileicon/" + url); |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 47 | std::string path = net::UnescapeURLComponent( |
| 48 | gurl.path().substr(1), (net::UnescapeRule::URL_SPECIAL_CHARS | |
| 49 | net::UnescapeRule::SPACES)); |
| 50 | |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 51 | #if defined(OS_WIN) |
| 52 | // The path we receive has the wrong slashes and escaping for what we need; |
| 53 | // this only appears to matter for getting icons from .exe files. |
| 54 | std::replace(path.begin(), path.end(), '/', '\\'); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame^] | 55 | *file_path = base::FilePath(UTF8ToWide(path)); |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 56 | #elif defined(OS_POSIX) |
| 57 | // The correct encoding on Linux may not actually be UTF8. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame^] | 58 | *file_path = base::FilePath(path); |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 59 | #endif |
| 60 | query->assign(gurl.query()); |
| 61 | } |
| 62 | |
| 63 | IconLoader::IconSize SizeStringToIconSize(const std::string& size_string) { |
| 64 | if (size_string == "small") return IconLoader::SMALL; |
| 65 | if (size_string == "large") return IconLoader::LARGE; |
| 66 | // We default to NORMAL if we don't recognize the size_string. Including |
| 67 | // size_string=="normal". |
| 68 | return IconLoader::NORMAL; |
| 69 | } |
| 70 | |
| 71 | // Simple parser for data on the query. |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 72 | void ParseQueryParams(const std::string& query, |
| 73 | ui::ScaleFactor* scale_factor, |
| 74 | IconLoader::IconSize* icon_size) { |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 75 | typedef std::pair<std::string, std::string> KVPair; |
| 76 | std::vector<KVPair> parameters; |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 77 | if (icon_size) |
| 78 | *icon_size = IconLoader::NORMAL; |
| 79 | if (scale_factor) |
[email protected] | 0a4cf45 | 2012-11-14 07:28:40 | [diff] [blame] | 80 | *scale_factor = ui::SCALE_FACTOR_100P; |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 81 | base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters); |
| 82 | for (std::vector<KVPair>::const_iterator iter = parameters.begin(); |
| 83 | iter != parameters.end(); ++iter) { |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 84 | if (icon_size && iter->first == kIconSize) |
| 85 | *icon_size = SizeStringToIconSize(iter->second); |
| 86 | else if (scale_factor && iter->first == kScaleFactor) |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 87 | webui::ParseScaleFactor(iter->second, scale_factor); |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 88 | } |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 92 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 93 | FileIconSource::IconRequestDetails::IconRequestDetails() { |
| 94 | } |
| 95 | |
| 96 | FileIconSource::IconRequestDetails::~IconRequestDetails() { |
| 97 | } |
| 98 | |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 99 | FileIconSource::FileIconSource() {} |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 100 | |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 101 | FileIconSource::~FileIconSource() {} |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 102 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 103 | void FileIconSource::FetchFileIcon( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame^] | 104 | const base::FilePath& path, |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 105 | ui::ScaleFactor scale_factor, |
| 106 | IconLoader::IconSize icon_size, |
| 107 | const content::URLDataSource::GotDataCallback& callback) { |
[email protected] | 66171ab | 2011-03-03 15:50:07 | [diff] [blame] | 108 | IconManager* im = g_browser_process->icon_manager(); |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 109 | gfx::Image* icon = im->LookupIcon(path, icon_size); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 110 | |
| 111 | if (icon) { |
[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 112 | scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 113 | gfx::PNGCodec::EncodeBGRASkBitmap( |
| 114 | icon->ToImageSkia()->GetRepresentation(scale_factor).sk_bitmap(), |
| 115 | false, &icon_data->data()); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 116 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 117 | callback.Run(icon_data); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 118 | } else { |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 119 | // Attach the ChromeURLDataManager request ID to the history request. |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 120 | IconRequestDetails details; |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 121 | details.callback = callback; |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 122 | details.scale_factor = scale_factor; |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 123 | |
| 124 | // Icon was not in cache, go fetch it slowly. |
| 125 | im->LoadIcon(path, |
| 126 | icon_size, |
| 127 | base::Bind(&FileIconSource::OnFileIconDataAvailable, |
| 128 | base::Unretained(this), details), |
| 129 | &cancelable_task_tracker_); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
[email protected] | 9225362 | 2013-01-14 20:40:50 | [diff] [blame] | 133 | std::string FileIconSource::GetSource() { |
| 134 | return kFileIconPath; |
| 135 | } |
| 136 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 137 | void FileIconSource::StartDataRequest( |
| 138 | const std::string& url_path, |
| 139 | bool is_incognito, |
| 140 | const content::URLDataSource::GotDataCallback& callback) { |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 141 | std::string query; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame^] | 142 | base::FilePath file_path; |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 143 | ui::ScaleFactor scale_factor; |
| 144 | IconLoader::IconSize icon_size; |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 145 | GetFilePathAndQuery(url_path, &file_path, &query); |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 146 | ParseQueryParams(query, &scale_factor, &icon_size); |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 147 | FetchFileIcon(file_path, scale_factor, icon_size, callback); |
[email protected] | 25cc750 | 2012-01-31 19:33:55 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 150 | std::string FileIconSource::GetMimeType(const std::string&) const { |
| 151 | // Rely on image decoder inferring the correct type. |
| 152 | return std::string(); |
| 153 | } |
| 154 | |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 155 | void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details, |
[email protected] | 66171ab | 2011-03-03 15:50:07 | [diff] [blame] | 156 | gfx::Image* icon) { |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 157 | if (icon) { |
[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 158 | scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); |
[email protected] | daf19c5 | 2012-07-13 19:01:32 | [diff] [blame] | 159 | gfx::PNGCodec::EncodeBGRASkBitmap( |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 160 | icon->ToImageSkia()->GetRepresentation(details.scale_factor) |
| 161 | .sk_bitmap(), |
| 162 | false, |
| 163 | &icon_data->data()); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 164 | |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 165 | details.callback.Run(icon_data); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 166 | } else { |
| 167 | // TODO(glen): send a dummy icon. |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 168 | details.callback.Run(NULL); |
[email protected] | 12e14fc | 2009-02-19 22:25:22 | [diff] [blame] | 169 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 170 | } |