summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/fallback_icon_source.cc
blob: 788c28486ad4529fe47b462d4e1d87f1b21b7503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2015 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/fallback_icon_source.h"

#include <vector>

#include "base/memory/ref_counted_memory.h"
#include "chrome/browser/search/instant_io_context.h"
#include "chrome/common/favicon/fallback_icon_url_parser.h"
#include "chrome/common/url_constants.h"
#include "components/favicon/core/fallback_icon_service.h"
#include "components/keyed_service/core/service_access_type.h"
#include "net/url_request/url_request.h"
#include "ui/gfx/favicon_size.h"
#include "url/gurl.h"

FallbackIconSource::FallbackIconSource(
    favicon::FallbackIconService* fallback_icon_service)
    : fallback_icon_service_(fallback_icon_service) {
}

FallbackIconSource::~FallbackIconSource() {
}

std::string FallbackIconSource::GetSource() const {
  return chrome::kChromeUIFallbackIconHost;
}

void FallbackIconSource::StartDataRequest(
    const std::string& path,
    int render_process_id,
    int render_frame_id,
    const content::URLDataSource::GotDataCallback& callback) {
  chrome::ParsedFallbackIconPath parsed;
  bool success = parsed.Parse(path);
  if (!success) {
    SendDefaultResponse(callback);
    return;
  }

  GURL url(parsed.url_string());
  if (url.is_empty() || !url.is_valid()) {
    SendDefaultResponse(callback);
    return;
  }

  SendFallbackIconHelper(
      url, parsed.size_in_pixels(), parsed.style(), callback);
}

std::string FallbackIconSource::GetMimeType(const std::string&) const {
  // We need to explicitly return a mime type, otherwise if the user tries to
  // drag the image they get no extension.
  return "image/png";
}

bool FallbackIconSource::ShouldReplaceExistingSource() const {
  // Leave the existing DataSource in place, otherwise we'll drop any pending
  // requests on the floor.
  return false;
}

bool FallbackIconSource::ShouldServiceRequest(
    const net::URLRequest* request) const {
  if (request->url().SchemeIs(chrome::kChromeSearchScheme))
    return InstantIOContext::ShouldServiceRequest(request);
  return URLDataSource::ShouldServiceRequest(request);
}

void FallbackIconSource::SendFallbackIconHelper(
    const GURL& url,
    int size_in_pixels,
    const favicon_base::FallbackIconStyle& style,
    const content::URLDataSource::GotDataCallback& callback) {
  if (!fallback_icon_service_) {  // Can be null for tests.
    callback.Run(nullptr);  // Trigger "Not Found" response.
    return;
  }
  std::vector<unsigned char> bitmap_data =
      fallback_icon_service_->RenderFallbackIconBitmap(
          url, size_in_pixels, style);
  callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
}

void FallbackIconSource::SendDefaultResponse(
    const content::URLDataSource::GotDataCallback& callback) {
  favicon_base::FallbackIconStyle default_style;
  SendFallbackIconHelper(GURL(), gfx::kFaviconSize, default_style, callback);
}