| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/media/desktop_media_picker.h" | 5 #include "chrome/browser/media/desktop_media_picker.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "chrome/browser/media/desktop_media_list.h" | 8 #include "chrome/browser/media/desktop_media_list.h" |
| 9 #include "chrome/browser/media/desktop_media_list_observer.h" | 9 #include "chrome/browser/media/desktop_media_list_observer.h" |
| 10 #include "chrome/browser/ui/ash/ash_util.h" | 10 #include "chrome/browser/ui/ash/ash_util.h" |
| 11 #include "components/web_modal/web_contents_modal_dialog_host.h" |
| 12 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 13 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" |
| 11 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 12 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 13 #include "ui/aura/window_tree_host.h" | 16 #include "ui/aura/window_tree_host.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/events/keycodes/keyboard_codes.h" | 18 #include "ui/events/keycodes/keyboard_codes.h" |
| 16 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
| 17 #include "ui/native_theme/native_theme.h" | 20 #include "ui/native_theme/native_theme.h" |
| 18 #include "ui/views/background.h" | 21 #include "ui/views/background.h" |
| 19 #include "ui/views/controls/image_view.h" | 22 #include "ui/views/controls/image_view.h" |
| 20 #include "ui/views/controls/label.h" | 23 #include "ui/views/controls/label.h" |
| 21 #include "ui/views/controls/scroll_view.h" | 24 #include "ui/views/controls/scroll_view.h" |
| 22 #include "ui/views/layout/box_layout.h" | 25 #include "ui/views/layout/box_layout.h" |
| 23 #include "ui/views/layout/layout_constants.h" | 26 #include "ui/views/layout/layout_constants.h" |
| 24 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
| 25 #include "ui/views/window/dialog_client_view.h" | 28 #include "ui/views/window/dialog_client_view.h" |
| 26 #include "ui/views/window/dialog_delegate.h" | 29 #include "ui/views/window/dialog_delegate.h" |
| 27 #include "ui/wm/core/shadow_types.h" | 30 #include "ui/wm/core/shadow_types.h" |
| 28 | 31 |
| 29 using content::DesktopMediaID; | 32 using content::DesktopMediaID; |
| 33 using web_modal::WebContentsModalDialogManager; |
| 34 using web_modal::WebContentsModalDialogManagerDelegate; |
| 30 | 35 |
| 31 namespace { | 36 namespace { |
| 32 | 37 |
| 33 const int kThumbnailWidth = 160; | 38 const int kThumbnailWidth = 160; |
| 34 const int kThumbnailHeight = 100; | 39 const int kThumbnailHeight = 100; |
| 35 const int kThumbnailMargin = 10; | 40 const int kThumbnailMargin = 10; |
| 36 const int kLabelHeight = 40; | 41 const int kLabelHeight = 40; |
| 37 const int kListItemWidth = kThumbnailMargin * 2 + kThumbnailWidth; | 42 const int kListItemWidth = kThumbnailMargin * 2 + kThumbnailWidth; |
| 38 const int kListItemHeight = | 43 const int kListItemHeight = |
| 39 kThumbnailMargin * 2 + kThumbnailHeight + kLabelHeight; | 44 kThumbnailMargin * 2 + kThumbnailHeight + kLabelHeight; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 145 |
| 141 DesktopMediaPickerDialogView* parent_; | 146 DesktopMediaPickerDialogView* parent_; |
| 142 scoped_ptr<DesktopMediaList> media_list_; | 147 scoped_ptr<DesktopMediaList> media_list_; |
| 143 | 148 |
| 144 DISALLOW_COPY_AND_ASSIGN(DesktopMediaListView); | 149 DISALLOW_COPY_AND_ASSIGN(DesktopMediaListView); |
| 145 }; | 150 }; |
| 146 | 151 |
| 147 // Dialog view used for DesktopMediaPickerViews. | 152 // Dialog view used for DesktopMediaPickerViews. |
| 148 class DesktopMediaPickerDialogView : public views::DialogDelegateView { | 153 class DesktopMediaPickerDialogView : public views::DialogDelegateView { |
| 149 public: | 154 public: |
| 150 DesktopMediaPickerDialogView(gfx::NativeWindow context, | 155 DesktopMediaPickerDialogView(content::WebContents* parent_web_contents, |
| 156 gfx::NativeWindow context, |
| 151 gfx::NativeWindow parent_window, | 157 gfx::NativeWindow parent_window, |
| 152 DesktopMediaPickerViews* parent, | 158 DesktopMediaPickerViews* parent, |
| 153 const base::string16& app_name, | 159 const base::string16& app_name, |
| 154 const base::string16& target_name, | 160 const base::string16& target_name, |
| 155 scoped_ptr<DesktopMediaList> media_list); | 161 scoped_ptr<DesktopMediaList> media_list); |
| 156 virtual ~DesktopMediaPickerDialogView(); | 162 virtual ~DesktopMediaPickerDialogView(); |
| 157 | 163 |
| 158 // Called by parent (DesktopMediaPickerViews) when it's destroyed. | 164 // Called by parent (DesktopMediaPickerViews) when it's destroyed. |
| 159 void DetachParent(); | 165 void DetachParent(); |
| 160 | 166 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 187 | 193 |
| 188 // Implementation of DesktopMediaPicker for Views. | 194 // Implementation of DesktopMediaPicker for Views. |
| 189 class DesktopMediaPickerViews : public DesktopMediaPicker { | 195 class DesktopMediaPickerViews : public DesktopMediaPicker { |
| 190 public: | 196 public: |
| 191 DesktopMediaPickerViews(); | 197 DesktopMediaPickerViews(); |
| 192 virtual ~DesktopMediaPickerViews(); | 198 virtual ~DesktopMediaPickerViews(); |
| 193 | 199 |
| 194 void NotifyDialogResult(DesktopMediaID source); | 200 void NotifyDialogResult(DesktopMediaID source); |
| 195 | 201 |
| 196 // DesktopMediaPicker overrides. | 202 // DesktopMediaPicker overrides. |
| 197 virtual void Show(gfx::NativeWindow context, | 203 virtual void Show(content::WebContents* web_contents, |
| 204 gfx::NativeWindow context, |
| 198 gfx::NativeWindow parent, | 205 gfx::NativeWindow parent, |
| 199 const base::string16& app_name, | 206 const base::string16& app_name, |
| 200 const base::string16& target_name, | 207 const base::string16& target_name, |
| 201 scoped_ptr<DesktopMediaList> media_list, | 208 scoped_ptr<DesktopMediaList> media_list, |
| 202 const DoneCallback& done_callback) OVERRIDE; | 209 const DoneCallback& done_callback) OVERRIDE; |
| 203 | 210 |
| 204 private: | 211 private: |
| 205 DoneCallback callback_; | 212 DoneCallback callback_; |
| 206 | 213 |
| 207 // The |dialog_| is owned by the corresponding views::Widget instance. | 214 // The |dialog_| is owned by the corresponding views::Widget instance. |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 } | 485 } |
| 479 | 486 |
| 480 void DesktopMediaListView::OnSourceThumbnailChanged(int index) { | 487 void DesktopMediaListView::OnSourceThumbnailChanged(int index) { |
| 481 const DesktopMediaList::Source& source = media_list_->GetSource(index); | 488 const DesktopMediaList::Source& source = media_list_->GetSource(index); |
| 482 DesktopMediaSourceView* source_view = | 489 DesktopMediaSourceView* source_view = |
| 483 static_cast<DesktopMediaSourceView*>(child_at(index)); | 490 static_cast<DesktopMediaSourceView*>(child_at(index)); |
| 484 source_view->SetThumbnail(source.thumbnail); | 491 source_view->SetThumbnail(source.thumbnail); |
| 485 } | 492 } |
| 486 | 493 |
| 487 DesktopMediaPickerDialogView::DesktopMediaPickerDialogView( | 494 DesktopMediaPickerDialogView::DesktopMediaPickerDialogView( |
| 495 content::WebContents* parent_web_contents, |
| 488 gfx::NativeWindow context, | 496 gfx::NativeWindow context, |
| 489 gfx::NativeWindow parent_window, | 497 gfx::NativeWindow parent_window, |
| 490 DesktopMediaPickerViews* parent, | 498 DesktopMediaPickerViews* parent, |
| 491 const base::string16& app_name, | 499 const base::string16& app_name, |
| 492 const base::string16& target_name, | 500 const base::string16& target_name, |
| 493 scoped_ptr<DesktopMediaList> media_list) | 501 scoped_ptr<DesktopMediaList> media_list) |
| 494 : parent_(parent), | 502 : parent_(parent), |
| 495 app_name_(app_name), | 503 app_name_(app_name), |
| 496 label_(new views::Label()), | 504 label_(new views::Label()), |
| 497 scroll_view_(views::ScrollView::CreateScrollViewWithBorder()), | 505 scroll_view_(views::ScrollView::CreateScrollViewWithBorder()), |
| 498 list_view_(new DesktopMediaListView(this, media_list.Pass())) { | 506 list_view_(new DesktopMediaListView(this, media_list.Pass())) { |
| 499 if (app_name == target_name) { | 507 if (app_name == target_name) { |
| 500 label_->SetText( | 508 label_->SetText( |
| 501 l10n_util::GetStringFUTF16(IDS_DESKTOP_MEDIA_PICKER_TEXT, app_name)); | 509 l10n_util::GetStringFUTF16(IDS_DESKTOP_MEDIA_PICKER_TEXT, app_name)); |
| 502 } else { | 510 } else { |
| 503 label_->SetText(l10n_util::GetStringFUTF16( | 511 label_->SetText(l10n_util::GetStringFUTF16( |
| 504 IDS_DESKTOP_MEDIA_PICKER_TEXT_DELEGATED, app_name, target_name)); | 512 IDS_DESKTOP_MEDIA_PICKER_TEXT_DELEGATED, app_name, target_name)); |
| 505 } | 513 } |
| 506 label_->SetMultiLine(true); | 514 label_->SetMultiLine(true); |
| 507 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 515 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 508 AddChildView(label_); | 516 AddChildView(label_); |
| 509 | 517 |
| 510 scroll_view_->SetContents(list_view_); | 518 scroll_view_->SetContents(list_view_); |
| 511 AddChildView(scroll_view_); | 519 AddChildView(scroll_view_); |
| 512 | 520 |
| 513 DialogDelegate::CreateDialogWidget(this, context, parent_window); | 521 WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
| 522 WebContentsModalDialogManager::FromWebContents(parent_web_contents); |
| 523 DCHECK(web_contents_modal_dialog_manager); |
| 524 WebContentsModalDialogManagerDelegate* delegate = |
| 525 web_contents_modal_dialog_manager->delegate(); |
| 526 DCHECK(delegate); |
| 527 views::Widget::CreateWindowAsFramelessChild( |
| 528 this, |
| 529 delegate->GetWebContentsModalDialogHost()->GetHostView()); |
| 514 | 530 |
| 515 // DesktopMediaList needs to know the ID of the picker window which | 531 // DesktopMediaList needs to know the ID of the picker window which |
| 516 // matches the ID it gets from the OS. Depending on the OS and configuration | 532 // matches the ID it gets from the OS. Depending on the OS and configuration |
| 517 // we get this ID differently. | 533 // we get this ID differently. |
| 518 content::DesktopMediaID::Id dialog_window_id = 0; | 534 content::DesktopMediaID::Id dialog_window_id = 0; |
| 519 | 535 |
| 520 #if defined(USE_ASH) | 536 #if defined(USE_ASH) |
| 521 if (chrome::IsNativeWindowInAsh(GetWidget()->GetNativeWindow())) { | 537 if (chrome::IsNativeWindowInAsh(GetWidget()->GetNativeWindow())) { |
| 522 dialog_window_id = content::DesktopMediaID::RegisterAuraWindow( | 538 dialog_window_id = content::DesktopMediaID::RegisterAuraWindow( |
| 523 GetWidget()->GetNativeWindow()).id; | 539 GetWidget()->GetNativeWindow()).id; |
| 524 } else | 540 } else |
| 525 #endif | 541 #endif |
| 526 { | 542 { |
| 527 dialog_window_id = AcceleratedWidgetToDesktopMediaId( | 543 dialog_window_id = AcceleratedWidgetToDesktopMediaId( |
| 528 GetWidget()->GetNativeWindow()->GetHost()-> | 544 GetWidget()->GetNativeWindow()->GetHost()-> |
| 529 GetAcceleratedWidget()); | 545 GetAcceleratedWidget()); |
| 530 } | 546 } |
| 531 | 547 |
| 532 list_view_->StartUpdating(dialog_window_id); | 548 list_view_->StartUpdating(dialog_window_id); |
| 533 | 549 |
| 534 GetWidget()->Show(); | 550 web_contents_modal_dialog_manager->ShowModalDialog( |
| 551 GetWidget()->GetNativeView()); |
| 535 } | 552 } |
| 536 | 553 |
| 537 DesktopMediaPickerDialogView::~DesktopMediaPickerDialogView() {} | 554 DesktopMediaPickerDialogView::~DesktopMediaPickerDialogView() {} |
| 538 | 555 |
| 539 void DesktopMediaPickerDialogView::DetachParent() { | 556 void DesktopMediaPickerDialogView::DetachParent() { |
| 540 parent_ = NULL; | 557 parent_ = NULL; |
| 541 } | 558 } |
| 542 | 559 |
| 543 gfx::Size DesktopMediaPickerDialogView::GetPreferredSize() { | 560 gfx::Size DesktopMediaPickerDialogView::GetPreferredSize() { |
| 544 return gfx::Size(600, 500); | 561 return gfx::Size(600, 500); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 : dialog_(NULL) { | 629 : dialog_(NULL) { |
| 613 } | 630 } |
| 614 | 631 |
| 615 DesktopMediaPickerViews::~DesktopMediaPickerViews() { | 632 DesktopMediaPickerViews::~DesktopMediaPickerViews() { |
| 616 if (dialog_) { | 633 if (dialog_) { |
| 617 dialog_->DetachParent(); | 634 dialog_->DetachParent(); |
| 618 dialog_->GetWidget()->Close(); | 635 dialog_->GetWidget()->Close(); |
| 619 } | 636 } |
| 620 } | 637 } |
| 621 | 638 |
| 622 void DesktopMediaPickerViews::Show(gfx::NativeWindow context, | 639 void DesktopMediaPickerViews::Show(content::WebContents* web_contents, |
| 640 gfx::NativeWindow context, |
| 623 gfx::NativeWindow parent, | 641 gfx::NativeWindow parent, |
| 624 const base::string16& app_name, | 642 const base::string16& app_name, |
| 625 const base::string16& target_name, | 643 const base::string16& target_name, |
| 626 scoped_ptr<DesktopMediaList> media_list, | 644 scoped_ptr<DesktopMediaList> media_list, |
| 627 const DoneCallback& done_callback) { | 645 const DoneCallback& done_callback) { |
| 628 callback_ = done_callback; | 646 callback_ = done_callback; |
| 629 dialog_ = new DesktopMediaPickerDialogView( | 647 dialog_ = new DesktopMediaPickerDialogView( |
| 630 context, parent, this, app_name, target_name, media_list.Pass()); | 648 web_contents, context, parent, this, app_name, target_name, |
| 649 media_list.Pass()); |
| 631 } | 650 } |
| 632 | 651 |
| 633 void DesktopMediaPickerViews::NotifyDialogResult( | 652 void DesktopMediaPickerViews::NotifyDialogResult( |
| 634 DesktopMediaID source) { | 653 DesktopMediaID source) { |
| 635 // Once this method is called the |dialog_| will close and destroy itself. | 654 // Once this method is called the |dialog_| will close and destroy itself. |
| 636 dialog_->DetachParent(); | 655 dialog_->DetachParent(); |
| 637 dialog_ = NULL; | 656 dialog_ = NULL; |
| 638 | 657 |
| 639 DCHECK(!callback_.is_null()); | 658 DCHECK(!callback_.is_null()); |
| 640 | 659 |
| 641 // Notify the |callback_| asynchronously because it may need to destroy | 660 // Notify the |callback_| asynchronously because it may need to destroy |
| 642 // DesktopMediaPicker. | 661 // DesktopMediaPicker. |
| 643 content::BrowserThread::PostTask( | 662 content::BrowserThread::PostTask( |
| 644 content::BrowserThread::UI, FROM_HERE, | 663 content::BrowserThread::UI, FROM_HERE, |
| 645 base::Bind(callback_, source)); | 664 base::Bind(callback_, source)); |
| 646 callback_.Reset(); | 665 callback_.Reset(); |
| 647 } | 666 } |
| 648 | 667 |
| 649 } // namespace | 668 } // namespace |
| 650 | 669 |
| 651 // static | 670 // static |
| 652 scoped_ptr<DesktopMediaPicker> DesktopMediaPicker::Create() { | 671 scoped_ptr<DesktopMediaPicker> DesktopMediaPicker::Create() { |
| 653 return scoped_ptr<DesktopMediaPicker>(new DesktopMediaPickerViews()); | 672 return scoped_ptr<DesktopMediaPicker>(new DesktopMediaPickerViews()); |
| 654 } | 673 } |
| OLD | NEW |