diff options
-rw-r--r-- | chrome/browser/ui/webui/downloads_dom_handler.cc | 5 | ||||
-rw-r--r-- | chrome/browser/ui/webui/fileicon_source_cros.cc | 242 | ||||
-rw-r--r-- | chrome/browser/ui/webui/fileicon_source_cros.h | 40 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 4 |
4 files changed, 291 insertions, 0 deletions
diff --git a/chrome/browser/ui/webui/downloads_dom_handler.cc b/chrome/browser/ui/webui/downloads_dom_handler.cc index 7520933..1f9b9d0 100644 --- a/chrome/browser/ui/webui/downloads_dom_handler.cc +++ b/chrome/browser/ui/webui/downloads_dom_handler.cc @@ -21,6 +21,7 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/webui/chrome_url_data_manager.h" #include "chrome/browser/ui/webui/fileicon_source.h" +#include "chrome/browser/ui/webui/fileicon_source_cros.h" #include "chrome/common/jstemplate_builder.h" #include "chrome/common/url_constants.h" #include "content/browser/browser_thread.h" @@ -53,7 +54,11 @@ DownloadsDOMHandler::DownloadsDOMHandler(DownloadManager* dlm) callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { // Create our fileicon data source. dlm->profile()->GetChromeURLDataManager()->AddDataSource( +#if defined(OS_CHROMEOS) + new FileIconSourceCros()); +#else new FileIconSource()); +#endif // OS_CHROMEOS } DownloadsDOMHandler::~DownloadsDOMHandler() { diff --git a/chrome/browser/ui/webui/fileicon_source_cros.cc b/chrome/browser/ui/webui/fileicon_source_cros.cc new file mode 100644 index 0000000..b522793 --- /dev/null +++ b/chrome/browser/ui/webui/fileicon_source_cros.cc @@ -0,0 +1,242 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/webui/fileicon_source_cros.h" + +#include <map> +#include <utility> +#include <vector> + +#include "base/file_util.h" +#include "base/memory/singleton.h" +#include "base/string_split.h" +#include "base/string_util.h" +#include "base/threading/thread_restrictions.h" +#include "chrome/browser/icon_loader.h" +#include "chrome/browser/io_thread.h" +#include "chrome/browser/ui/webui/chrome_url_data_manager.h" +#include "chrome/common/url_constants.h" +#include "content/browser/browser_thread.h" +#include "googleurl/src/gurl.h" +#include "grit/app_resources.h" +#include "grit/component_extension_resources.h" +#include "grit/component_extension_resources_map.h" +#include "grit/generated_resources.h" +#include "grit/theme_resources.h" +#include "net/base/mime_util.h" +#include "ui/base/resource/resource_bundle.h" + +namespace { + +struct IdrBySize { + int idr_small_; + int idr_normal_; + int idr_large_; +}; + +typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap; +typedef std::map<std::string, IdrBySize> ExtensionIconSizeMap; + +const char *kIconSize = "iconsize"; + +QueryIconSizeMap::value_type kQueryIconSizeData[] = { + std::make_pair("small", IconLoader::SMALL), + std::make_pair("normal", IconLoader::NORMAL), + std::make_pair("large", IconLoader::LARGE) +}; + +const IdrBySize kAudioIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_AUDIO, + IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_AUDIO, + IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_AUDIO +}; +const IdrBySize kDocIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_DOC, + IDR_FILE_MANAGER_IMG_FILETYPE_DOC, + IDR_FILE_MANAGER_IMG_FILETYPE_DOC +}; +const IdrBySize kFolderIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_FOLDER, + IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_FOLDER, + IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_FOLDER +}; +const IdrBySize kGenericIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_GENERIC, + IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_GENERIC, + IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_GENERIC +}; +const IdrBySize kHtmlIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_HTML, + IDR_FILE_MANAGER_IMG_FILETYPE_HTML, + IDR_FILE_MANAGER_IMG_FILETYPE_HTML +}; +const IdrBySize kImageIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_IMAGE, + IDR_FILE_MANAGER_IMG_FILETYPE_IMAGE, + IDR_FILE_MANAGER_IMG_FILETYPE_IMAGE +}; +const IdrBySize kPdfIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_PDF, + IDR_FILE_MANAGER_IMG_FILETYPE_PDF, + IDR_FILE_MANAGER_IMG_FILETYPE_PDF +}; +const IdrBySize kPresentationIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_PRESENTATION, + IDR_FILE_MANAGER_IMG_FILETYPE_PRESENTATION, + IDR_FILE_MANAGER_IMG_FILETYPE_PRESENTATION +}; +const IdrBySize kSpreadsheetIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_SPREADSHEET, + IDR_FILE_MANAGER_IMG_FILETYPE_SPREADSHEET, + IDR_FILE_MANAGER_IMG_FILETYPE_SPREADSHEET +}; +const IdrBySize kTextIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_TEXT, + IDR_FILE_MANAGER_IMG_FILETYPE_TEXT, + IDR_FILE_MANAGER_IMG_FILETYPE_TEXT +}; +const IdrBySize kVideoIdrs = { + IDR_FILE_MANAGER_IMG_FILETYPE_VIDEO, + IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_VIDEO, + IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_VIDEO +}; + +// The code below should match translation in +// chrome/browser/resources/file_manager/js/file_manager.js +// chrome/browser/resources/file_manager/css/file_manager.css +// 'audio': /\.(mp3|m4a|oga|ogg|wav)$/i, +// 'html': /\.(html?)$/i, +// 'image': /\.(bmp|gif|jpe?g|ico|png|webp)$/i, +// 'pdf' : /\.(pdf)$/i, +// 'text': /\.(pod|rst|txt|log)$/i, +// 'video': /\.(mov|mp4|m4v|mpe?g4?|ogm|ogv|ogx|webm)$/i + +const ExtensionIconSizeMap::value_type kExtensionIdrBySizeData[] = { + std::make_pair(".m4a", kAudioIdrs), + std::make_pair(".mp3", kAudioIdrs), + std::make_pair(".oga", kAudioIdrs), + std::make_pair(".ogg", kAudioIdrs), + std::make_pair(".wav", kAudioIdrs), +// std::make_pair(".doc", kIdrDoc), + std::make_pair(".htm", kHtmlIdrs), + std::make_pair(".html", kHtmlIdrs), + std::make_pair(".bmp", kImageIdrs), + std::make_pair(".gif", kImageIdrs), + std::make_pair(".ico", kImageIdrs), + std::make_pair(".jpeg", kImageIdrs), + std::make_pair(".jpg", kImageIdrs), + std::make_pair(".png", kImageIdrs), + std::make_pair(".webp", kImageIdrs), + std::make_pair(".pdf", kPdfIdrs), + std::make_pair(".log", kTextIdrs), + std::make_pair(".pod", kTextIdrs), + std::make_pair(".rst", kTextIdrs), + std::make_pair(".txt", kTextIdrs), + std::make_pair(".m4v", kVideoIdrs), + std::make_pair(".mov", kVideoIdrs), + std::make_pair(".mp4", kVideoIdrs), + std::make_pair(".mpeg", kVideoIdrs), + std::make_pair(".mpg", kVideoIdrs), + std::make_pair(".mpeg4", kVideoIdrs), + std::make_pair(".mpg4", kVideoIdrs), + std::make_pair(".ogm", kVideoIdrs), + std::make_pair(".ogv", kVideoIdrs), + std::make_pair(".ogx", kVideoIdrs), + std::make_pair(".webm", kVideoIdrs), +}; + +const size_t kQSize = arraysize(kQueryIconSizeData); + +const QueryIconSizeMap kQueryIconSizeMap(&kQueryIconSizeData[0], + &kQueryIconSizeData[kQSize]); + +const size_t kESize = arraysize(kExtensionIdrBySizeData); + +const ExtensionIconSizeMap kExtensionIdrSizeMap( + &kExtensionIdrBySizeData[0], + &kExtensionIdrBySizeData[kESize]); + +// Split on the very first &. The first part is path, the rest query. +void GetExtensionAndQuery(const std::string& url, + std::string* extension, + std::string* query) { + // We receive the url with chrome://fileicon/ stripped but GURL expects it. + const GURL gurl("chrome://fileicon/" + url); + const std::string path = gurl.path(); + *extension = StringToLowerASCII(FilePath().AppendASCII(path).Extension()); + *query = gurl.query(); +} + +// Simple parser for data on the query. +IconLoader::IconSize QueryToIconSize(const std::string& query) { + typedef std::pair<std::string, std::string> KVPair; + std::vector<KVPair> parameters; + if (base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters)) { + for (std::vector<KVPair>::const_iterator itk = parameters.begin(); + itk != parameters.end(); ++itk) { + if (itk->first == kIconSize) { + QueryIconSizeMap::const_iterator itq( + kQueryIconSizeMap.find(itk->second)); + if (itq != kQueryIconSizeMap.end()) + return itq->second; + } + } + } + return IconLoader::NORMAL; +} + +// Finds matching resource of proper size. Fallback to generic. +int UrlToIDR(const std::string& url) { + std::string extension, query; + int idr = -1; + GetExtensionAndQuery(url, &extension, &query); + const IconLoader::IconSize size = QueryToIconSize(query); + ExtensionIconSizeMap::const_iterator it = + kExtensionIdrSizeMap.find(extension); + if (it != kExtensionIdrSizeMap.end()) { + IdrBySize idrbysize = it->second; + if (size == IconLoader::SMALL) { + idr = idrbysize.idr_small_; + } else if (size == IconLoader::NORMAL) { + idr = idrbysize.idr_normal_; + } else if (size == IconLoader::LARGE) { + idr = idrbysize.idr_large_; + } + } + DCHECK_NE(-1, idr) << " Missing fileicon for: " << url; + if (idr == -1) { + if (size == IconLoader::SMALL) { + idr = kGenericIdrs.idr_small_; + } else if (size == IconLoader::NORMAL) { + idr = kGenericIdrs.idr_normal_; + } else { + idr = kGenericIdrs.idr_large_; + } + } + return idr; +} +} // namespace + +FileIconSourceCros::FileIconSourceCros() + : DataSource("fileicon", NULL) { +} + +FileIconSourceCros::~FileIconSourceCros() { +} + +void FileIconSourceCros::StartDataRequest(const std::string& url, + bool is_incognito, + int request_id) { + int idr = UrlToIDR(url); + const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + scoped_refptr<RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes(idr)); + SendResponse(request_id, bytes); +} + +// The mime type refers to the type of the response/icon served. +std::string FileIconSourceCros::GetMimeType( + const std::string& url) const { + return "image/png"; +} + diff --git a/chrome/browser/ui/webui/fileicon_source_cros.h b/chrome/browser/ui/webui/fileicon_source_cros.h new file mode 100644 index 0000000..9a5a80e --- /dev/null +++ b/chrome/browser/ui/webui/fileicon_source_cros.h @@ -0,0 +1,40 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_WEBUI_FILEICON_SOURCE_CROS_H_ +#define CHROME_BROWSER_UI_WEBUI_FILEICON_SOURCE_CROS_H_ +#pragma once + +#include <string> +#include "base/compiler_specific.h" +#include "chrome/browser/ui/webui/chrome_url_data_manager.h" + +namespace net { +class URLRequest; +class URLRequestJob; +} // namespace net + +class GURL; + +// A DataSource for chrome://fileicon/ URLs, but reads from +// resources instead of disk. Allows for parameterization +// via chrome://fileicon/path.ext?size=[small|medium|large] +class FileIconSourceCros : public ChromeURLDataManager::DataSource { + public: + FileIconSourceCros(); + + // Overridden from ChromeURLDataManager::DataSource. + virtual void StartDataRequest(const std::string& url, + bool is_incognito, + int request_id) OVERRIDE; + virtual std::string GetMimeType(const std::string&) const OVERRIDE; + + private: + virtual ~FileIconSourceCros(); + + DISALLOW_COPY_AND_ASSIGN(FileIconSourceCros); +}; + +#endif // CHROME_BROWSER_UI_WEBUI_FILEICON_SOURCE_CROS_H_ + diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 326ba11..c176df2 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -3325,6 +3325,8 @@ 'browser/ui/webui/favicon_source.h', 'browser/ui/webui/fileicon_source.cc', 'browser/ui/webui/fileicon_source.h', + 'browser/ui/webui/fileicon_source_cros.h', + 'browser/ui/webui/fileicon_source_cros.cc', 'browser/ui/webui/flags_ui.cc', 'browser/ui/webui/flags_ui.h', 'browser/ui/webui/gpu_internals_ui.cc', @@ -3571,6 +3573,8 @@ ['exclude', 'browser/ui/webui/collected_cookies_ui_delegate.h'], ['exclude', 'browser/ui/webui/cookies_tree_model_adapter.cc'], ['exclude', 'browser/ui/webui/cookies_tree_model_adapter.h'], + ['exclude', 'browser/ui/webui/fileicon_source_cros.cc'], + ['exclude', 'browser/ui/webui/fileicon_source_cros.h'], ['exclude', 'browser/ui/webui/mediaplayer_ui.cc'], ], }], |