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/manifest/manifest_icon_downloader.h" | |
6 | |
7 #include <stddef.h> | 5 #include <stddef.h> |
8 | 6 |
9 #include <limits> | 7 #include <limits> |
10 | 8 |
11 #include "chrome/browser/manifest/manifest_icon_selector.h" | |
12 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/manifest_icon_downloader.h" | |
please use gerrit instead
2017/06/12 23:57:22
This line should be on top.
zino
2017/06/13 16:07:44
Done.
| |
11 #include "content/public/browser/manifest_icon_selector.h" | |
13 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
14 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
15 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
16 #include "content/public/common/console_message_level.h" | 15 #include "content/public/common/console_message_level.h" |
17 #include "skia/ext/image_operations.h" | 16 #include "skia/ext/image_operations.h" |
18 | 17 |
18 namespace content { | |
please use gerrit instead
2017/06/12 23:57:22
Newline below.
zino
2017/06/13 16:07:44
Done.
| |
19 // DevToolsConsoleHelper is a class that holds a WebContents in order to be able | 19 // DevToolsConsoleHelper is a class that holds a WebContents in order to be able |
20 // to send a message to the WebContents' main frame. It is used so | 20 // to send a message to the WebContents' main frame. It is used so |
21 // ManifestIconDownloader and the callers do not have to worry about | 21 // ManifestIconDownloader and the callers do not have to worry about |
22 // |web_contents| lifetime. If the |web_contents| is invalidated before the | 22 // |web_contents| lifetime. If the |web_contents| is invalidated before the |
23 // message can be sent, the message will simply be ignored. | 23 // message can be sent, the message will simply be ignored. |
24 class ManifestIconDownloader::DevToolsConsoleHelper | 24 class ManifestIconDownloader::DevToolsConsoleHelper |
25 : public content::WebContentsObserver { | 25 : public content::WebContentsObserver { |
26 public: | 26 public: |
27 explicit DevToolsConsoleHelper(content::WebContents* web_contents); | 27 explicit DevToolsConsoleHelper(content::WebContents* web_contents); |
28 ~DevToolsConsoleHelper() override = default; | 28 ~DevToolsConsoleHelper() override = default; |
29 | 29 |
30 void AddMessage(content::ConsoleMessageLevel level, | 30 void AddMessage(content::ConsoleMessageLevel level, |
31 const std::string& message); | 31 const std::string& message); |
32 }; | 32 }; |
33 | 33 |
34 ManifestIconDownloader::DevToolsConsoleHelper::DevToolsConsoleHelper( | 34 ManifestIconDownloader::DevToolsConsoleHelper::DevToolsConsoleHelper( |
35 content::WebContents* web_contents) | 35 content::WebContents* web_contents) |
36 : WebContentsObserver(web_contents) { | 36 : WebContentsObserver(web_contents) {} |
37 } | |
38 | 37 |
39 void ManifestIconDownloader::DevToolsConsoleHelper::AddMessage( | 38 void ManifestIconDownloader::DevToolsConsoleHelper::AddMessage( |
40 content::ConsoleMessageLevel level, | 39 content::ConsoleMessageLevel level, |
41 const std::string& message) { | 40 const std::string& message) { |
42 if (!web_contents()) | 41 if (!web_contents()) |
43 return; | 42 return; |
44 web_contents()->GetMainFrame()->AddMessageToConsole(level, message); | 43 web_contents()->GetMainFrame()->AddMessageToConsole(level, message); |
45 } | 44 } |
46 | 45 |
47 bool ManifestIconDownloader::Download( | 46 bool ManifestIconDownloader::Download( |
48 content::WebContents* web_contents, | 47 content::WebContents* web_contents, |
49 const GURL& icon_url, | 48 const GURL& icon_url, |
50 int ideal_icon_size_in_px, | 49 int ideal_icon_size_in_px, |
51 int minimum_icon_size_in_px, | 50 int minimum_icon_size_in_px, |
52 const ManifestIconDownloader::IconFetchCallback& callback) { | 51 const ManifestIconDownloader::IconFetchCallback& callback) { |
53 DCHECK(minimum_icon_size_in_px <= ideal_icon_size_in_px); | 52 DCHECK(minimum_icon_size_in_px <= ideal_icon_size_in_px); |
54 if (!web_contents || !icon_url.is_valid()) | 53 if (!web_contents || !icon_url.is_valid()) |
55 return false; | 54 return false; |
56 | 55 |
57 web_contents->DownloadImage( | 56 web_contents->DownloadImage( |
58 icon_url, | 57 icon_url, |
59 false, // is_favicon | 58 false, // is_favicon |
60 0, // max_bitmap_size - 0 means no maximum size. | 59 0, // max_bitmap_size - 0 means no maximum size. |
61 false, // bypass_cache | 60 false, // bypass_cache |
62 base::Bind(&ManifestIconDownloader::OnIconFetched, | 61 base::Bind(&ManifestIconDownloader::OnIconFetched, ideal_icon_size_in_px, |
63 ideal_icon_size_in_px, | |
64 minimum_icon_size_in_px, | 62 minimum_icon_size_in_px, |
65 base::Owned(new DevToolsConsoleHelper(web_contents)), | 63 base::Owned(new DevToolsConsoleHelper(web_contents)), |
66 callback)); | 64 callback)); |
67 return true; | 65 return true; |
68 } | 66 } |
69 | 67 |
70 void ManifestIconDownloader::OnIconFetched( | 68 void ManifestIconDownloader::OnIconFetched( |
71 int ideal_icon_size_in_px, | 69 int ideal_icon_size_in_px, |
72 int minimum_icon_size_in_px, | 70 int minimum_icon_size_in_px, |
73 DevToolsConsoleHelper* console_helper, | 71 DevToolsConsoleHelper* console_helper, |
74 const ManifestIconDownloader::IconFetchCallback& callback, | 72 const ManifestIconDownloader::IconFetchCallback& callback, |
75 int id, | 73 int id, |
76 int http_status_code, | 74 int http_status_code, |
77 const GURL& url, | 75 const GURL& url, |
78 const std::vector<SkBitmap>& bitmaps, | 76 const std::vector<SkBitmap>& bitmaps, |
79 const std::vector<gfx::Size>& sizes) { | 77 const std::vector<gfx::Size>& sizes) { |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
81 | 79 |
82 if (bitmaps.empty()) { | 80 if (bitmaps.empty()) { |
83 console_helper->AddMessage( | 81 console_helper->AddMessage( |
84 content::CONSOLE_MESSAGE_LEVEL_ERROR, | 82 content::CONSOLE_MESSAGE_LEVEL_ERROR, |
85 "Error while trying to use the following icon from the Manifest: " | 83 "Error while trying to use the following icon from the Manifest: " + |
86 + url.spec() + " (Download error or resource isn't a valid image)"); | 84 url.spec() + " (Download error or resource isn't a valid image)"); |
87 | 85 |
88 callback.Run(SkBitmap()); | 86 callback.Run(SkBitmap()); |
89 return; | 87 return; |
90 } | 88 } |
91 | 89 |
92 const int closest_index = FindClosestBitmapIndex( | 90 const int closest_index = FindClosestBitmapIndex( |
93 ideal_icon_size_in_px, minimum_icon_size_in_px, bitmaps); | 91 ideal_icon_size_in_px, minimum_icon_size_in_px, bitmaps); |
94 | 92 |
95 if (closest_index == -1) { | 93 if (closest_index == -1) { |
96 console_helper->AddMessage( | 94 console_helper->AddMessage( |
97 content::CONSOLE_MESSAGE_LEVEL_ERROR, | 95 content::CONSOLE_MESSAGE_LEVEL_ERROR, |
98 "Error while trying to use the following icon from the Manifest: " | 96 "Error while trying to use the following icon from the Manifest: " + |
99 + url.spec() | 97 url.spec() + |
100 + " (Resource size is not correct - typo in the Manifest?)"); | 98 " (Resource size is not correct - typo in the Manifest?)"); |
101 | 99 |
102 callback.Run(SkBitmap()); | 100 callback.Run(SkBitmap()); |
103 return; | 101 return; |
104 } | 102 } |
105 | 103 |
106 const SkBitmap& chosen = bitmaps[closest_index]; | 104 const SkBitmap& chosen = bitmaps[closest_index]; |
107 | 105 |
108 // Only scale if we need to scale down. For scaling up we will let the system | 106 // Only scale if we need to scale down. For scaling up we will let the system |
109 // handle that when it is required to display it. This saves space in the | 107 // handle that when it is required to display it. This saves space in the |
110 // webapp storage system as well. | 108 // webapp storage system as well. |
111 if (chosen.height() > ideal_icon_size_in_px || | 109 if (chosen.height() > ideal_icon_size_in_px || |
112 chosen.width() > ideal_icon_size_in_px) { | 110 chosen.width() > ideal_icon_size_in_px) { |
113 content::BrowserThread::PostTask( | 111 content::BrowserThread::PostTask( |
114 content::BrowserThread::IO, FROM_HERE, | 112 content::BrowserThread::IO, FROM_HERE, |
115 base::BindOnce(&ManifestIconDownloader::ScaleIcon, | 113 base::BindOnce(&ManifestIconDownloader::ScaleIcon, |
116 ideal_icon_size_in_px, chosen, callback)); | 114 ideal_icon_size_in_px, chosen, callback)); |
117 return; | 115 return; |
118 } | 116 } |
119 | 117 |
120 callback.Run(chosen); | 118 callback.Run(chosen); |
121 } | 119 } |
122 | 120 |
123 void ManifestIconDownloader::ScaleIcon( | 121 void ManifestIconDownloader::ScaleIcon( |
124 int ideal_icon_size_in_px, | 122 int ideal_icon_size_in_px, |
125 const SkBitmap& bitmap, | 123 const SkBitmap& bitmap, |
126 const ManifestIconDownloader::IconFetchCallback& callback) { | 124 const ManifestIconDownloader::IconFetchCallback& callback) { |
127 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 125 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
128 | 126 |
129 const SkBitmap& scaled = skia::ImageOperations::Resize( | 127 const SkBitmap& scaled = skia::ImageOperations::Resize( |
130 bitmap, | 128 bitmap, skia::ImageOperations::RESIZE_BEST, ideal_icon_size_in_px, |
131 skia::ImageOperations::RESIZE_BEST, | |
132 ideal_icon_size_in_px, | |
133 ideal_icon_size_in_px); | 129 ideal_icon_size_in_px); |
134 | 130 |
135 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 131 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
136 base::BindOnce(callback, scaled)); | 132 base::BindOnce(callback, scaled)); |
137 } | 133 } |
138 | 134 |
139 int ManifestIconDownloader::FindClosestBitmapIndex( | 135 int ManifestIconDownloader::FindClosestBitmapIndex( |
140 int ideal_icon_size_in_px, | 136 int ideal_icon_size_in_px, |
141 int minimum_icon_size_in_px, | 137 int minimum_icon_size_in_px, |
142 const std::vector<SkBitmap>& bitmaps) { | 138 const std::vector<SkBitmap>& bitmaps) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 float width = static_cast<float>(bitmaps[i].width()); | 175 float width = static_cast<float>(bitmaps[i].width()); |
180 float ratio = height / width; | 176 float ratio = height / width; |
181 float ratio_difference = fabs(ratio - 1); | 177 float ratio_difference = fabs(ratio - 1); |
182 if (ratio_difference < best_ratio_difference) { | 178 if (ratio_difference < best_ratio_difference) { |
183 best_index = i; | 179 best_index = i; |
184 best_ratio_difference = ratio_difference; | 180 best_ratio_difference = ratio_difference; |
185 } | 181 } |
186 } | 182 } |
187 | 183 |
188 return best_index; | 184 return best_index; |
189 } | 185 } |
please use gerrit instead
2017/06/12 23:57:22
newline below
zino
2017/06/13 16:07:44
Done.
| |
186 } | |
please use gerrit instead
2017/06/12 23:57:22
// namespace content
zino
2017/06/13 16:07:44
Done.
| |
OLD | NEW |