| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/dom_ui/fileicon_source.h" | 5 #include "chrome/browser/dom_ui/fileicon_source.h" |
| 6 | 6 |
| 7 #include "base/gfx/png_encoder.h" | 7 #include "base/gfx/png_encoder.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/common/time_format.h" | 10 #include "chrome/common/time_format.h" |
| 11 #include "grit/generated_resources.h" | 11 #include "grit/generated_resources.h" |
| 12 #include "net/base/escape.h" |
| 12 | 13 |
| 13 // The path used in internal URLs to file icon data. | 14 // The path used in internal URLs to file icon data. |
| 14 static const char kFileIconPath[] = "fileicon"; | 15 static const char kFileIconPath[] = "fileicon"; |
| 15 | 16 |
| 16 FileIconSource::FileIconSource() | 17 FileIconSource::FileIconSource() |
| 17 : DataSource(kFileIconPath, MessageLoop::current()) {} | 18 : DataSource(kFileIconPath, MessageLoop::current()) {} |
| 18 | 19 |
| 19 FileIconSource::~FileIconSource() { | 20 FileIconSource::~FileIconSource() { |
| 20 cancelable_consumer_.CancelAllRequests(); | 21 cancelable_consumer_.CancelAllRequests(); |
| 21 } | 22 } |
| 22 | 23 |
| 23 void FileIconSource::StartDataRequest(const std::string& path, | 24 void FileIconSource::StartDataRequest(const std::string& path, |
| 24 int request_id) { | 25 int request_id) { |
| 25 IconManager* im = g_browser_process->icon_manager(); | 26 IconManager* im = g_browser_process->icon_manager(); |
| 26 | 27 |
| 28 // The path we receive has the wrong slashes and escaping for what we need; |
| 29 // this only appears to matter for getting icons from .exe files. |
| 30 std::string escaped_path = UnescapeURLComponent(path, UnescapeRule::SPACES); |
| 31 std::replace(escaped_path.begin(), escaped_path.end(), '/', '\\'); |
| 32 |
| 27 // Fast look up. | 33 // Fast look up. |
| 28 SkBitmap* icon = im->LookupIcon(UTF8ToWide(path), IconLoader::NORMAL); | 34 SkBitmap* icon = im->LookupIcon(UTF8ToWide(escaped_path), IconLoader::NORMAL); |
| 29 | 35 |
| 30 if (icon) { | 36 if (icon) { |
| 31 std::vector<unsigned char> png_bytes; | 37 std::vector<unsigned char> png_bytes; |
| 32 PNGEncoder::EncodeBGRASkBitmap(*icon, false, &png_bytes); | 38 PNGEncoder::EncodeBGRASkBitmap(*icon, false, &png_bytes); |
| 33 | 39 |
| 34 scoped_refptr<RefCountedBytes> icon_data = new RefCountedBytes(png_bytes); | 40 scoped_refptr<RefCountedBytes> icon_data = new RefCountedBytes(png_bytes); |
| 35 SendResponse(request_id, icon_data); | 41 SendResponse(request_id, icon_data); |
| 36 } else { | 42 } else { |
| 37 // Icon was not in cache, go fetch it slowly. | 43 // Icon was not in cache, go fetch it slowly. |
| 38 IconManager::Handle h = im->LoadIcon(UTF8ToWide(path), IconLoader::NORMAL, | 44 IconManager::Handle h = im->LoadIcon(UTF8ToWide(escaped_path), |
| 45 IconLoader::NORMAL, |
| 39 &cancelable_consumer_, | 46 &cancelable_consumer_, |
| 40 NewCallback(this, &FileIconSource::OnFileIconDataAvailable)); | 47 NewCallback(this, &FileIconSource::OnFileIconDataAvailable)); |
| 41 | 48 |
| 42 // Attach the ChromeURLDataManager request ID to the history request. | 49 // Attach the ChromeURLDataManager request ID to the history request. |
| 43 cancelable_consumer_.SetClientData(im, h, request_id); | 50 cancelable_consumer_.SetClientData(im, h, request_id); |
| 44 } | 51 } |
| 45 } | 52 } |
| 46 | 53 |
| 47 void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle, | 54 void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle, |
| 48 SkBitmap* icon) { | 55 SkBitmap* icon) { |
| 49 IconManager* im = g_browser_process->icon_manager(); | 56 IconManager* im = g_browser_process->icon_manager(); |
| 50 int request_id = cancelable_consumer_.GetClientData(im, handle); | 57 int request_id = cancelable_consumer_.GetClientData(im, handle); |
| 51 | 58 |
| 52 if (icon) { | 59 if (icon) { |
| 53 std::vector<unsigned char> png_bytes; | 60 std::vector<unsigned char> png_bytes; |
| 54 PNGEncoder::EncodeBGRASkBitmap(*icon, false, &png_bytes); | 61 PNGEncoder::EncodeBGRASkBitmap(*icon, false, &png_bytes); |
| 55 | 62 |
| 56 scoped_refptr<RefCountedBytes> icon_data = new RefCountedBytes(png_bytes); | 63 scoped_refptr<RefCountedBytes> icon_data = new RefCountedBytes(png_bytes); |
| 57 SendResponse(request_id, icon_data); | 64 SendResponse(request_id, icon_data); |
| 58 } else { | 65 } else { |
| 59 // TODO(glen): send a dummy icon. | 66 // TODO(glen): send a dummy icon. |
| 60 SendResponse(request_id, NULL); | 67 SendResponse(request_id, NULL); |
| 61 } | 68 } |
| 62 } | 69 } |
| OLD | NEW |