Try my own class
This commit is contained in:
parent
8e4ed664d9
commit
d118fed5c2
7 changed files with 141 additions and 242 deletions
|
@ -17,7 +17,7 @@
|
|||
#include "base/files/file_util.h"
|
||||
#include "base/strings/pattern.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "chrome/browser/ui/webui/fileicon_source.h"
|
||||
#include "atom/common/fileicon_fetcher.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
#include "net/base/data_url.h"
|
||||
|
@ -306,7 +306,16 @@ gfx::Size NativeImage::GetSize() {
|
|||
void NativeImage::CreateFromFileIcon(v8::Isolate* isolate,
|
||||
const base::FilePath& path,
|
||||
const IconLoadedCallback& callback) {
|
||||
callback.Run(CreateEmpty(isolate));
|
||||
IconLoader::IconSize icon_size = IconLoader::IconSize::NORMAL;
|
||||
float scale_factor = 1.0f;
|
||||
auto onready = base::Bind(&NativeImage::OnIconLoaded, isolate, callback);
|
||||
FileIconFetcher::FetchFileIcon(path, scale_factor, icon_size, onready);
|
||||
}
|
||||
|
||||
void NativeImage::OnIconLoaded(v8::Isolate* isolate,
|
||||
const IconLoadedCallback& callback,
|
||||
gfx::Image& image) {
|
||||
callback.Run(Create(isolate, image));
|
||||
}
|
||||
|
||||
float NativeImage::GetAspectRatio() {
|
||||
|
|
|
@ -38,7 +38,7 @@ namespace atom {
|
|||
namespace api {
|
||||
|
||||
class NativeImage : public mate::Wrappable<NativeImage> {
|
||||
using IconLoadedCallback = base::Callback<void(mate::Handle<NativeImage>)>;
|
||||
using IconLoadedCallback = base::Callback<void(mate::Handle<NativeImage>)>;
|
||||
public:
|
||||
static mate::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
|
||||
static mate::Handle<NativeImage> Create(
|
||||
|
@ -90,6 +90,9 @@ using IconLoadedCallback = base::Callback<void(mate::Handle<NativeImage>)>;
|
|||
gfx::Size GetSize();
|
||||
float GetAspectRatio();
|
||||
|
||||
void OnIconLoaded(v8::Isolate* isolate,
|
||||
const IconLoadedCallback& callback,
|
||||
gfx::Image& image);
|
||||
// Mark the image as template image.
|
||||
void SetTemplateImage(bool setAsTemplate);
|
||||
// Determine if the image is a template image.
|
||||
|
|
76
atom/common/fileicon_fetcher.cc
Normal file
76
atom/common/fileicon_fetcher.cc
Normal file
|
@ -0,0 +1,76 @@
|
|||
// Copyright (c) 2016 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "chrome/browser/ui/webui/fileicon_source.h"
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/callback.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/memory/ref_counted_memory.h"
|
||||
#include "base/message_loop/message_loop.h"
|
||||
#include "base/strings/string_split.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "chrome/browser/browser_process.h"
|
||||
#include "net/base/escape.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
#include "ui/base/webui/web_ui_util.h"
|
||||
#include "ui/gfx/codec/png_codec.h"
|
||||
#include "ui/gfx/image/image.h"
|
||||
#include "ui/gfx/image/image_skia.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
void FileIconFetcher::FetchFileIcon(const base::FilePath& path,
|
||||
float scale_factor,
|
||||
IconLoader::IconSize icon_size,
|
||||
const IconFetchedCallback& callback) {
|
||||
IconManager* im = g_browser_process->icon_manager();
|
||||
gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size);
|
||||
|
||||
if (icon) {
|
||||
scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
|
||||
gfx::PNGCodec::EncodeBGRASkBitmap(
|
||||
icon->ToImageSkia()->GetRepresentation(scale_factor).sk_bitmap(),
|
||||
false,
|
||||
&icon_data->data());
|
||||
|
||||
callback.Run(icon_data.get());
|
||||
} else {
|
||||
// Attach the ChromeURLDataManager request ID to the history request.
|
||||
IconRequestDetails details;
|
||||
details.callback = callback;
|
||||
details.scale_factor = scale_factor;
|
||||
|
||||
// Icon was not in cache, go fetch it slowly.
|
||||
im->LoadIcon(path,
|
||||
icon_size,
|
||||
base::Bind(&FileIconFetcher::OnFileIconDataAvailable, details),
|
||||
&cancelable_task_tracker_);
|
||||
}
|
||||
}
|
||||
|
||||
void FileIconFetcher::OnFileIconDataAvailable(const IconRequestDetails& details,
|
||||
gfx::Image* icon) {
|
||||
if (icon) {
|
||||
scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
|
||||
gfx::PNGCodec::EncodeBGRASkBitmap(
|
||||
icon->ToImageSkia()->GetRepresentation(
|
||||
details.scale_factor).sk_bitmap(),
|
||||
false,
|
||||
&icon_data->data());
|
||||
|
||||
details.callback.Run(icon_data.get());
|
||||
} else {
|
||||
details.callback.Run(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FileIconFetcher::IconRequestDetails::IconRequestDetails() : scale_factor(1.0f) {
|
||||
}
|
||||
|
||||
FileIconFetcher::IconRequestDetails::IconRequestDetails(
|
||||
const IconRequestDetails& other) = default;
|
||||
|
||||
FileIconFetcher::IconRequestDetails::~IconRequestDetails() {
|
||||
}
|
48
atom/common/fileicon_fetcher.h
Normal file
48
atom/common/fileicon_fetcher.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Copyright (c) 2016 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_COMMON_API_FILEICON_FETCHER_H_
|
||||
#define ATOM_COMMON_API_FILEICON_FETCHER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/task/cancelable_task_tracker.h"
|
||||
#include "chrome/browser/icon_manager.h"
|
||||
#include "content/public/browser/url_data_source.h"
|
||||
|
||||
namespace gfx {
|
||||
class Image;
|
||||
}
|
||||
|
||||
class FileIconFetcher {
|
||||
using IconFetchedCallback = base::Callback<void(gfx::Image&)>;
|
||||
public:
|
||||
static void FetchFileIcon(const base::FilePath& path,
|
||||
float scale_factor,
|
||||
IconLoader::IconSize icon_size,
|
||||
const IconFetchedCallback& callback);
|
||||
|
||||
private:
|
||||
struct IconRequestDetails {
|
||||
IconRequestDetails();
|
||||
IconRequestDetails(const IconRequestDetails& other);
|
||||
~IconRequestDetails();
|
||||
|
||||
// The callback to run with the response.
|
||||
IconFetchedCallback callback;
|
||||
|
||||
// The requested scale factor to respond with.
|
||||
float scale_factor;
|
||||
};
|
||||
// Called when favicon data is available from the history backend.
|
||||
static void OnFileIconDataAvailable(const IconRequestDetails& details,
|
||||
gfx::Image* icon);
|
||||
|
||||
// Tracks tasks requesting file icons.
|
||||
base::CancelableTaskTracker cancelable_task_tracker_;
|
||||
};
|
||||
|
||||
#endif // ATOM_COMMON_API_FILEICON_FETCHER_H_
|
|
@ -1,164 +0,0 @@
|
|||
// Copyright (c) 2012 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.h"
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/callback.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/memory/ref_counted_memory.h"
|
||||
#include "base/message_loop/message_loop.h"
|
||||
#include "base/strings/string_split.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "chrome/browser/browser_process.h"
|
||||
#include "net/base/escape.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
#include "ui/base/webui/web_ui_util.h"
|
||||
#include "ui/gfx/codec/png_codec.h"
|
||||
#include "ui/gfx/image/image.h"
|
||||
#include "ui/gfx/image/image_skia.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
namespace {
|
||||
|
||||
typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap;
|
||||
|
||||
// The path used in internal URLs to file icon data.
|
||||
const char kFileIconPath[] = "fileicon";
|
||||
|
||||
// URL parameter specifying icon size.
|
||||
const char kIconSize[] = "iconsize";
|
||||
|
||||
// URL parameter specifying scale factor.
|
||||
const char kScaleFactor[] = "scale";
|
||||
|
||||
// Assuming the url is of the form '/path?query', convert the path portion into
|
||||
// a FilePath and return the resulting |file_path| and |query|. The path
|
||||
// portion may have been encoded using encodeURIComponent().
|
||||
void GetFilePathAndQuery(const std::string& url,
|
||||
base::FilePath* file_path,
|
||||
std::string* query) {
|
||||
// We receive the url with chrome://fileicon/ stripped but GURL expects it.
|
||||
const GURL gurl("chrome://fileicon/" + url);
|
||||
std::string path = net::UnescapeURLComponent(
|
||||
gurl.path().substr(1),
|
||||
net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS |
|
||||
net::UnescapeRule::PATH_SEPARATORS | net::UnescapeRule::SPACES);
|
||||
|
||||
*file_path = base::FilePath::FromUTF8Unsafe(path);
|
||||
*file_path = file_path->NormalizePathSeparators();
|
||||
query->assign(gurl.query());
|
||||
}
|
||||
|
||||
IconLoader::IconSize SizeStringToIconSize(const std::string& size_string) {
|
||||
if (size_string == "small") return IconLoader::SMALL;
|
||||
if (size_string == "large") return IconLoader::LARGE;
|
||||
// We default to NORMAL if we don't recognize the size_string. Including
|
||||
// size_string=="normal".
|
||||
return IconLoader::NORMAL;
|
||||
}
|
||||
|
||||
// Simple parser for data on the query.
|
||||
void ParseQueryParams(const std::string& query,
|
||||
float* scale_factor,
|
||||
IconLoader::IconSize* icon_size) {
|
||||
base::StringPairs parameters;
|
||||
if (icon_size)
|
||||
*icon_size = IconLoader::NORMAL;
|
||||
if (scale_factor)
|
||||
*scale_factor = 1.0f;
|
||||
base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters);
|
||||
for (base::StringPairs::const_iterator iter = parameters.begin();
|
||||
iter != parameters.end(); ++iter) {
|
||||
if (icon_size && iter->first == kIconSize)
|
||||
*icon_size = SizeStringToIconSize(iter->second);
|
||||
else if (scale_factor && iter->first == kScaleFactor)
|
||||
webui::ParseScaleFactor(iter->second, scale_factor);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
FileIconSource::IconRequestDetails::IconRequestDetails() : scale_factor(1.0f) {
|
||||
}
|
||||
|
||||
FileIconSource::IconRequestDetails::IconRequestDetails(
|
||||
const IconRequestDetails& other) = default;
|
||||
|
||||
FileIconSource::IconRequestDetails::~IconRequestDetails() {
|
||||
}
|
||||
|
||||
FileIconSource::FileIconSource() {}
|
||||
|
||||
FileIconSource::~FileIconSource() {}
|
||||
|
||||
void FileIconSource::FetchFileIcon(
|
||||
const base::FilePath& path,
|
||||
float scale_factor,
|
||||
IconLoader::IconSize icon_size,
|
||||
const content::URLDataSource::GotDataCallback& callback) {
|
||||
IconManager* im = g_browser_process->icon_manager();
|
||||
gfx::Image* icon = im->LookupIconFromFilepath(path, icon_size);
|
||||
|
||||
if (icon) {
|
||||
scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
|
||||
gfx::PNGCodec::EncodeBGRASkBitmap(
|
||||
icon->ToImageSkia()->GetRepresentation(scale_factor).sk_bitmap(),
|
||||
false,
|
||||
&icon_data->data());
|
||||
|
||||
callback.Run(icon_data.get());
|
||||
} else {
|
||||
// Attach the ChromeURLDataManager request ID to the history request.
|
||||
IconRequestDetails details;
|
||||
details.callback = callback;
|
||||
details.scale_factor = scale_factor;
|
||||
|
||||
// Icon was not in cache, go fetch it slowly.
|
||||
im->LoadIcon(path,
|
||||
icon_size,
|
||||
base::Bind(&FileIconSource::OnFileIconDataAvailable,
|
||||
base::Unretained(this), details),
|
||||
&cancelable_task_tracker_);
|
||||
}
|
||||
}
|
||||
|
||||
std::string FileIconSource::GetSource() const {
|
||||
return kFileIconPath;
|
||||
}
|
||||
|
||||
// void FileIconSource::StartDataRequest(
|
||||
// const std::string& url_path,
|
||||
// const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
|
||||
// const content::URLDataSource::GotDataCallback& callback) {
|
||||
// std::string query;
|
||||
// base::FilePath file_path;
|
||||
// IconLoader::IconSize icon_size;
|
||||
// float scale_factor = 1.0f;
|
||||
// GetFilePathAndQuery(url_path, &file_path, &query);
|
||||
// ParseQueryParams(query, &scale_factor, &icon_size);
|
||||
// FetchFileIcon(file_path, scale_factor, icon_size, callback);
|
||||
// }
|
||||
|
||||
std::string FileIconSource::GetMimeType(const std::string&) const {
|
||||
// Rely on image decoder inferring the correct type.
|
||||
return std::string();
|
||||
}
|
||||
|
||||
void FileIconSource::OnFileIconDataAvailable(const IconRequestDetails& details,
|
||||
gfx::Image* icon) {
|
||||
if (icon) {
|
||||
scoped_refptr<base::RefCountedBytes> icon_data(new base::RefCountedBytes);
|
||||
gfx::PNGCodec::EncodeBGRASkBitmap(
|
||||
icon->ToImageSkia()->GetRepresentation(
|
||||
details.scale_factor).sk_bitmap(),
|
||||
false,
|
||||
&icon_data->data());
|
||||
|
||||
details.callback.Run(icon_data.get());
|
||||
} else {
|
||||
// TODO(glen): send a dummy icon.
|
||||
details.callback.Run(NULL);
|
||||
}
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
// Copyright (c) 2012 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_H_
|
||||
#define CHROME_BROWSER_UI_WEBUI_FILEICON_SOURCE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/task/cancelable_task_tracker.h"
|
||||
#include "chrome/browser/icon_manager.h"
|
||||
#include "content/public/browser/url_data_source.h"
|
||||
|
||||
namespace gfx {
|
||||
class Image;
|
||||
}
|
||||
|
||||
namespace content {
|
||||
class ResourceRequestInfo;
|
||||
}
|
||||
|
||||
// FileIconSource is the gateway between network-level chrome:
|
||||
// requests for favicons and the history backend that serves these.
|
||||
class FileIconSource : public content::URLDataSource {
|
||||
public:
|
||||
FileIconSource();
|
||||
|
||||
// content::URLDataSource implementation.
|
||||
std::string GetSource() const override;
|
||||
// void StartDataRequest(
|
||||
// const std::string& path,
|
||||
// const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
|
||||
// const content::URLDataSource::GotDataCallback& callback) override;
|
||||
std::string GetMimeType(const std::string&) const override;
|
||||
|
||||
protected:
|
||||
~FileIconSource() override;
|
||||
|
||||
// Once the |path| and |icon_size| has been determined from the request, this
|
||||
// function is called to perform the actual fetch. Declared as virtual for
|
||||
// testing.
|
||||
virtual void FetchFileIcon(
|
||||
const base::FilePath& path,
|
||||
float scale_factor,
|
||||
IconLoader::IconSize icon_size,
|
||||
const content::URLDataSource::GotDataCallback& callback);
|
||||
|
||||
private:
|
||||
// Contains the necessary information for completing an icon fetch request.
|
||||
struct IconRequestDetails {
|
||||
IconRequestDetails();
|
||||
IconRequestDetails(const IconRequestDetails& other);
|
||||
~IconRequestDetails();
|
||||
|
||||
// The callback to run with the response.
|
||||
content::URLDataSource::GotDataCallback callback;
|
||||
|
||||
// The requested scale factor to respond with.
|
||||
float scale_factor;
|
||||
};
|
||||
|
||||
// Called when favicon data is available from the history backend.
|
||||
void OnFileIconDataAvailable(const IconRequestDetails& details,
|
||||
gfx::Image* icon);
|
||||
|
||||
// Tracks tasks requesting file icons.
|
||||
base::CancelableTaskTracker cancelable_task_tracker_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(FileIconSource);
|
||||
};
|
||||
#endif // CHROME_BROWSER_UI_WEBUI_FILEICON_SOURCE_H_
|
|
@ -404,6 +404,8 @@
|
|||
'atom/common/crash_reporter/win/crash_service_main.h',
|
||||
'atom/common/draggable_region.cc',
|
||||
'atom/common/draggable_region.h',
|
||||
'atom/common/fileicon_fetcher.cc',
|
||||
'atom/common/fileicon_fetcher.h',
|
||||
'atom/common/google_api_key.h',
|
||||
'atom/common/key_weak_map.h',
|
||||
'atom/common/keyboard_util.cc',
|
||||
|
@ -547,8 +549,6 @@
|
|||
'chromium_src/chrome/browser/ui/views/color_chooser_aura.h',
|
||||
'chromium_src/chrome/browser/ui/views/frame/global_menu_bar_registrar_x11.cc',
|
||||
'chromium_src/chrome/browser/ui/views/frame/global_menu_bar_registrar_x11.h',
|
||||
'chromium_src/chrome/browser/ui/webui/fileicon_source.cc',
|
||||
'chromium_src/chrome/browser/ui/webui/fileicon_source.h',
|
||||
'chromium_src/chrome/common/chrome_constants.cc',
|
||||
'chromium_src/chrome/common/chrome_constants.h',
|
||||
'chromium_src/chrome/common/chrome_paths.cc',
|
||||
|
|
Loading…
Reference in a new issue