Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/callback.h" | 8 #include "base/callback.h" |
| 8 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/string_split.h" | |
| 10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/common/time_format.h" | 14 #include "chrome/common/time_format.h" |
| 15 #include "googleurl/src/gurl.h" | |
| 13 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 14 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/gfx/codec/png_codec.h" | 19 #include "ui/gfx/codec/png_codec.h" |
| 17 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
| 18 | 21 |
| 22 namespace { | |
| 23 | |
| 24 typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap; | |
| 25 | |
| 19 // The path used in internal URLs to file icon data. | 26 // The path used in internal URLs to file icon data. |
| 20 static const char kFileIconPath[] = "fileicon"; | 27 const char kFileIconPath[] = "fileicon"; |
| 28 | |
| 29 // URL parameter specifying icon size. | |
| 30 const char kIconSize[] = "iconsize"; | |
| 31 | |
| 32 void GetPathAndQuery(const std::string& url, | |
| 33 std::string* path, | |
| 34 std::string* query) { | |
| 35 // We receive the url with chrome://fileicon/ stripped but GURL expects it. | |
| 36 const GURL gurl("chrome://fileicon/" + url); | |
| 37 path->assign(net::UnescapeURLComponent( | |
| 38 gurl.path().substr(1), (net::UnescapeRule::URL_SPECIAL_CHARS | | |
| 39 net::UnescapeRule::SPACES))); | |
| 40 query->assign(gurl.query()); | |
| 41 } | |
| 42 | |
| 43 QueryIconSizeMap BuildQueryIconSizeMap() { | |
| 44 QueryIconSizeMap::value_type kQueryIconSizeData[] = { | |
| 45 std::make_pair("small", IconLoader::SMALL), | |
| 46 std::make_pair("normal", IconLoader::NORMAL), | |
| 47 std::make_pair("large", IconLoader::LARGE) | |
| 48 }; | |
| 49 | |
| 50 size_t kQSize = arraysize(kQueryIconSizeData); | |
| 51 return QueryIconSizeMap(&kQueryIconSizeData[0], | |
| 52 &kQueryIconSizeData[kQSize]); | |
| 53 } | |
| 54 | |
| 55 // Simple parser for data on the query. | |
| 56 IconLoader::IconSize QueryToIconSize(const std::string& query) { | |
| 57 CR_DEFINE_STATIC_LOCAL( | |
| 58 QueryIconSizeMap, kQueryIconSizeMap, (BuildQueryIconSizeMap())); | |
| 59 typedef std::pair<std::string, std::string> KVPair; | |
| 60 std::vector<KVPair> parameters; | |
| 61 if (base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters)) { | |
| 62 for (std::vector<KVPair>::const_iterator itk = parameters.begin(); | |
| 63 itk != parameters.end(); ++itk) { | |
| 64 if (itk->first == kIconSize) { | |
| 65 QueryIconSizeMap::const_iterator itq( | |
| 66 kQueryIconSizeMap.find(itk->second)); | |
| 67 if (itq != kQueryIconSizeMap.end()) | |
| 68 return itq->second; | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 return IconLoader::NORMAL; | |
| 73 } | |
| 74 | |
| 75 } | |
| 21 | 76 |
| 22 FileIconSource::FileIconSource() | 77 FileIconSource::FileIconSource() |
| 23 : DataSource(kFileIconPath, MessageLoop::current()) {} | 78 : DataSource(kFileIconPath, MessageLoop::current()) {} |
| 24 | 79 |
| 25 FileIconSource::~FileIconSource() { | 80 FileIconSource::~FileIconSource() { |
| 26 cancelable_consumer_.CancelAllRequests(); | 81 cancelable_consumer_.CancelAllRequests(); |
| 27 } | 82 } |
| 28 | 83 |
| 29 void FileIconSource::StartDataRequest(const std::string& path, | 84 void FileIconSource::FetchFileIcon(const FilePath& path, |
| 30 bool is_incognito, | 85 IconLoader::IconSize icon_size, |
| 31 int request_id) { | 86 int request_id) { |
| 32 std::string escaped_path = net::UnescapeURLComponent(path, | |
| 33 net::UnescapeRule::SPACES); | |
| 34 #if defined(OS_WIN) | |
| 35 // The path we receive has the wrong slashes and escaping for what we need; | |
| 36 // this only appears to matter for getting icons from .exe files. | |
| 37 std::replace(escaped_path.begin(), escaped_path.end(), '/', '\\'); | |
| 38 FilePath escaped_filepath(UTF8ToWide(escaped_path)); | |
| 39 #elif defined(OS_POSIX) | |
| 40 // The correct encoding on Linux may not actually be UTF8. | |
| 41 FilePath escaped_filepath(escaped_path); | |
| 42 #endif | |
| 43 | |
| 44 IconManager* im = g_browser_process->icon_manager(); | 87 IconManager* im = g_browser_process->icon_manager(); |
| 45 gfx::Image* icon = im->LookupIcon(escaped_filepath, IconLoader::NORMAL); | 88 gfx::Image* icon = im->LookupIcon(path, icon_size); |
|
ilja
2012/01/22 02:31:43
Shouldn't that still be named "escaped_filepath"?
asanka
2012/01/23 19:16:47
The FilePath argument here shouldn't contain any e
| |
| 46 | 89 |
| 47 if (icon) { | 90 if (icon) { |
| 48 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); | 91 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); |
| 49 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); | 92 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); |
| 50 | 93 |
| 51 SendResponse(request_id, icon_data); | 94 SendResponse(request_id, icon_data); |
| 52 } else { | 95 } else { |
| 53 // Icon was not in cache, go fetch it slowly. | 96 // Icon was not in cache, go fetch it slowly. |
| 54 IconManager::Handle h = im->LoadIcon(escaped_filepath, | 97 IconManager::Handle h = im->LoadIcon( |
| 55 IconLoader::NORMAL, | 98 path, icon_size, &cancelable_consumer_, |
| 56 &cancelable_consumer_, | |
| 57 base::Bind(&FileIconSource::OnFileIconDataAvailable, | 99 base::Bind(&FileIconSource::OnFileIconDataAvailable, |
| 58 base::Unretained(this))); | 100 base::Unretained(this))); |
| 59 | 101 |
| 60 // Attach the ChromeURLDataManager request ID to the history request. | 102 // Attach the ChromeURLDataManager request ID to the history request. |
| 61 cancelable_consumer_.SetClientData(im, h, request_id); | 103 cancelable_consumer_.SetClientData(im, h, request_id); |
| 62 } | 104 } |
| 63 } | 105 } |
| 64 | 106 |
| 107 void FileIconSource::StartDataRequest(const std::string& path, | |
| 108 bool is_incognito, | |
| 109 int request_id) { | |
| 110 std::string query; | |
| 111 std::string escaped_path; | |
| 112 GetPathAndQuery(path, &escaped_path, &query); | |
| 113 #if defined(OS_WIN) | |
| 114 // The path we receive has the wrong slashes and escaping for what we need; | |
| 115 // this only appears to matter for getting icons from .exe files. | |
| 116 std::replace(escaped_path.begin(), escaped_path.end(), '/', '\\'); | |
| 117 FilePath escaped_filepath(UTF8ToWide(escaped_path)); | |
| 118 #elif defined(OS_POSIX) | |
| 119 // The correct encoding on Linux may not actually be UTF8. | |
| 120 FilePath escaped_filepath(escaped_path); | |
| 121 #endif | |
| 122 FetchFileIcon(escaped_filepath, QueryToIconSize(query), request_id); | |
| 123 } | |
| 124 | |
| 65 std::string FileIconSource::GetMimeType(const std::string&) const { | 125 std::string FileIconSource::GetMimeType(const std::string&) const { |
| 66 // Rely on image decoder inferring the correct type. | 126 // Rely on image decoder inferring the correct type. |
| 67 return std::string(); | 127 return std::string(); |
| 68 } | 128 } |
| 69 | 129 |
| 70 void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle, | 130 void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle, |
| 71 gfx::Image* icon) { | 131 gfx::Image* icon) { |
| 72 IconManager* im = g_browser_process->icon_manager(); | 132 IconManager* im = g_browser_process->icon_manager(); |
| 73 int request_id = cancelable_consumer_.GetClientData(im, handle); | 133 int request_id = cancelable_consumer_.GetClientData(im, handle); |
| 74 | 134 |
| 75 if (icon) { | 135 if (icon) { |
| 76 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); | 136 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); |
| 77 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); | 137 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); |
| 78 | 138 |
| 79 SendResponse(request_id, icon_data); | 139 SendResponse(request_id, icon_data); |
| 80 } else { | 140 } else { |
| 81 // TODO(glen): send a dummy icon. | 141 // TODO(glen): send a dummy icon. |
| 82 SendResponse(request_id, NULL); | 142 SendResponse(request_id, NULL); |
| 83 } | 143 } |
| 84 } | 144 } |
| OLD | NEW |