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