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/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/string_split.h" | 13 #include "base/string_split.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
17 #include "chrome/browser/ui/webui/web_ui_util.h" | 16 #include "chrome/browser/ui/webui/web_ui_util.h" |
18 #include "chrome/common/time_format.h" | 17 #include "chrome/common/time_format.h" |
19 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
20 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
21 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
22 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
23 #include "ui/gfx/codec/png_codec.h" | 22 #include "ui/gfx/codec/png_codec.h" |
24 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
25 #include "ui/gfx/image/image_skia.h" | 24 #include "ui/gfx/image/image_skia.h" |
26 | 25 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 iter != parameters.end(); ++iter) { | 83 iter != parameters.end(); ++iter) { |
85 if (icon_size && iter->first == kIconSize) | 84 if (icon_size && iter->first == kIconSize) |
86 *icon_size = SizeStringToIconSize(iter->second); | 85 *icon_size = SizeStringToIconSize(iter->second); |
87 else if (scale_factor && iter->first == kScaleFactor) | 86 else if (scale_factor && iter->first == kScaleFactor) |
88 web_ui_util::ParseScaleFactor(iter->second, scale_factor); | 87 web_ui_util::ParseScaleFactor(iter->second, scale_factor); |
89 } | 88 } |
90 } | 89 } |
91 | 90 |
92 } // namespace | 91 } // namespace |
93 | 92 |
| 93 FileIconSource::IconRequestDetails::IconRequestDetails() { |
| 94 } |
| 95 |
| 96 FileIconSource::IconRequestDetails::~IconRequestDetails() { |
| 97 } |
| 98 |
94 FileIconSource::FileIconSource() {} | 99 FileIconSource::FileIconSource() {} |
95 | 100 |
96 FileIconSource::~FileIconSource() {} | 101 FileIconSource::~FileIconSource() {} |
97 | 102 |
98 void FileIconSource::FetchFileIcon(const FilePath& path, | 103 void FileIconSource::FetchFileIcon( |
99 ui::ScaleFactor scale_factor, | 104 const FilePath& path, |
100 IconLoader::IconSize icon_size, | 105 ui::ScaleFactor scale_factor, |
101 int request_id) { | 106 IconLoader::IconSize icon_size, |
| 107 const content::URLDataSource::GotDataCallback& callback) { |
102 IconManager* im = g_browser_process->icon_manager(); | 108 IconManager* im = g_browser_process->icon_manager(); |
103 gfx::Image* icon = im->LookupIcon(path, icon_size); | 109 gfx::Image* icon = im->LookupIcon(path, icon_size); |
104 | 110 |
105 if (icon) { | 111 if (icon) { |
106 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); | 112 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); |
107 gfx::PNGCodec::EncodeBGRASkBitmap( | 113 gfx::PNGCodec::EncodeBGRASkBitmap( |
108 icon->ToImageSkia()->GetRepresentation(scale_factor).sk_bitmap(), | 114 icon->ToImageSkia()->GetRepresentation(scale_factor).sk_bitmap(), |
109 false, &icon_data->data()); | 115 false, &icon_data->data()); |
110 | 116 |
111 url_data_source()->SendResponse(request_id, icon_data); | 117 callback.Run(icon_data); |
112 } else { | 118 } else { |
113 // Attach the ChromeURLDataManager request ID to the history request. | 119 // Attach the ChromeURLDataManager request ID to the history request. |
114 IconRequestDetails details; | 120 IconRequestDetails details; |
115 details.request_id = request_id; | 121 details.callback = callback; |
116 details.scale_factor = scale_factor; | 122 details.scale_factor = scale_factor; |
117 | 123 |
118 // Icon was not in cache, go fetch it slowly. | 124 // Icon was not in cache, go fetch it slowly. |
119 im->LoadIcon(path, | 125 im->LoadIcon(path, |
120 icon_size, | 126 icon_size, |
121 base::Bind(&FileIconSource::OnFileIconDataAvailable, | 127 base::Bind(&FileIconSource::OnFileIconDataAvailable, |
122 base::Unretained(this), details), | 128 base::Unretained(this), details), |
123 &cancelable_task_tracker_); | 129 &cancelable_task_tracker_); |
124 } | 130 } |
125 } | 131 } |
126 | 132 |
127 std::string FileIconSource::GetSource() { | 133 std::string FileIconSource::GetSource() { |
128 return kFileIconPath; | 134 return kFileIconPath; |
129 } | 135 } |
130 | 136 |
131 void FileIconSource::StartDataRequest(const std::string& url_path, | 137 void FileIconSource::StartDataRequest( |
132 bool is_incognito, | 138 const std::string& url_path, |
133 int request_id) { | 139 bool is_incognito, |
| 140 const content::URLDataSource::GotDataCallback& callback) { |
134 std::string query; | 141 std::string query; |
135 FilePath file_path; | 142 FilePath file_path; |
136 ui::ScaleFactor scale_factor; | 143 ui::ScaleFactor scale_factor; |
137 IconLoader::IconSize icon_size; | 144 IconLoader::IconSize icon_size; |
138 GetFilePathAndQuery(url_path, &file_path, &query); | 145 GetFilePathAndQuery(url_path, &file_path, &query); |
139 ParseQueryParams(query, &scale_factor, &icon_size); | 146 ParseQueryParams(query, &scale_factor, &icon_size); |
140 FetchFileIcon(file_path, scale_factor, icon_size, request_id); | 147 FetchFileIcon(file_path, scale_factor, icon_size, callback); |
141 } | 148 } |
142 | 149 |
143 std::string FileIconSource::GetMimeType(const std::string&) const { | 150 std::string FileIconSource::GetMimeType(const std::string&) const { |
144 // Rely on image decoder inferring the correct type. | 151 // Rely on image decoder inferring the correct type. |
145 return std::string(); | 152 return std::string(); |
146 } | 153 } |
147 | 154 |
148 void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details, | 155 void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details, |
149 gfx::Image* icon) { | 156 gfx::Image* icon) { |
150 if (icon) { | 157 if (icon) { |
151 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); | 158 scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes); |
152 gfx::PNGCodec::EncodeBGRASkBitmap( | 159 gfx::PNGCodec::EncodeBGRASkBitmap( |
153 icon->ToImageSkia()->GetRepresentation(details.scale_factor) | 160 icon->ToImageSkia()->GetRepresentation(details.scale_factor) |
154 .sk_bitmap(), | 161 .sk_bitmap(), |
155 false, | 162 false, |
156 &icon_data->data()); | 163 &icon_data->data()); |
157 | 164 |
158 url_data_source()->SendResponse(details.request_id, icon_data); | 165 details.callback.Run(icon_data); |
159 } else { | 166 } else { |
160 // TODO(glen): send a dummy icon. | 167 // TODO(glen): send a dummy icon. |
161 url_data_source()->SendResponse(details.request_id, NULL); | 168 details.callback.Run(NULL); |
162 } | 169 } |
163 } | 170 } |
OLD | NEW |