Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / webui / fallback_icon_source.cc
blob6f391bff1be7c2e691cd4ac59a064c12e0f3e5f0
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/fallback_icon_source.h"
7 #include <vector>
9 #include "base/memory/ref_counted_memory.h"
10 #include "chrome/browser/search/instant_io_context.h"
11 #include "chrome/common/url_constants.h"
12 #include "components/favicon/core/fallback_icon_service.h"
13 #include "components/favicon_base/fallback_icon_url_parser.h"
14 #include "components/keyed_service/core/service_access_type.h"
15 #include "net/url_request/url_request.h"
16 #include "ui/gfx/favicon_size.h"
17 #include "url/gurl.h"
19 FallbackIconSource::FallbackIconSource(
20 favicon::FallbackIconService* fallback_icon_service)
21 : fallback_icon_service_(fallback_icon_service) {
24 FallbackIconSource::~FallbackIconSource() {
27 std::string FallbackIconSource::GetSource() const {
28 return chrome::kChromeUIFallbackIconHost;
31 void FallbackIconSource::StartDataRequest(
32 const std::string& path,
33 int render_process_id,
34 int render_frame_id,
35 const content::URLDataSource::GotDataCallback& callback) {
36 chrome::ParsedFallbackIconPath parsed;
37 bool success = parsed.Parse(path);
38 if (!success) {
39 SendDefaultResponse(callback);
40 return;
43 GURL url(parsed.url_string());
44 if (url.is_empty() || !url.is_valid()) {
45 SendDefaultResponse(callback);
46 return;
49 SendFallbackIconHelper(
50 url, parsed.size_in_pixels(), parsed.style(), callback);
53 std::string FallbackIconSource::GetMimeType(const std::string&) const {
54 // We need to explicitly return a mime type, otherwise if the user tries to
55 // drag the image they get no extension.
56 return "image/png";
59 bool FallbackIconSource::ShouldReplaceExistingSource() const {
60 // Leave the existing DataSource in place, otherwise we'll drop any pending
61 // requests on the floor.
62 return false;
65 bool FallbackIconSource::ShouldServiceRequest(
66 const net::URLRequest* request) const {
67 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
68 return InstantIOContext::ShouldServiceRequest(request);
69 return URLDataSource::ShouldServiceRequest(request);
72 void FallbackIconSource::SendFallbackIconHelper(
73 const GURL& url,
74 int size_in_pixels,
75 const favicon_base::FallbackIconStyle& style,
76 const content::URLDataSource::GotDataCallback& callback) {
77 if (!fallback_icon_service_) { // Can be null for tests.
78 callback.Run(nullptr); // Trigger "Not Found" response.
79 return;
81 std::vector<unsigned char> bitmap_data =
82 fallback_icon_service_->RenderFallbackIconBitmap(
83 url, size_in_pixels, style);
84 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
87 void FallbackIconSource::SendDefaultResponse(
88 const content::URLDataSource::GotDataCallback& callback) {
89 favicon_base::FallbackIconStyle default_style;
90 SendFallbackIconHelper(GURL(), gfx::kFaviconSize, default_style, callback);