OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/fallback_icon_source.h" | 5 #include "chrome/browser/ui/webui/fallback_icon_source.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
11 #include "chrome/browser/favicon/chrome_fallback_icon_client_factory.h" | |
11 #include "chrome/browser/search/instant_io_context.h" | 12 #include "chrome/browser/search/instant_io_context.h" |
12 #include "chrome/common/favicon/fallback_icon_url_parser.h" | 13 #include "chrome/common/favicon/fallback_icon_url_parser.h" |
13 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
14 #include "grit/platform_locale_settings.h" | 15 #include "components/favicon/core/browser/fallback_icon_client.h" |
16 #include "components/favicon_base/fallback_icon_service.h" | |
17 #include "components/keyed_service/core/service_access_type.h" | |
15 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
16 #include "ui/base/l10n/l10n_util.h" | |
17 #include "ui/gfx/favicon_size.h" | 19 #include "ui/gfx/favicon_size.h" |
18 | 20 |
19 FallbackIconSource::FallbackIconSource() { | 21 FallbackIconSource::FallbackIconSource(FallbackIconClient* fallback_icon_client) |
20 std::vector<std::string> font_list; | 22 : fallback_icon_client_(fallback_icon_client) { |
21 #if defined(OS_CHROMEOS) | |
22 font_list.push_back("Noto Sans"); | |
23 #elif defined(OS_IOS) | |
24 font_list.push_back("Helvetica Neue"); | |
25 #else | |
26 font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY)); | |
27 #endif | |
28 fallback_icon_service_.reset( | |
29 new favicon_base::FallbackIconService(font_list)); | |
30 } | 23 } |
31 | 24 |
32 FallbackIconSource::~FallbackIconSource() { | 25 FallbackIconSource::~FallbackIconSource() { |
33 } | 26 } |
34 | 27 |
35 std::string FallbackIconSource::GetSource() const { | 28 std::string FallbackIconSource::GetSource() const { |
36 return chrome::kChromeUIFallbackIconHost; | 29 return chrome::kChromeUIFallbackIconHost; |
37 } | 30 } |
38 | 31 |
39 void FallbackIconSource::StartDataRequest( | 32 void FallbackIconSource::StartDataRequest( |
40 const std::string& path, | 33 const std::string& path, |
41 int render_process_id, | 34 int render_process_id, |
42 int render_frame_id, | 35 int render_frame_id, |
43 const content::URLDataSource::GotDataCallback& callback) { | 36 const content::URLDataSource::GotDataCallback& callback) { |
44 chrome::ParsedFallbackIconPath parsed; | 37 chrome::ParsedFallbackIconPath parsed; |
45 bool success = parsed.Parse(path); | 38 bool success = parsed.Parse(path); |
46 if (!success) { | 39 if (!success) { |
47 SendDefaultResponse(callback); | 40 SendDefaultResponse(callback); |
48 return; | 41 return; |
49 } | 42 } |
50 | 43 |
51 GURL url(parsed.url()); | 44 SendFallbackIconHelper( |
52 std::vector<unsigned char> bitmap_data = | 45 parsed.url(), parsed.size_in_pixels(), parsed.style(), callback); |
53 fallback_icon_service_->RenderFallbackIconBitmap( | |
54 url, parsed.size_in_pixels(), parsed.style()); | |
55 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
56 } | 46 } |
57 | 47 |
58 std::string FallbackIconSource::GetMimeType(const std::string&) const { | 48 std::string FallbackIconSource::GetMimeType(const std::string&) const { |
59 // We need to explicitly return a mime type, otherwise if the user tries to | 49 // We need to explicitly return a mime type, otherwise if the user tries to |
60 // drag the image they get no extension. | 50 // drag the image they get no extension. |
61 return "image/png"; | 51 return "image/png"; |
62 } | 52 } |
63 | 53 |
64 bool FallbackIconSource::ShouldReplaceExistingSource() const { | 54 bool FallbackIconSource::ShouldReplaceExistingSource() const { |
65 // Leave the existing DataSource in place, otherwise we'll drop any pending | 55 // Leave the existing DataSource in place, otherwise we'll drop any pending |
66 // requests on the floor. | 56 // requests on the floor. |
67 return false; | 57 return false; |
68 } | 58 } |
69 | 59 |
70 bool FallbackIconSource::ShouldServiceRequest( | 60 bool FallbackIconSource::ShouldServiceRequest( |
71 const net::URLRequest* request) const { | 61 const net::URLRequest* request) const { |
72 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | 62 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) |
73 return InstantIOContext::ShouldServiceRequest(request); | 63 return InstantIOContext::ShouldServiceRequest(request); |
74 return URLDataSource::ShouldServiceRequest(request); | 64 return URLDataSource::ShouldServiceRequest(request); |
75 } | 65 } |
76 | 66 |
67 void FallbackIconSource::SendFallbackIconHelper( | |
68 const GURL& url, | |
69 int size_in_pixels, | |
70 const favicon_base::FallbackIconStyle& style, | |
71 const content::URLDataSource::GotDataCallback& callback) { | |
72 favicon_base::FallbackIconService* fallback_icon_service = | |
sdefresne
2015/03/16 09:11:29
Why receive the client to only use it to access th
huangs
2015/03/16 22:06:11
Done.
| |
73 fallback_icon_client_->GetFallbackIconService(); | |
74 if (!fallback_icon_service) { | |
75 callback.Run(nullptr); | |
76 return; | |
77 } | |
78 std::vector<unsigned char> bitmap_data = | |
79 fallback_icon_service->RenderFallbackIconBitmap( | |
80 url, size_in_pixels, style); | |
81 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
82 } | |
83 | |
77 void FallbackIconSource::SendDefaultResponse( | 84 void FallbackIconSource::SendDefaultResponse( |
78 const content::URLDataSource::GotDataCallback& callback) { | 85 const content::URLDataSource::GotDataCallback& callback) { |
79 std::vector<unsigned char> bitmap_data = | 86 favicon_base::FallbackIconStyle default_style; |
80 fallback_icon_service_->RenderFallbackIconBitmap( | 87 SendFallbackIconHelper(GURL(), gfx::kFaviconSize, default_style, callback); |
81 GURL(), gfx::kFaviconSize, favicon_base::FallbackIconStyle()); | |
82 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | |
83 } | 88 } |
OLD | NEW |