Index: chrome/browser/media/tab_desktop_media_list.cc |
diff --git a/chrome/browser/media/tab_desktop_media_list.cc b/chrome/browser/media/tab_desktop_media_list.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4c2c6914b11b34965a819c3f8ff0e1ad9924e823 |
--- /dev/null |
+++ b/chrome/browser/media/tab_desktop_media_list.cc |
@@ -0,0 +1,183 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/media/tab_desktop_media_list.h" |
+ |
+#include "base/hash.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_iterator.h" |
+#include "chrome/browser/ui/tabs/tab_strip_model.h" |
+#include "chrome/grit/generated_resources.h" |
+#include "components/favicon/content/content_favicon_driver.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "third_party/skia/include/core/SkCanvas.h" |
+#include "third_party/skia/include/core/SkImage.h" |
+#include "ui/base/l10n/l10n_util.h" |
+ |
+using content::DesktopMediaID; |
+ |
+namespace { |
+ |
+// Returns a hash of a favicon to detect when the favicon of media source has |
+// changed. |
+uint32_t GetImageHash(const gfx::Image& favicon) { |
+ SkBitmap bitmap = favicon.AsBitmap(); |
+ bitmap.lockPixels(); |
+ 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.
|
+ reinterpret_cast<char*>(bitmap.getPixels()), bitmap.getSize()); |
+ bitmap.unlockPixels(); |
+ |
+ return value; |
+} |
+ |
+gfx::ImageSkia CreateEnlargedFaviconImage(gfx::Size size, gfx::Image& favicon) { |
+ if (size.width() < 20 || size.height() < 20) |
+ 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.
|
+ |
+ // Create a bitmap. |
+ SkBitmap result; |
+ result.allocN32Pixels(size.width(), size.height(), true); |
+ SkCanvas canvas(result); |
+ |
+ // Fill with white. |
+ 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
|
+ |
+ // Draw black border. |
+ const int thickness = result.width() / 30; |
+ SkPaint paint; |
+ paint.setARGB(255, 0, 0, 0); |
+ paint.setStyle(SkPaint::kStroke_Style); |
+ paint.setStrokeWidth(thickness); |
+ canvas.drawRectCoords(thickness, // left |
+ thickness, // top |
+ result.width() - thickness, // right |
+ result.height() - thickness, // bottom |
+ paint); |
+ |
+ // Draw a scaled favicon image into the center of result image to take up to |
+ // 3/4 of result image. |
+ 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.
|
+ 3.0 / 4 * result.height() / favicon.Height()); |
+ SkRect dest_rect; |
+ 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.
|
+ result.height() / 2 - favicon.Height() * scale / 2, // top |
+ result.width() / 2 + favicon.Width() * scale / 2, // right |
+ result.height() / 2 + favicon.Height() * scale / 2); // bottom |
+ 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.
|
+ SkImage::NewFromBitmap(favicon.AsBitmap())); |
+ canvas.drawImageRect(temp_image.get(), dest_rect, nullptr); |
+ |
+ // Create a skia image. |
+ return gfx::ImageSkia::CreateFrom1xBitmap(result); |
+} |
+ |
+// Update the list once per second. |
+const int kDefaultUpdatePeriod = 1000; |
+ |
+} // namespace |
+ |
+TabDesktopMediaList::TabDesktopMediaList() |
+ : DesktopMediaListBase( |
+ base::TimeDelta::FromMilliseconds(kDefaultUpdatePeriod)) {} |
+ |
+TabDesktopMediaList::~TabDesktopMediaList() {} |
+ |
+void TabDesktopMediaList::Refresh() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ std::map<DesktopMediaID, gfx::Image> favicon_map; |
+ ImageHashesMap new_favicon_hashes; |
+ std::vector<SourceDescription> sources; |
+ |
+ if (!fake_sources_.size()) { |
+ Profile* profile = ProfileManager::GetLastUsedProfileAllowedByPolicy(); |
+ std::vector<Browser*> browsers; |
+ for (chrome::BrowserIterator it; !it.done(); it.Next()) { |
+ if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile()) |
+ browsers.push_back(*it); |
+ } |
+ |
+ std::map<base::TimeTicks, SourceDescription> tab_map; |
+ |
+ // Enumerate all tabs with their titles and favicons for a user profile. |
+ for (auto browser : browsers) { |
+ const TabStripModel* tab_strip_model = browser->tab_strip_model(); |
+ DCHECK(tab_strip_model); |
+ |
+ for (int i = 0; i < tab_strip_model->count(); i++) { |
+ // Create id for tab. |
+ content::WebContents* contents = tab_strip_model->GetWebContentsAt(i); |
+ DCHECK(contents); |
+ content::RenderFrameHost* main_frame = contents->GetMainFrame(); |
+ DCHECK(main_frame); |
+ DesktopMediaID media_id( |
+ DesktopMediaID::TYPE_WEB_CONTENTS, DesktopMediaID::kNullId, |
+ content::WebContentsMediaCaptureId( |
+ main_frame->GetProcess()->GetID(), main_frame->GetRoutingID())); |
+ |
+ // Create display tab title. |
+ const base::string16 title = l10n_util::GetStringFUTF16( |
+ IDS_DESKTOP_MEDIA_PICKER_CHROME_TAB_TITLE, contents->GetTitle()); |
+ |
+ // Get tab's last active time stamp. |
+ const base::TimeTicks t = contents->GetLastActiveTime(); |
+ 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
|
+ |
+ // Get favicon for tab. |
+ favicon::FaviconDriver* favicon_driver = |
+ favicon::ContentFaviconDriver::FromWebContents(contents); |
+ if (favicon_driver) { |
+ gfx::Image favicon = favicon_driver->GetFavicon(); |
+ // Only new or changed favicon need update. |
+ new_favicon_hashes[media_id] = GetImageHash(favicon); |
+ if (!favicon_hashes_.count(media_id) || |
+ (favicon_hashes_[media_id] != new_favicon_hashes[media_id])) { |
+ 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.
|
+ std::pair<DesktopMediaID, gfx::Image>(media_id, favicon)); |
+ } |
+ } |
+ } |
+ } |
+ |
+ // Sort tab sources by time. Most recent one first. Then update sources |
+ // list. |
+ for (auto it = tab_map.rbegin(); it != tab_map.rend(); ++it) { |
+ SourceDescription& source = it->second; |
+ sources.push_back(source); |
+ } |
+ } else { |
+ // 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
|
+ for (size_t i = 0; i < fake_sources_.size(); i++) { |
+ // Only new or changed favicon need update. |
+ DesktopMediaID media_id = fake_sources_[i].id; |
+ new_favicon_hashes[media_id] = GetImageHash(fake_favicons_[i]); |
+ if (!favicon_hashes_.count(media_id) || |
+ (favicon_hashes_[media_id] != new_favicon_hashes[media_id])) { |
+ favicon_map.insert( |
+ std::pair<DesktopMediaID, gfx::Image>(media_id, fake_favicons_[i])); |
+ } |
+ } |
+ |
+ sources = fake_sources_; |
+ } |
+ favicon_hashes_ = new_favicon_hashes; |
+ |
+ UpdateSourcesList(sources); |
+ |
+ for (auto it : favicon_map) |
+ UpdateSourceThumbnail( |
+ it.first, CreateEnlargedFaviconImage(thumbnail_size_, it.second)); |
+ |
+ ScheduleNextRefresh(); |
+} |
+ |
+void TabDesktopMediaList::SetFakeSourcesForTesting( |
+ std::vector<SourceDescription>& sources, |
+ std::vector<gfx::Image>& favicons) { |
+ fake_sources_ = sources; |
+ fake_favicons_ = favicons; |
+} |