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