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/fileicon_source.h" | 5 #include "chrome/browser/ui/webui/fileicon_source.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 IconLoader::IconSize SizeStringToIconSize(const std::string& size_string) { | 55 IconLoader::IconSize SizeStringToIconSize(const std::string& size_string) { |
| 56 if (size_string == "small") return IconLoader::SMALL; | 56 if (size_string == "small") return IconLoader::SMALL; |
| 57 if (size_string == "large") return IconLoader::LARGE; | 57 if (size_string == "large") return IconLoader::LARGE; |
| 58 // We default to NORMAL if we don't recognize the size_string. Including | 58 // We default to NORMAL if we don't recognize the size_string. Including |
| 59 // size_string=="normal". | 59 // size_string=="normal". |
| 60 return IconLoader::NORMAL; | 60 return IconLoader::NORMAL; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Simple parser for data on the query. | 63 // Simple parser for data on the query. |
| 64 void ParseQueryParams(const std::string& query, | 64 void ParseQueryParams(const std::string& query, |
| 65 ui::ScaleFactor* scale_factor, | 65 float* scale_factor, |
| 66 IconLoader::IconSize* icon_size) { | 66 IconLoader::IconSize* icon_size) { |
| 67 typedef std::pair<std::string, std::string> KVPair; | 67 typedef std::pair<std::string, std::string> KVPair; |
| 68 std::vector<KVPair> parameters; | 68 std::vector<KVPair> parameters; |
| 69 if (icon_size) | 69 if (icon_size) |
| 70 *icon_size = IconLoader::NORMAL; | 70 *icon_size = IconLoader::NORMAL; |
| 71 if (scale_factor) | 71 if (scale_factor) |
| 72 *scale_factor = ui::SCALE_FACTOR_100P; | 72 *scale_factor = 1.0f; |
| 73 base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters); | 73 base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters); |
| 74 for (std::vector<KVPair>::const_iterator iter = parameters.begin(); | 74 for (std::vector<KVPair>::const_iterator iter = parameters.begin(); |
| 75 iter != parameters.end(); ++iter) { | 75 iter != parameters.end(); ++iter) { |
| 76 if (icon_size && iter->first == kIconSize) | 76 if (icon_size && iter->first == kIconSize) |
| 77 *icon_size = SizeStringToIconSize(iter->second); | 77 *icon_size = SizeStringToIconSize(iter->second); |
| 78 else if (scale_factor && iter->first == kScaleFactor) | 78 else if (scale_factor && iter->first == kScaleFactor) |
| 79 webui::ParseScaleFactor(iter->second, scale_factor); | 79 webui::ParseScaleFactor(iter->second, scale_factor); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 } // namespace | 83 } // namespace |
| 84 | 84 |
| 85 FileIconSource::IconRequestDetails::IconRequestDetails() { | 85 FileIconSource::IconRequestDetails::IconRequestDetails() { |
|
pkotwicz
2014/05/20 03:31:19
The constructor should default initialize here
oshima
2014/05/20 18:23:28
Done.
| |
| 86 } | 86 } |
| 87 | 87 |
| 88 FileIconSource::IconRequestDetails::~IconRequestDetails() { | 88 FileIconSource::IconRequestDetails::~IconRequestDetails() { |
| 89 } | 89 } |
| 90 | 90 |
| 91 FileIconSource::FileIconSource() {} | 91 FileIconSource::FileIconSource() {} |
| 92 | 92 |
| 93 FileIconSource::~FileIconSource() {} | 93 FileIconSource::~FileIconSource() {} |
| 94 | 94 |
| 95 void FileIconSource::FetchFileIcon( | 95 void FileIconSource::FetchFileIcon( |
| 96 const base::FilePath& path, | 96 const base::FilePath& path, |
| 97 ui::ScaleFactor scale_factor, | 97 float scale_factor, |
| 98 IconLoader::IconSize icon_size, | 98 IconLoader::IconSize icon_size, |
| 99 const content::URLDataSource::GotDataCallback& callback) { | 99 const content::URLDataSource::GotDataCallback& callback) { |
| 100 IconManager* im = g_browser_process->icon_manager(); | 100 IconManager* im = g_browser_process->icon_manager(); |
| 101 gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size); | 101 gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size); |
| 102 | 102 |
| 103 if (icon) { | 103 if (icon) { |
| 104 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); | 104 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); |
| 105 gfx::PNGCodec::EncodeBGRASkBitmap( | 105 gfx::PNGCodec::EncodeBGRASkBitmap( |
| 106 icon->ToImageSkia()->GetRepresentation( | 106 icon->ToImageSkia() |
| 107 ui::GetImageScale(scale_factor)).sk_bitmap(), | 107 ->GetRepresentation(scale_factor) |
|
pkotwicz
2014/05/20 03:31:19
Nit: no new line. (Similar to how the code is stru
oshima
2014/05/20 18:23:28
Done.
| |
| 108 false, &icon_data->data()); | 108 .sk_bitmap(), |
| 109 false, | |
| 110 &icon_data->data()); | |
| 109 | 111 |
| 110 callback.Run(icon_data.get()); | 112 callback.Run(icon_data.get()); |
| 111 } else { | 113 } else { |
| 112 // Attach the ChromeURLDataManager request ID to the history request. | 114 // Attach the ChromeURLDataManager request ID to the history request. |
| 113 IconRequestDetails details; | 115 IconRequestDetails details; |
| 114 details.callback = callback; | 116 details.callback = callback; |
| 115 details.scale_factor = scale_factor; | 117 details.scale_factor = scale_factor; |
| 116 | 118 |
| 117 // Icon was not in cache, go fetch it slowly. | 119 // Icon was not in cache, go fetch it slowly. |
| 118 im->LoadIcon(path, | 120 im->LoadIcon(path, |
| 119 icon_size, | 121 icon_size, |
| 120 base::Bind(&FileIconSource::OnFileIconDataAvailable, | 122 base::Bind(&FileIconSource::OnFileIconDataAvailable, |
| 121 base::Unretained(this), details), | 123 base::Unretained(this), details), |
| 122 &cancelable_task_tracker_); | 124 &cancelable_task_tracker_); |
| 123 } | 125 } |
| 124 } | 126 } |
| 125 | 127 |
| 126 std::string FileIconSource::GetSource() const { | 128 std::string FileIconSource::GetSource() const { |
| 127 return kFileIconPath; | 129 return kFileIconPath; |
| 128 } | 130 } |
| 129 | 131 |
| 130 void FileIconSource::StartDataRequest( | 132 void FileIconSource::StartDataRequest( |
| 131 const std::string& url_path, | 133 const std::string& url_path, |
| 132 int render_process_id, | 134 int render_process_id, |
| 133 int render_frame_id, | 135 int render_frame_id, |
| 134 const content::URLDataSource::GotDataCallback& callback) { | 136 const content::URLDataSource::GotDataCallback& callback) { |
| 135 std::string query; | 137 std::string query; |
| 136 base::FilePath file_path; | 138 base::FilePath file_path; |
| 137 ui::ScaleFactor scale_factor; | |
| 138 IconLoader::IconSize icon_size; | 139 IconLoader::IconSize icon_size; |
| 140 float scale_factor; | |
|
pkotwicz
2014/05/20 03:31:19
Nit: default initialize
oshima
2014/05/20 18:23:28
Done.
| |
| 139 GetFilePathAndQuery(url_path, &file_path, &query); | 141 GetFilePathAndQuery(url_path, &file_path, &query); |
| 140 ParseQueryParams(query, &scale_factor, &icon_size); | 142 ParseQueryParams(query, &scale_factor, &icon_size); |
| 141 FetchFileIcon(file_path, scale_factor, icon_size, callback); | 143 FetchFileIcon(file_path, scale_factor, icon_size, callback); |
| 142 } | 144 } |
| 143 | 145 |
| 144 std::string FileIconSource::GetMimeType(const std::string&) const { | 146 std::string FileIconSource::GetMimeType(const std::string&) const { |
| 145 // Rely on image decoder inferring the correct type. | 147 // Rely on image decoder inferring the correct type. |
| 146 return std::string(); | 148 return std::string(); |
| 147 } | 149 } |
| 148 | 150 |
| 149 void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details, | 151 void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details, |
| 150 gfx::Image* icon) { | 152 gfx::Image* icon) { |
| 151 if (icon) { | 153 if (icon) { |
| 152 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); | 154 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); |
| 153 gfx::PNGCodec::EncodeBGRASkBitmap( | 155 gfx::PNGCodec::EncodeBGRASkBitmap( |
| 154 icon->ToImageSkia()->GetRepresentation( | 156 icon->ToImageSkia()->GetRepresentation( |
| 155 ui::GetImageScale(details.scale_factor)).sk_bitmap(), | 157 details.scale_factor).sk_bitmap(), |
| 156 false, | 158 false, |
| 157 &icon_data->data()); | 159 &icon_data->data()); |
| 158 | 160 |
| 159 details.callback.Run(icon_data.get()); | 161 details.callback.Run(icon_data.get()); |
| 160 } else { | 162 } else { |
| 161 // TODO(glen): send a dummy icon. | 163 // TODO(glen): send a dummy icon. |
| 162 details.callback.Run(NULL); | 164 details.callback.Run(NULL); |
| 163 } | 165 } |
| 164 } | 166 } |
| OLD | NEW |