Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / webui / favicon_source.h
blob2c2285ebf7f294fd51cd360a2cc5f0b8a6cf7f99
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_UI_WEBUI_FAVICON_SOURCE_H_
6 #define CHROME_BROWSER_UI_WEBUI_FAVICON_SOURCE_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/task/cancelable_task_tracker.h"
14 #include "components/favicon/core/favicon_service.h"
15 #include "content/public/browser/url_data_source.h"
16 #include "ui/gfx/favicon_size.h"
18 class Profile;
20 // FaviconSource is the gateway between network-level chrome:
21 // requests for favicons and the history backend that serves these.
23 // Format:
24 // chrome://favicon/size&scalefactor/urlmodifier/url
25 // Some parameters are optional as described below. However, the order of the
26 // parameters is not interchangeable.
28 // Parameter:
29 // 'url' Required
30 // Specifies the page URL of the requested favicon. If the 'urlmodifier'
31 // parameter is 'iconurl', the URL refers to the URL of the favicon image
32 // instead.
33 // 'size&scalefactor' Optional
34 // Values: ['largest', size/aa@bx/]
35 // 'largest': Specifies that the largest available favicon is requested.
36 // Example: chrome://favicon/largest/http://www.google.com/
37 // 'size/aa@bx/':
38 // Specifies the requested favicon's size in DIP (aa) and the requested
39 // favicon's scale factor. (b).
40 // The supported requested DIP sizes are: 16x16, 32x32 and 64x64.
41 // If the parameter is unspecified, the requested favicon's size defaults
42 // to 16 and the requested scale factor defaults to 1x.
43 // Example: chrome://favicon/size/16@2x/http://www.google.com/
44 // 'urlmodifier' Optional
45 // Values: ['iconurl', 'origin']
46 // 'iconurl': Specifies that the url parameter refers to the URL of
47 // the favicon image as opposed to the URL of the page that the favicon is
48 // on.
49 // Example: chrome://favicon/iconurl/http://www.google.com/favicon.ico
50 // 'origin': Specifies that the URL should be converted to a form with
51 // an empty path and a valid scheme. The converted URL will be used to
52 // request the favicon from the favicon service.
53 // Examples:
54 // chrome://favicon/origin/http://example.com/a
55 // chrome://favicon/origin/example.com
56 // Both URLs request the favicon for http://example.com from the
57 // favicon service.
58 class FaviconSource : public content::URLDataSource {
59 public:
60 // Defines the type of icon the FaviconSource will provide.
61 enum IconType {
62 FAVICON,
63 // Any available icon in the priority of TOUCH_ICON_PRECOMPOSED, TOUCH_ICON,
64 // FAVICON, and default favicon.
65 ANY
68 // |type| is the type of icon this FaviconSource will provide.
69 FaviconSource(Profile* profile, IconType type);
71 ~FaviconSource() override;
73 // content::URLDataSource implementation.
74 std::string GetSource() const override;
75 void StartDataRequest(
76 const std::string& path,
77 int render_process_id,
78 int render_frame_id,
79 const content::URLDataSource::GotDataCallback& callback) override;
80 std::string GetMimeType(const std::string&) const override;
81 bool ShouldReplaceExistingSource() const override;
82 bool ShouldServiceRequest(const net::URLRequest* request) const override;
84 protected:
85 struct IconRequest {
86 IconRequest();
87 IconRequest(const content::URLDataSource::GotDataCallback& cb,
88 const GURL& path,
89 int size,
90 float scale);
91 ~IconRequest();
93 content::URLDataSource::GotDataCallback callback;
94 GURL request_path;
95 int size_in_dip;
96 float device_scale_factor;
99 // Called when the favicon data is missing to perform additional checks to
100 // locate the resource.
101 // |request| contains information for the failed request.
102 // Returns true if the missing resource is found.
103 virtual bool HandleMissingResource(const IconRequest& request);
105 Profile* profile_;
107 private:
108 FRIEND_TEST_ALL_PREFIXES(FaviconSourceTest, InstantParsing);
109 FRIEND_TEST_ALL_PREFIXES(FaviconSourceTest, Parsing);
111 // Defines the allowed pixel sizes for requested favicons.
112 enum IconSize {
113 SIZE_16,
114 SIZE_32,
115 SIZE_64,
116 NUM_SIZES
119 // Called when favicon data is available from the history backend.
120 void OnFaviconDataAvailable(
121 const IconRequest& request,
122 const favicon_base::FaviconRawBitmapResult& bitmap_result);
124 // Sends the 16x16 DIP 1x default favicon.
125 void SendDefaultResponse(
126 const content::URLDataSource::GotDataCallback& callback);
128 // Sends the default favicon.
129 void SendDefaultResponse(const IconRequest& request);
131 base::CancelableTaskTracker cancelable_task_tracker_;
133 // Raw PNG representations of favicons of each size to show when the favicon
134 // database doesn't have a favicon for a webpage. Indexed by IconSize values.
135 scoped_refptr<base::RefCountedMemory> default_favicons_[NUM_SIZES];
137 // The favicon_base::IconTypes of icon that this FaviconSource handles.
138 int icon_types_;
140 DISALLOW_COPY_AND_ASSIGN(FaviconSource);
143 #endif // CHROME_BROWSER_UI_WEBUI_FAVICON_SOURCE_H_