Commit fe27c47c authored by Xiangjun Zhang's avatar Xiangjun Zhang Committed by Commit Bot

Media remoting dialog: Check pref before showing dialog.

This CL adds the implementation to check the preference of
enabling/disabling media remoting. The remoting dialog is only shown
when user has not set the preference.

The preference can be set through the remoting dialog. Though this CL
doesn't clear the preference once it is set. This will be done in a
later CL.

Bug: 849020
Change-Id: I89c4ed6b71a2e51bd81fae919531d15b53664b18
Reviewed-on: https://chromium-review.googlesource.com/1125355
Commit-Queue: Xiangjun Zhang <xjz@chromium.org>
Reviewed-by: default avatarDerek Cheng <imcheng@chromium.org>
Reviewed-by: default avatarTakumi Fujimoto <takumif@chromium.org>
Cr-Commit-Position: refs/heads/master@{#574954}
parent 1948b482
...@@ -4,8 +4,13 @@ ...@@ -4,8 +4,13 @@
#include "chrome/browser/ui/views/media_router/media_remoting_dialog_view.h" #include "chrome/browser/ui/views/media_router/media_remoting_dialog_view.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
#include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/button/checkbox.h" #include "ui/views/controls/button/checkbox.h"
#include "ui/views/layout/box_layout.h" #include "ui/views/layout/box_layout.h"
...@@ -14,9 +19,47 @@ ...@@ -14,9 +19,47 @@
namespace media_router { namespace media_router {
// static // static
void MediaRemotingDialogView::ShowDialog(views::View* anchor_view) { void MediaRemotingDialogView::GetPermission(content::WebContents* web_contents,
DCHECK(!instance_); PermissionCallback callback) {
instance_ = new MediaRemotingDialogView(anchor_view); HideDialog(); // Close the previous dialog if it is still showing.
DCHECK(web_contents);
DCHECK(callback);
// Check whether user has set the permission.
PrefService* const pref_service =
Profile::FromBrowserContext(web_contents->GetBrowserContext())
->GetPrefs();
DCHECK(pref_service);
const PrefService::Preference* pref =
pref_service->FindPreference(prefs::kMediaRouterMediaRemotingEnabled);
if (pref && !pref->IsDefaultValue()) {
std::move(callback).Run(pref->GetValue()->GetBool());
return;
}
// Show dialog.
Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
if (!browser) {
std::move(callback).Run(false);
return;
}
BrowserActionsContainer* browser_actions =
BrowserView::GetBrowserViewForBrowser(browser)
->toolbar()
->browser_actions();
// |browser_actions| may be null in toolbar-less browser windows.
// TODO(takumif): Show the dialog at some default position if the toolbar is
// missing.
if (!browser_actions) {
std::move(callback).Run(false);
return;
}
views::View* anchor_view = browser_actions->GetViewForId(
ComponentToolbarActionsFactory::kMediaRouterActionId);
instance_ = new MediaRemotingDialogView(anchor_view, pref_service,
std::move(callback));
views::Widget* widget = views::Widget* widget =
views::BubbleDialogDelegateView::CreateBubble(instance_); views::BubbleDialogDelegateView::CreateBubble(instance_);
widget->Show(); widget->Show();
...@@ -59,21 +102,17 @@ int MediaRemotingDialogView::GetDialogButtons() const { ...@@ -59,21 +102,17 @@ int MediaRemotingDialogView::GetDialogButtons() const {
} }
bool MediaRemotingDialogView::Accept() { bool MediaRemotingDialogView::Accept() {
// TODO(https://crbug.com/849020): Notify the Media Remoting feature that it ReportPermission(true);
// should be enabled. return true;
bool should_close_dialog = true;
return should_close_dialog;
} }
bool MediaRemotingDialogView::Cancel() { bool MediaRemotingDialogView::Cancel() {
// TODO(https://crbug.com/849020): Notify the Media Remoting feature that it ReportPermission(false);
// shouldn't be enabled. return true;
bool should_close_dialog = true;
return should_close_dialog;
} }
bool MediaRemotingDialogView::Close() { bool MediaRemotingDialogView::Close() {
return Cancel(); return true;
} }
gfx::Size MediaRemotingDialogView::CalculatePreferredSize() const { gfx::Size MediaRemotingDialogView::CalculatePreferredSize() const {
...@@ -82,10 +121,15 @@ gfx::Size MediaRemotingDialogView::CalculatePreferredSize() const { ...@@ -82,10 +121,15 @@ gfx::Size MediaRemotingDialogView::CalculatePreferredSize() const {
return gfx::Size(width, GetHeightForWidth(width)); return gfx::Size(width, GetHeightForWidth(width));
} }
MediaRemotingDialogView::MediaRemotingDialogView(views::View* anchor_view) MediaRemotingDialogView::MediaRemotingDialogView(views::View* anchor_view,
PrefService* pref_service,
PermissionCallback callback)
: BubbleDialogDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), : BubbleDialogDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT),
permission_callback_(std::move(callback)),
pref_service_(pref_service),
dialog_title_( dialog_title_(
l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_REMOTING_DIALOG_TITLE)) { l10n_util::GetStringUTF16(IDS_MEDIA_ROUTER_REMOTING_DIALOG_TITLE)) {
DCHECK(pref_service_);
SetLayoutManager( SetLayoutManager(
std::make_unique<views::BoxLayout>(views::BoxLayout::kVertical)); std::make_unique<views::BoxLayout>(views::BoxLayout::kVertical));
} }
...@@ -110,6 +154,14 @@ void MediaRemotingDialogView::WindowClosing() { ...@@ -110,6 +154,14 @@ void MediaRemotingDialogView::WindowClosing() {
instance_ = nullptr; instance_ = nullptr;
} }
void MediaRemotingDialogView::ReportPermission(bool allowed) {
DCHECK(remember_choice_checkbox_);
DCHECK(permission_callback_);
if (remember_choice_checkbox_->checked())
pref_service_->SetBoolean(prefs::kMediaRouterMediaRemotingEnabled, allowed);
std::move(permission_callback_).Run(allowed);
}
// static // static
MediaRemotingDialogView* MediaRemotingDialogView::instance_ = nullptr; MediaRemotingDialogView* MediaRemotingDialogView::instance_ = nullptr;
......
...@@ -7,10 +7,16 @@ ...@@ -7,10 +7,16 @@
#include "ui/views/bubble/bubble_dialog_delegate.h" #include "ui/views/bubble/bubble_dialog_delegate.h"
class PrefService;
namespace views { namespace views {
class Checkbox; class Checkbox;
} // namespace views } // namespace views
namespace content {
class WebContents;
} // namespace content
namespace media_router { namespace media_router {
// Dialog that the user can use to either enable Media Remoting or keep it // Dialog that the user can use to either enable Media Remoting or keep it
...@@ -19,11 +25,13 @@ namespace media_router { ...@@ -19,11 +25,13 @@ namespace media_router {
// then mirroring it remotely. // then mirroring it remotely.
class MediaRemotingDialogView : public views::BubbleDialogDelegateView { class MediaRemotingDialogView : public views::BubbleDialogDelegateView {
public: public:
// Instantiates and shows the singleton dialog. The dialog must not be using PermissionCallback = base::OnceCallback<void(bool)>;
// currently shown. // Checks the existing preference value, and if unset, instantiates and shows
// TODO(https://crbug.com/849020): This method needs to take a callback to // the singleton dialog to get user's permission. |callback| runs on the same
// call when the user interacts with the dialog. // stack if the preference was set, or asynchronously when user sets the
static void ShowDialog(views::View* anchor_view); // permission through the dialog.
static void GetPermission(content::WebContents* web_contents,
PermissionCallback callback);
// No-op if the dialog is currently not shown. // No-op if the dialog is currently not shown.
static void HideDialog(); static void HideDialog();
...@@ -49,17 +57,26 @@ class MediaRemotingDialogView : public views::BubbleDialogDelegateView { ...@@ -49,17 +57,26 @@ class MediaRemotingDialogView : public views::BubbleDialogDelegateView {
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
private: private:
explicit MediaRemotingDialogView(views::View* anchor_view); explicit MediaRemotingDialogView(views::View* anchor_view,
PrefService* pref_service,
PermissionCallback callback);
~MediaRemotingDialogView() override; ~MediaRemotingDialogView() override;
// views::BubbleDialogDelegateView: // views::BubbleDialogDelegateView:
void Init() override; void Init() override;
void WindowClosing() override; void WindowClosing() override;
// Runs |permission_callback_| to report whether remoting is allowed by user.
void ReportPermission(bool allowed);
// The singleton dialog instance. This is a nullptr when a dialog is not // The singleton dialog instance. This is a nullptr when a dialog is not
// shown. // shown.
static MediaRemotingDialogView* instance_; static MediaRemotingDialogView* instance_;
PermissionCallback permission_callback_;
PrefService* const pref_service_;
// Title shown at the top of the dialog. // Title shown at the top of the dialog.
base::string16 dialog_title_; base::string16 dialog_title_;
......
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