Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Side by Side Diff: content/browser/manifest/manifest_icon_downloader.cc

Issue 2933743002: Move chrome/browser/manifest to content/browser. (Closed)
Patch Set: rebased Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 WebContentsObserver {
26 public: 28 public:
27 explicit DevToolsConsoleHelper(content::WebContents* web_contents); 29 explicit DevToolsConsoleHelper(WebContents* web_contents);
28 ~DevToolsConsoleHelper() override = default; 30 ~DevToolsConsoleHelper() override = default;
29 31
30 void AddMessage(content::ConsoleMessageLevel level, 32 void AddMessage(ConsoleMessageLevel level, const std::string& message);
31 const std::string& message);
32 }; 33 };
33 34
34 ManifestIconDownloader::DevToolsConsoleHelper::DevToolsConsoleHelper( 35 ManifestIconDownloader::DevToolsConsoleHelper::DevToolsConsoleHelper(
35 content::WebContents* web_contents) 36 WebContents* web_contents)
36 : WebContentsObserver(web_contents) { 37 : WebContentsObserver(web_contents) {}
37 }
38 38
39 void ManifestIconDownloader::DevToolsConsoleHelper::AddMessage( 39 void ManifestIconDownloader::DevToolsConsoleHelper::AddMessage(
40 content::ConsoleMessageLevel level, 40 ConsoleMessageLevel level,
41 const std::string& message) { 41 const std::string& message) {
42 if (!web_contents()) 42 if (!web_contents())
43 return; 43 return;
44 web_contents()->GetMainFrame()->AddMessageToConsole(level, message); 44 web_contents()->GetMainFrame()->AddMessageToConsole(level, message);
45 } 45 }
46 46
47 bool ManifestIconDownloader::Download( 47 bool ManifestIconDownloader::Download(
48 content::WebContents* web_contents, 48 WebContents* web_contents,
49 const GURL& icon_url, 49 const GURL& icon_url,
50 int ideal_icon_size_in_px, 50 int ideal_icon_size_in_px,
51 int minimum_icon_size_in_px, 51 int minimum_icon_size_in_px,
52 const ManifestIconDownloader::IconFetchCallback& callback) { 52 const ManifestIconDownloader::IconFetchCallback& callback) {
53 DCHECK(minimum_icon_size_in_px <= ideal_icon_size_in_px); 53 DCHECK(minimum_icon_size_in_px <= ideal_icon_size_in_px);
54 if (!web_contents || !icon_url.is_valid()) 54 if (!web_contents || !icon_url.is_valid())
55 return false; 55 return false;
56 56
57 web_contents->DownloadImage( 57 web_contents->DownloadImage(
58 icon_url, 58 icon_url,
59 false, // is_favicon 59 false, // is_favicon
60 0, // max_bitmap_size - 0 means no maximum size. 60 0, // max_bitmap_size - 0 means no maximum size.
61 false, // bypass_cache 61 false, // bypass_cache
62 base::Bind(&ManifestIconDownloader::OnIconFetched, 62 base::Bind(&ManifestIconDownloader::OnIconFetched, ideal_icon_size_in_px,
63 ideal_icon_size_in_px,
64 minimum_icon_size_in_px, 63 minimum_icon_size_in_px,
65 base::Owned(new DevToolsConsoleHelper(web_contents)), 64 base::Owned(new DevToolsConsoleHelper(web_contents)),
66 callback)); 65 callback));
67 return true; 66 return true;
68 } 67 }
69 68
70 void ManifestIconDownloader::OnIconFetched( 69 void ManifestIconDownloader::OnIconFetched(
71 int ideal_icon_size_in_px, 70 int ideal_icon_size_in_px,
72 int minimum_icon_size_in_px, 71 int minimum_icon_size_in_px,
73 DevToolsConsoleHelper* console_helper, 72 DevToolsConsoleHelper* console_helper,
74 const ManifestIconDownloader::IconFetchCallback& callback, 73 const ManifestIconDownloader::IconFetchCallback& callback,
75 int id, 74 int id,
76 int http_status_code, 75 int http_status_code,
77 const GURL& url, 76 const GURL& url,
78 const std::vector<SkBitmap>& bitmaps, 77 const std::vector<SkBitmap>& bitmaps,
79 const std::vector<gfx::Size>& sizes) { 78 const std::vector<gfx::Size>& sizes) {
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 79 DCHECK_CURRENTLY_ON(BrowserThread::UI);
81 80
82 if (bitmaps.empty()) { 81 if (bitmaps.empty()) {
83 console_helper->AddMessage( 82 console_helper->AddMessage(
84 content::CONSOLE_MESSAGE_LEVEL_ERROR, 83 CONSOLE_MESSAGE_LEVEL_ERROR,
85 "Error while trying to use the following icon from the Manifest: " 84 "Error while trying to use the following icon from the Manifest: " +
86 + url.spec() + " (Download error or resource isn't a valid image)"); 85 url.spec() + " (Download error or resource isn't a valid image)");
87 86
88 callback.Run(SkBitmap()); 87 callback.Run(SkBitmap());
89 return; 88 return;
90 } 89 }
91 90
92 const int closest_index = FindClosestBitmapIndex( 91 const int closest_index = FindClosestBitmapIndex(
93 ideal_icon_size_in_px, minimum_icon_size_in_px, bitmaps); 92 ideal_icon_size_in_px, minimum_icon_size_in_px, bitmaps);
94 93
95 if (closest_index == -1) { 94 if (closest_index == -1) {
96 console_helper->AddMessage( 95 console_helper->AddMessage(
97 content::CONSOLE_MESSAGE_LEVEL_ERROR, 96 CONSOLE_MESSAGE_LEVEL_ERROR,
98 "Error while trying to use the following icon from the Manifest: " 97 "Error while trying to use the following icon from the Manifest: " +
99 + url.spec() 98 url.spec() +
100 + " (Resource size is not correct - typo in the Manifest?)"); 99 " (Resource size is not correct - typo in the Manifest?)");
101 100
102 callback.Run(SkBitmap()); 101 callback.Run(SkBitmap());
103 return; 102 return;
104 } 103 }
105 104
106 const SkBitmap& chosen = bitmaps[closest_index]; 105 const SkBitmap& chosen = bitmaps[closest_index];
107 106
108 // Only scale if we need to scale down. For scaling up we will let the system 107 // 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 108 // handle that when it is required to display it. This saves space in the
110 // webapp storage system as well. 109 // webapp storage system as well.
111 if (chosen.height() > ideal_icon_size_in_px || 110 if (chosen.height() > ideal_icon_size_in_px ||
112 chosen.width() > ideal_icon_size_in_px) { 111 chosen.width() > ideal_icon_size_in_px) {
113 content::BrowserThread::PostTask( 112 BrowserThread::PostTask(
114 content::BrowserThread::IO, FROM_HERE, 113 BrowserThread::IO, FROM_HERE,
115 base::BindOnce(&ManifestIconDownloader::ScaleIcon, 114 base::BindOnce(&ManifestIconDownloader::ScaleIcon,
116 ideal_icon_size_in_px, chosen, callback)); 115 ideal_icon_size_in_px, chosen, callback));
117 return; 116 return;
118 } 117 }
119 118
120 callback.Run(chosen); 119 callback.Run(chosen);
121 } 120 }
122 121
123 void ManifestIconDownloader::ScaleIcon( 122 void ManifestIconDownloader::ScaleIcon(
124 int ideal_icon_size_in_px, 123 int ideal_icon_size_in_px,
125 const SkBitmap& bitmap, 124 const SkBitmap& bitmap,
126 const ManifestIconDownloader::IconFetchCallback& callback) { 125 const ManifestIconDownloader::IconFetchCallback& callback) {
127 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 126 DCHECK_CURRENTLY_ON(BrowserThread::IO);
128 127
129 const SkBitmap& scaled = skia::ImageOperations::Resize( 128 const SkBitmap& scaled = skia::ImageOperations::Resize(
130 bitmap, 129 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); 130 ideal_icon_size_in_px);
134 131
135 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 132 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
136 base::BindOnce(callback, scaled)); 133 base::BindOnce(callback, scaled));
137 } 134 }
138 135
139 int ManifestIconDownloader::FindClosestBitmapIndex( 136 int ManifestIconDownloader::FindClosestBitmapIndex(
140 int ideal_icon_size_in_px, 137 int ideal_icon_size_in_px,
141 int minimum_icon_size_in_px, 138 int minimum_icon_size_in_px,
142 const std::vector<SkBitmap>& bitmaps) { 139 const std::vector<SkBitmap>& bitmaps) {
143 int best_index = -1; 140 int best_index = -1;
144 int best_delta = std::numeric_limits<int>::min(); 141 int best_delta = std::numeric_limits<int>::min();
145 const int max_negative_delta = 142 const int max_negative_delta =
146 minimum_icon_size_in_px - ideal_icon_size_in_px; 143 minimum_icon_size_in_px - ideal_icon_size_in_px;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 float ratio = height / width; 177 float ratio = height / width;
181 float ratio_difference = fabs(ratio - 1); 178 float ratio_difference = fabs(ratio - 1);
182 if (ratio_difference < best_ratio_difference) { 179 if (ratio_difference < best_ratio_difference) {
183 best_index = i; 180 best_index = i;
184 best_ratio_difference = ratio_difference; 181 best_ratio_difference = ratio_difference;
185 } 182 }
186 } 183 }
187 184
188 return best_index; 185 return best_index;
189 } 186 }
187
188 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/manifest/manifest_browsertest.cc ('k') | content/browser/manifest/manifest_icon_downloader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698