Commit 9cbb2e7b authored by estade@chromium.org's avatar estade@chromium.org

platform apps: use app name for WebRTC balloons

BUG=132846
TEST=manual

Review URL: https://chromiumcodereview.appspot.com/10534174

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@150443 0039d316-1c4b-4281-b951-d872f2087c98
parent 7edf0125
......@@ -9,6 +9,8 @@
#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/status_icons/status_icon.h"
#include "chrome/browser/status_icons/status_tray.h"
#include "chrome/browser/tab_contents/tab_util.h"
......@@ -27,6 +29,83 @@
using content::BrowserThread;
using content::WebContents;
namespace {
const extensions::Extension* GetExtension(int render_process_id,
int render_view_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
WebContents* web_contents = tab_util::GetWebContentsByID(
render_process_id, render_view_id);
if (!web_contents)
return NULL;
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
if (!profile)
return NULL;
ExtensionService* extension_service = profile->GetExtensionService();
if (!extension_service)
return NULL;
return extension_service->extensions()->GetExtensionOrAppByURL(
ExtensionURLInfo(web_contents->GetURL()));
}
// Gets the security originator of the tab. It returns a string with no '/'
// at the end to display in the UI.
string16 GetSecurityOrigin(int render_process_id, int render_view_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
WebContents* tab_content = tab_util::GetWebContentsByID(
render_process_id, render_view_id);
if (!tab_content)
return string16();
std::string security_origin = tab_content->GetURL().GetOrigin().spec();
// Remove the last character if it is a '/'.
if (!security_origin.empty()) {
std::string::iterator it = security_origin.end() - 1;
if (*it == '/')
security_origin.erase(it);
}
return UTF8ToUTF16(security_origin);
}
string16 GetTitle(int render_process_id, int render_view_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const extensions::Extension* extension =
GetExtension(render_process_id, render_view_id);
if (extension)
return UTF8ToUTF16(extension->name());
WebContents* tab_content = tab_util::GetWebContentsByID(
render_process_id, render_view_id);
if (!tab_content)
return string16();
string16 tab_title = tab_content->GetTitle();
if (tab_title.empty()) {
// If the page's title is empty use its security originator.
tab_title = GetSecurityOrigin(render_process_id, render_view_id);
} else {
// If the page's title matches its URL, use its security originator.
std::string languages =
content::GetContentClient()->browser()->GetAcceptLangs(
tab_content->GetBrowserContext());
if (tab_title == net::FormatUrl(tab_content->GetURL(), languages))
tab_title = GetSecurityOrigin(render_process_id, render_view_id);
}
return tab_title;
}
} // namespace
MediaStreamCaptureIndicator::TabEquals::TabEquals(int render_process_id,
int render_view_id)
: render_process_id_(render_process_id),
......@@ -42,7 +121,9 @@ MediaStreamCaptureIndicator::MediaStreamCaptureIndicator()
: status_icon_(NULL),
mic_image_(NULL),
camera_image_(NULL),
balloon_image_(NULL) {
balloon_image_(NULL),
ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)),
request_index_(0) {
}
MediaStreamCaptureIndicator::~MediaStreamCaptureIndicator() {
......@@ -178,10 +259,9 @@ void MediaStreamCaptureIndicator::ShowBalloon(
int render_process_id,
int render_view_id,
bool audio,
bool video) const {
bool video) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(audio || video);
string16 title = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
int message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_AUDIO_AND_VIDEO;
if (audio && !video)
......@@ -189,12 +269,42 @@ void MediaStreamCaptureIndicator::ShowBalloon(
else if (!audio && video)
message_id = IDS_MEDIA_STREAM_STATUS_TRAY_BALLOON_BODY_VIDEO_ONLY;
string16 body = l10n_util::GetStringFUTF16(
message_id, GetSecurityOrigin(render_process_id, render_view_id));
const extensions::Extension* extension =
GetExtension(render_process_id, render_view_id);
if (extension) {
pending_messages_[request_index_++] =
l10n_util::GetStringFUTF16(message_id,
UTF8ToUTF16(extension->name()));
tracker_.LoadImage(
extension,
extension->GetIconResource(32, ExtensionIconSet::MATCH_BIGGER),
gfx::Size(32, 32),
ImageLoadingTracker::CACHE);
return;
}
string16 title = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
string16 body = l10n_util::GetStringFUTF16(message_id,
GetSecurityOrigin(render_process_id, render_view_id));
status_icon_->DisplayBalloon(*balloon_image_, title, body);
}
void MediaStreamCaptureIndicator::OnImageLoaded(
const gfx::Image& image,
const std::string& extension_id,
int index) {
string16 message;
message.swap(pending_messages_[index]);
pending_messages_.erase(index);
status_icon_->DisplayBalloon(
!image.IsEmpty() ? *image.ToImageSkia() :
*ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
IDR_APP_DEFAULT_ICON),
string16(),
message);
}
void MediaStreamCaptureIndicator::Hide() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!status_icon_)
......@@ -335,47 +445,3 @@ void MediaStreamCaptureIndicator::RemoveCaptureDeviceTab(
UpdateStatusTrayIconContextMenu();
}
string16 MediaStreamCaptureIndicator::GetTitle(int render_process_id,
int render_view_id) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
WebContents* tab_content = tab_util::GetWebContentsByID(
render_process_id, render_view_id);
if (!tab_content)
return string16();
string16 tab_title = tab_content->GetTitle();
if (tab_title.empty()) {
// If the page's title is empty use its security originator.
tab_title = GetSecurityOrigin(render_process_id, render_view_id);
} else {
// If the page's title matches its URL, use its security originator.
std::string languages =
content::GetContentClient()->browser()->GetAcceptLangs(
tab_content->GetBrowserContext());
if (tab_title == net::FormatUrl(tab_content->GetURL(), languages))
tab_title = GetSecurityOrigin(render_process_id, render_view_id);
}
return tab_title;
}
string16 MediaStreamCaptureIndicator::GetSecurityOrigin(
int render_process_id, int render_view_id) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
WebContents* tab_content = tab_util::GetWebContentsByID(
render_process_id, render_view_id);
if (!tab_content)
return string16();
std::string security_origin = tab_content->GetURL().GetOrigin().spec();
// Remove the last character if it is a '/'.
if (!security_origin.empty()) {
std::string::iterator it = security_origin.end() - 1;
if (*it == '/')
security_origin.erase(it);
}
return UTF8ToUTF16(security_origin);
}
......@@ -9,6 +9,7 @@
#include <vector>
#include "base/memory/ref_counted.h"
#include "chrome/browser/extensions/image_loading_tracker.h"
#include "content/public/common/media_stream_request.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/gfx/image/image_skia.h"
......@@ -20,7 +21,8 @@ class StatusTray;
// is deleted.
class MediaStreamCaptureIndicator
: public base::RefCountedThreadSafe<MediaStreamCaptureIndicator>,
public ui::SimpleMenuModel::Delegate {
public ui::SimpleMenuModel::Delegate,
public ImageLoadingTracker::Observer {
public:
MediaStreamCaptureIndicator();
......@@ -42,8 +44,15 @@ class MediaStreamCaptureIndicator
int render_view_id,
const content::MediaStreamDevices& devices);
// ImageLoadingTracker::Observer implementation.
virtual void OnImageLoaded(const gfx::Image& image,
const std::string& extension_id,
int index) OVERRIDE;
private:
// Struct to store the usage information of the capture devices for each tab.
// TODO(estade): this should be called CaptureDeviceContents; not all the
// render views it represents are tabs.
struct CaptureDeviceTab {
CaptureDeviceTab(int render_process_id,
int render_view_id)
......@@ -103,19 +112,12 @@ class MediaStreamCaptureIndicator
// Triggers a balloon in the corner telling capture devices are being used.
// This function is called by AddCaptureDeviceTab().
void ShowBalloon(int render_process_id, int render_view_id,
bool audio, bool video) const;
bool audio, bool video);
// Hides the status tray from the desktop. This function is called by
// RemoveCaptureDeviceTab() when the device usage list becomes empty.
void Hide();
// Gets the title of the tab.
string16 GetTitle(int render_process_id, int render_view_id) const;
// Gets the security originator of the tab. It returns a string with no '/'
// at the end to display in the UI.
string16 GetSecurityOrigin(int render_process_id, int render_view_id) const;
// Updates the status tray menu with the new device list. This call will be
// triggered by both AddCaptureDeviceTab() and RemoveCaptureDeviceTab().
void UpdateStatusTrayIconContextMenu();
......@@ -137,6 +139,14 @@ class MediaStreamCaptureIndicator
// A list that contains the usage information of the opened capture devices.
typedef std::vector<CaptureDeviceTab> CaptureDeviceTabs;
CaptureDeviceTabs tabs_;
// Tracks the load of extension icons.
ImageLoadingTracker tracker_;
// The messages to display when extension images are loaded. The index
// corresponds to the index of the associated LoadImage request.
std::map<int, string16> pending_messages_;
// Tracks the number of requests to |tracker_|.
int request_index_;
};
#endif // CHROME_BROWSER_MEDIA_MEDIA_STREAM_CAPTURE_INDICATOR_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment