Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #include "chrome/browser/media/tab_desktop_media_list.h" | |
| 6 | |
| 7 #include "base/hash.h" | |
| 8 #include "chrome/browser/profiles/profile_manager.h" | |
| 9 #include "chrome/browser/ui/browser.h" | |
| 10 #include "chrome/browser/ui/browser_iterator.h" | |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 12 #include "chrome/grit/generated_resources.h" | |
| 13 #include "components/favicon/content/content_favicon_driver.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/render_frame_host.h" | |
| 16 #include "content/public/browser/render_process_host.h" | |
| 17 #include "third_party/skia/include/core/SkCanvas.h" | |
| 18 #include "third_party/skia/include/core/SkImage.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | |
| 20 | |
| 21 using content::DesktopMediaID; | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Returns a hash of a favicon to detect when the favicon of media source has | |
| 26 // changed. | |
| 27 uint32_t GetImageHash(const gfx::Image& favicon) { | |
| 28 SkBitmap bitmap = favicon.AsBitmap(); | |
| 29 bitmap.lockPixels(); | |
| 30 uint32_t value = base::SuperFastHash( | |
|
Sergey Ulanov
2016/01/26 18:06:54
Use base::Hash() instead of SuperFastHash().
Super
GeorgeZ
2016/01/27 00:17:38
Done.
| |
| 31 reinterpret_cast<char*>(bitmap.getPixels()), bitmap.getSize()); | |
| 32 bitmap.unlockPixels(); | |
| 33 | |
| 34 return value; | |
| 35 } | |
| 36 | |
| 37 gfx::ImageSkia CreateEnlargedFaviconImage(gfx::Size size, gfx::Image& favicon) { | |
| 38 if (size.width() < 20 || size.height() < 20) | |
| 39 return gfx::ImageSkia(); | |
|
Sergey Ulanov
2016/01/26 18:06:54
Why return an empty image here? Can this be a DCHE
GeorgeZ
2016/01/27 00:17:38
Done.
| |
| 40 | |
| 41 // Create a bitmap. | |
| 42 SkBitmap result; | |
| 43 result.allocN32Pixels(size.width(), size.height(), true); | |
| 44 SkCanvas canvas(result); | |
| 45 | |
| 46 // Fill with white. | |
| 47 canvas.drawARGB(255, 255, 255, 255); | |
|
Sergey Ulanov
2016/01/26 18:06:54
Why is it white instead of transparent? This may n
GeorgeZ
2016/01/27 00:17:38
I modified as required. Please refer to https://do
| |
| 48 | |
| 49 // Draw black border. | |
| 50 const int thickness = result.width() / 30; | |
| 51 SkPaint paint; | |
| 52 paint.setARGB(255, 0, 0, 0); | |
| 53 paint.setStyle(SkPaint::kStroke_Style); | |
| 54 paint.setStrokeWidth(thickness); | |
| 55 canvas.drawRectCoords(thickness, // left | |
| 56 thickness, // top | |
| 57 result.width() - thickness, // right | |
| 58 result.height() - thickness, // bottom | |
| 59 paint); | |
| 60 | |
| 61 // Draw a scaled favicon image into the center of result image to take up to | |
| 62 // 3/4 of result image. | |
| 63 const double scale = fmin(3.0 / 4 * result.width() / favicon.Width(), | |
|
Sergey Ulanov
2016/01/26 18:06:54
std::min() instead of fmin()
GeorgeZ
2016/01/27 00:17:38
Done.
| |
| 64 3.0 / 4 * result.height() / favicon.Height()); | |
| 65 SkRect dest_rect; | |
| 66 dest_rect.set(result.width() / 2 - favicon.Width() * scale / 2, // left | |
|
Sergey Ulanov
2016/01/26 18:06:54
Combine this with the previous line:
SkRect dest_
GeorgeZ
2016/01/27 00:17:39
Done.
| |
| 67 result.height() / 2 - favicon.Height() * scale / 2, // top | |
| 68 result.width() / 2 + favicon.Width() * scale / 2, // right | |
| 69 result.height() / 2 + favicon.Height() * scale / 2); // bottom | |
| 70 const scoped_ptr<SkImage> temp_image( | |
|
Sergey Ulanov
2016/01/26 18:06:54
call it 'bitmap'? "temp_" doesn't make sense.
GeorgeZ
2016/01/27 00:17:38
Done.
| |
| 71 SkImage::NewFromBitmap(favicon.AsBitmap())); | |
| 72 canvas.drawImageRect(temp_image.get(), dest_rect, nullptr); | |
| 73 | |
| 74 // Create a skia image. | |
| 75 return gfx::ImageSkia::CreateFrom1xBitmap(result); | |
| 76 } | |
| 77 | |
| 78 // Update the list once per second. | |
| 79 const int kDefaultUpdatePeriod = 1000; | |
| 80 | |
| 81 } // namespace | |
| 82 | |
| 83 TabDesktopMediaList::TabDesktopMediaList() | |
| 84 : DesktopMediaListBase( | |
| 85 base::TimeDelta::FromMilliseconds(kDefaultUpdatePeriod)) {} | |
| 86 | |
| 87 TabDesktopMediaList::~TabDesktopMediaList() {} | |
| 88 | |
| 89 void TabDesktopMediaList::Refresh() { | |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 91 | |
| 92 std::map<DesktopMediaID, gfx::Image> favicon_map; | |
| 93 ImageHashesMap new_favicon_hashes; | |
| 94 std::vector<SourceDescription> sources; | |
| 95 | |
| 96 if (!fake_sources_.size()) { | |
| 97 Profile* profile = ProfileManager::GetLastUsedProfileAllowedByPolicy(); | |
| 98 std::vector<Browser*> browsers; | |
| 99 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | |
| 100 if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile()) | |
| 101 browsers.push_back(*it); | |
| 102 } | |
| 103 | |
| 104 std::map<base::TimeTicks, SourceDescription> tab_map; | |
| 105 | |
| 106 // Enumerate all tabs with their titles and favicons for a user profile. | |
| 107 for (auto browser : browsers) { | |
| 108 const TabStripModel* tab_strip_model = browser->tab_strip_model(); | |
| 109 DCHECK(tab_strip_model); | |
| 110 | |
| 111 for (int i = 0; i < tab_strip_model->count(); i++) { | |
| 112 // Create id for tab. | |
| 113 content::WebContents* contents = tab_strip_model->GetWebContentsAt(i); | |
| 114 DCHECK(contents); | |
| 115 content::RenderFrameHost* main_frame = contents->GetMainFrame(); | |
| 116 DCHECK(main_frame); | |
| 117 DesktopMediaID media_id( | |
| 118 DesktopMediaID::TYPE_WEB_CONTENTS, DesktopMediaID::kNullId, | |
| 119 content::WebContentsMediaCaptureId( | |
| 120 main_frame->GetProcess()->GetID(), main_frame->GetRoutingID())); | |
| 121 | |
| 122 // Create display tab title. | |
| 123 const base::string16 title = l10n_util::GetStringFUTF16( | |
| 124 IDS_DESKTOP_MEDIA_PICKER_CHROME_TAB_TITLE, contents->GetTitle()); | |
| 125 | |
| 126 // Get tab's last active time stamp. | |
| 127 const base::TimeTicks t = contents->GetLastActiveTime(); | |
| 128 tab_map.insert(std::make_pair(t, SourceDescription(media_id, title))); | |
|
qiangchen
2016/01/26 01:01:50
simpler to use [] operator?
GeorgeZ
2016/01/27 00:17:38
struct SourceDescription does have default constru
| |
| 129 | |
| 130 // Get favicon for tab. | |
| 131 favicon::FaviconDriver* favicon_driver = | |
| 132 favicon::ContentFaviconDriver::FromWebContents(contents); | |
| 133 if (favicon_driver) { | |
| 134 gfx::Image favicon = favicon_driver->GetFavicon(); | |
| 135 // Only new or changed favicon need update. | |
| 136 new_favicon_hashes[media_id] = GetImageHash(favicon); | |
| 137 if (!favicon_hashes_.count(media_id) || | |
| 138 (favicon_hashes_[media_id] != new_favicon_hashes[media_id])) { | |
| 139 favicon_map.insert( | |
|
qiangchen
2016/01/26 01:01:50
simpler to use []?
favicon_map[media_id] = favicon
GeorgeZ
2016/01/27 00:17:38
Done.
| |
| 140 std::pair<DesktopMediaID, gfx::Image>(media_id, favicon)); | |
| 141 } | |
| 142 } | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 // Sort tab sources by time. Most recent one first. Then update sources | |
| 147 // list. | |
| 148 for (auto it = tab_map.rbegin(); it != tab_map.rend(); ++it) { | |
| 149 SourceDescription& source = it->second; | |
| 150 sources.push_back(source); | |
| 151 } | |
| 152 } else { | |
| 153 // For unit tests. | |
|
Sergey Ulanov
2016/01/26 18:06:54
This doesn't look like the right way to unittest t
GeorgeZ
2016/01/27 00:17:38
Since tab related code will be removed from this C
| |
| 154 for (size_t i = 0; i < fake_sources_.size(); i++) { | |
| 155 // Only new or changed favicon need update. | |
| 156 DesktopMediaID media_id = fake_sources_[i].id; | |
| 157 new_favicon_hashes[media_id] = GetImageHash(fake_favicons_[i]); | |
| 158 if (!favicon_hashes_.count(media_id) || | |
| 159 (favicon_hashes_[media_id] != new_favicon_hashes[media_id])) { | |
| 160 favicon_map.insert( | |
| 161 std::pair<DesktopMediaID, gfx::Image>(media_id, fake_favicons_[i])); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 sources = fake_sources_; | |
| 166 } | |
| 167 favicon_hashes_ = new_favicon_hashes; | |
| 168 | |
| 169 UpdateSourcesList(sources); | |
| 170 | |
| 171 for (auto it : favicon_map) | |
| 172 UpdateSourceThumbnail( | |
| 173 it.first, CreateEnlargedFaviconImage(thumbnail_size_, it.second)); | |
| 174 | |
| 175 ScheduleNextRefresh(); | |
| 176 } | |
| 177 | |
| 178 void TabDesktopMediaList::SetFakeSourcesForTesting( | |
| 179 std::vector<SourceDescription>& sources, | |
| 180 std::vector<gfx::Image>& favicons) { | |
| 181 fake_sources_ = sources; | |
| 182 fake_favicons_ = favicons; | |
| 183 } | |
| OLD | NEW |