Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/ui/views/website_settings/permissions_bubble_view.h" | 5 #include "chrome/browser/ui/views/website_settings/permissions_bubble_view.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_window.h" | |
| 14 #include "chrome/browser/ui/views/exclusive_access_bubble_views.h" | 15 #include "chrome/browser/ui/views/exclusive_access_bubble_views.h" |
| 15 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 16 #include "chrome/browser/ui/views/frame/top_container_view.h" | |
| 17 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | |
| 18 #include "chrome/browser/ui/views/location_bar/location_icon_view.h" | |
| 19 #include "chrome/browser/ui/views/website_settings/permission_selector_view.h" | 16 #include "chrome/browser/ui/views/website_settings/permission_selector_view.h" |
| 20 #include "chrome/browser/ui/views/website_settings/permission_selector_view_obse rver.h" | 17 #include "chrome/browser/ui/views/website_settings/permission_selector_view_obse rver.h" |
| 21 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" | 18 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" |
| 22 #include "chrome/grit/generated_resources.h" | 19 #include "chrome/grit/generated_resources.h" |
| 23 #include "components/url_formatter/elide_url.h" | 20 #include "components/url_formatter/elide_url.h" |
| 24 #include "grit/components_strings.h" | 21 #include "grit/components_strings.h" |
| 25 #include "ui/accessibility/ax_view_state.h" | 22 #include "ui/accessibility/ax_view_state.h" |
| 26 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 27 #include "ui/base/models/combobox_model.h" | 24 #include "ui/base/models/combobox_model.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 29 #include "ui/gfx/color_palette.h" | 26 #include "ui/gfx/color_palette.h" |
| 30 #include "ui/gfx/paint_vector_icon.h" | 27 #include "ui/gfx/paint_vector_icon.h" |
| 31 #include "ui/gfx/text_constants.h" | 28 #include "ui/gfx/text_constants.h" |
| 32 #include "ui/gfx/vector_icons_public.h" | 29 #include "ui/gfx/vector_icons_public.h" |
| 33 #include "ui/views/bubble/bubble_delegate.h" | 30 #include "ui/views/bubble/bubble_delegate.h" |
| 31 #include "ui/views/bubble/bubble_dialog_delegate.h" | |
| 34 #include "ui/views/bubble/bubble_frame_view.h" | 32 #include "ui/views/bubble/bubble_frame_view.h" |
| 35 #include "ui/views/controls/button/checkbox.h" | 33 #include "ui/views/controls/button/checkbox.h" |
| 36 #include "ui/views/controls/button/menu_button.h" | 34 #include "ui/views/controls/button/menu_button.h" |
| 37 #include "ui/views/controls/button/menu_button_listener.h" | 35 #include "ui/views/controls/button/menu_button_listener.h" |
| 38 #include "ui/views/controls/combobox/combobox.h" | 36 #include "ui/views/controls/combobox/combobox.h" |
| 39 #include "ui/views/controls/combobox/combobox_listener.h" | 37 #include "ui/views/controls/combobox/combobox_listener.h" |
| 40 #include "ui/views/controls/label.h" | 38 #include "ui/views/controls/label.h" |
| 41 #include "ui/views/controls/menu/menu_runner.h" | 39 #include "ui/views/controls/menu/menu_runner.h" |
| 42 #include "ui/views/layout/box_layout.h" | 40 #include "ui/views/layout/box_layout.h" |
| 43 #include "ui/views/layout/grid_layout.h" | 41 #include "ui/views/layout/grid_layout.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 } | 140 } |
| 143 | 141 |
| 144 /////////////////////////////////////////////////////////////////////////////// | 142 /////////////////////////////////////////////////////////////////////////////// |
| 145 // View implementation for the permissions bubble. | 143 // View implementation for the permissions bubble. |
| 146 class PermissionsBubbleDialogDelegateView | 144 class PermissionsBubbleDialogDelegateView |
| 147 : public views::BubbleDialogDelegateView, | 145 : public views::BubbleDialogDelegateView, |
| 148 public PermissionCombobox::Listener { | 146 public PermissionCombobox::Listener { |
| 149 public: | 147 public: |
| 150 PermissionsBubbleDialogDelegateView( | 148 PermissionsBubbleDialogDelegateView( |
| 151 views::View* anchor_view, | 149 views::View* anchor_view, |
| 150 const gfx::Point& anchor_point, | |
| 152 views::BubbleBorder::Arrow anchor_arrow, | 151 views::BubbleBorder::Arrow anchor_arrow, |
| 153 PermissionBubbleViewViews* owner, | 152 PermissionBubbleViewViews* owner, |
| 154 const std::vector<PermissionBubbleRequest*>& requests, | 153 const std::vector<PermissionBubbleRequest*>& requests, |
| 155 const std::vector<bool>& accept_state); | 154 const std::vector<bool>& accept_state); |
| 156 ~PermissionsBubbleDialogDelegateView() override; | 155 ~PermissionsBubbleDialogDelegateView() override; |
| 157 | 156 |
| 158 void CloseBubble(); | 157 void CloseBubble(); |
| 159 void SizeToContents(); | 158 void SizeToContents(); |
| 160 | 159 |
| 161 // BubbleDialogDelegateView: | 160 // BubbleDialogDelegateView: |
| 162 bool ShouldShowCloseButton() const override; | 161 bool ShouldShowCloseButton() const override; |
| 163 const gfx::FontList& GetTitleFontList() const override; | 162 const gfx::FontList& GetTitleFontList() const override; |
| 164 base::string16 GetWindowTitle() const override; | 163 base::string16 GetWindowTitle() const override; |
| 165 void OnWidgetDestroying(views::Widget* widget) override; | 164 void OnWidgetDestroying(views::Widget* widget) override; |
| 166 gfx::Size GetPreferredSize() const override; | 165 gfx::Size GetPreferredSize() const override; |
| 167 void GetAccessibleState(ui::AXViewState* state) override; | 166 void GetAccessibleState(ui::AXViewState* state) override; |
| 168 bool Cancel() override; | 167 bool Cancel() override; |
| 169 bool Accept() override; | 168 bool Accept() override; |
| 170 bool Close() override; | 169 bool Close() override; |
| 171 int GetDialogButtons() const override; | 170 int GetDialogButtons() const override; |
| 172 base::string16 GetDialogButtonLabel(ui::DialogButton button) const override; | 171 base::string16 GetDialogButtonLabel(ui::DialogButton button) const override; |
| 173 | 172 |
| 174 // PermissionCombobox::Listener: | 173 // PermissionCombobox::Listener: |
| 175 void PermissionSelectionChanged(int index, bool allowed) override; | 174 void PermissionSelectionChanged(int index, bool allowed) override; |
| 176 | 175 |
| 177 // Updates the anchor's arrow and view. Also repositions the bubble so it's | 176 // Updates the anchor's arrow and view. Also repositions the bubble so it's |
| 178 // displayed in the correct location. | 177 // displayed in the correct location. |
| 179 void UpdateAnchor(views::View* anchor_view, | 178 void UpdateAnchor(views::View* anchor_view, |
| 179 const gfx::Point& anchor_point, | |
| 180 views::BubbleBorder::Arrow anchor_arrow); | 180 views::BubbleBorder::Arrow anchor_arrow); |
| 181 | 181 |
| 182 private: | 182 private: |
| 183 PermissionBubbleViewViews* owner_; | 183 PermissionBubbleViewViews* owner_; |
| 184 bool multiple_requests_; | 184 bool multiple_requests_; |
| 185 base::string16 display_origin_; | 185 base::string16 display_origin_; |
| 186 std::unique_ptr<PermissionMenuModel> menu_button_model_; | 186 std::unique_ptr<PermissionMenuModel> menu_button_model_; |
| 187 std::vector<PermissionCombobox*> customize_comboboxes_; | 187 std::vector<PermissionCombobox*> customize_comboboxes_; |
| 188 | 188 |
| 189 DISALLOW_COPY_AND_ASSIGN(PermissionsBubbleDialogDelegateView); | 189 DISALLOW_COPY_AND_ASSIGN(PermissionsBubbleDialogDelegateView); |
| 190 }; | 190 }; |
| 191 | 191 |
| 192 PermissionsBubbleDialogDelegateView::PermissionsBubbleDialogDelegateView( | 192 PermissionsBubbleDialogDelegateView::PermissionsBubbleDialogDelegateView( |
| 193 views::View* anchor_view, | 193 views::View* anchor_view, |
| 194 const gfx::Point& anchor_point, | |
| 194 views::BubbleBorder::Arrow anchor_arrow, | 195 views::BubbleBorder::Arrow anchor_arrow, |
| 195 PermissionBubbleViewViews* owner, | 196 PermissionBubbleViewViews* owner, |
| 196 const std::vector<PermissionBubbleRequest*>& requests, | 197 const std::vector<PermissionBubbleRequest*>& requests, |
| 197 const std::vector<bool>& accept_state) | 198 const std::vector<bool>& accept_state) |
| 198 : views::BubbleDialogDelegateView(anchor_view, anchor_arrow), | 199 : views::BubbleDialogDelegateView(anchor_view, anchor_arrow), |
| 199 owner_(owner), | 200 owner_(owner), |
| 200 multiple_requests_(requests.size() > 1) { | 201 multiple_requests_(requests.size() > 1) { |
| 201 DCHECK(!requests.empty()); | 202 DCHECK(!requests.empty()); |
| 202 | 203 |
| 204 if (!anchor_view) | |
| 205 SetAnchorRect(gfx::Rect(anchor_point, gfx::Size())); | |
| 206 | |
| 203 set_close_on_deactivate(false); | 207 set_close_on_deactivate(false); |
| 204 | 208 |
| 205 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, | 209 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, |
| 206 kItemMajorSpacing)); | 210 kItemMajorSpacing)); |
| 207 | 211 |
| 208 display_origin_ = url_formatter::FormatUrlForSecurityDisplay( | 212 display_origin_ = url_formatter::FormatUrlForSecurityDisplay( |
| 209 requests[0]->GetOrigin(), | 213 requests[0]->GetOrigin(), |
| 210 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); | 214 url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); |
| 211 | 215 |
| 212 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 216 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 } | 350 } |
| 347 | 351 |
| 348 void PermissionsBubbleDialogDelegateView::PermissionSelectionChanged( | 352 void PermissionsBubbleDialogDelegateView::PermissionSelectionChanged( |
| 349 int index, | 353 int index, |
| 350 bool allowed) { | 354 bool allowed) { |
| 351 owner_->Toggle(index, allowed); | 355 owner_->Toggle(index, allowed); |
| 352 } | 356 } |
| 353 | 357 |
| 354 void PermissionsBubbleDialogDelegateView::UpdateAnchor( | 358 void PermissionsBubbleDialogDelegateView::UpdateAnchor( |
| 355 views::View* anchor_view, | 359 views::View* anchor_view, |
| 360 const gfx::Point& anchor_point, | |
| 356 views::BubbleBorder::Arrow anchor_arrow) { | 361 views::BubbleBorder::Arrow anchor_arrow) { |
| 357 if (GetAnchorView() == anchor_view && arrow() == anchor_arrow) | |
| 358 return; | |
| 359 | |
| 360 set_arrow(anchor_arrow); | 362 set_arrow(anchor_arrow); |
| 361 | 363 |
| 362 // Update the border in the bubble: will either add or remove the arrow. | 364 // Update the border in the bubble: will either add or remove the arrow. |
| 363 views::BubbleFrameView* frame = | 365 views::BubbleFrameView* frame = |
| 364 views::BubbleDialogDelegateView::GetBubbleFrameView(); | 366 views::BubbleDialogDelegateView::GetBubbleFrameView(); |
| 365 views::BubbleBorder::Arrow adjusted_arrow = anchor_arrow; | 367 views::BubbleBorder::Arrow adjusted_arrow = anchor_arrow; |
| 366 if (base::i18n::IsRTL()) | 368 if (base::i18n::IsRTL()) |
| 367 adjusted_arrow = views::BubbleBorder::horizontal_mirror(adjusted_arrow); | 369 adjusted_arrow = views::BubbleBorder::horizontal_mirror(adjusted_arrow); |
| 368 frame->SetBubbleBorder(std::unique_ptr<views::BubbleBorder>( | 370 frame->SetBubbleBorder(std::unique_ptr<views::BubbleBorder>( |
| 369 new views::BubbleBorder(adjusted_arrow, shadow(), color()))); | 371 new views::BubbleBorder(adjusted_arrow, shadow(), color()))); |
| 370 | 372 |
| 371 // Reposition the bubble based on the updated arrow and view. | 373 // Reposition the bubble based on the updated arrow and view. |
| 372 SetAnchorView(anchor_view); | 374 if (anchor_view) |
| 375 SetAnchorView(anchor_view); | |
| 376 else | |
| 377 SetAnchorRect(gfx::Rect(anchor_point, gfx::Size())); | |
| 373 } | 378 } |
| 374 | 379 |
| 375 ////////////////////////////////////////////////////////////////////////////// | 380 ////////////////////////////////////////////////////////////////////////////// |
| 376 // PermissionBubbleViewViews | 381 // PermissionBubbleViewViews |
| 377 | 382 |
| 378 PermissionBubbleViewViews::PermissionBubbleViewViews(Browser* browser) | 383 PermissionBubbleViewViews::PermissionBubbleViewViews(Browser* browser) |
| 379 : browser_(browser), | 384 : browser_(browser), |
| 380 delegate_(nullptr), | 385 delegate_(nullptr), |
| 381 bubble_delegate_(nullptr) { | 386 bubble_delegate_(nullptr), |
| 387 anchor_delegate_(CreateAnchorDelegate(browser)) { | |
| 382 DCHECK(browser); | 388 DCHECK(browser); |
| 389 DCHECK(browser->window()); | |
| 383 } | 390 } |
| 384 | 391 |
| 385 PermissionBubbleViewViews::~PermissionBubbleViewViews() { | 392 PermissionBubbleViewViews::~PermissionBubbleViewViews() { |
| 386 } | 393 } |
| 387 | 394 |
| 388 // static | |
| 389 std::unique_ptr<PermissionBubbleView> PermissionBubbleView::Create( | |
| 390 Browser* browser) { | |
| 391 return base::WrapUnique(new PermissionBubbleViewViews(browser)); | |
| 392 } | |
| 393 | |
| 394 views::View* PermissionBubbleViewViews::GetAnchorView() { | |
| 395 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser_); | |
| 396 | |
| 397 if (browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR)) | |
| 398 return browser_view->GetLocationBarView()->location_icon_view(); | |
| 399 | |
| 400 if (browser_view->IsFullscreenBubbleVisible()) | |
| 401 return browser_view->exclusive_access_bubble()->GetView(); | |
| 402 | |
| 403 return browser_view->top_container(); | |
| 404 } | |
| 405 | |
| 406 views::BubbleBorder::Arrow PermissionBubbleViewViews::GetAnchorArrow() { | 395 views::BubbleBorder::Arrow PermissionBubbleViewViews::GetAnchorArrow() { |
| 407 if (browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR)) | 396 if (browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR)) |
| 408 return views::BubbleBorder::TOP_LEFT; | 397 return views::BubbleBorder::TOP_LEFT; |
| 409 return views::BubbleBorder::NONE; | 398 return views::BubbleBorder::NONE; |
| 410 } | 399 } |
| 411 | 400 |
| 412 void PermissionBubbleViewViews::SetDelegate(Delegate* delegate) { | 401 void PermissionBubbleViewViews::SetDelegate(Delegate* delegate) { |
| 413 delegate_ = delegate; | 402 delegate_ = delegate; |
| 414 } | 403 } |
| 415 | 404 |
| 416 void PermissionBubbleViewViews::Show( | 405 void PermissionBubbleViewViews::Show( |
| 417 const std::vector<PermissionBubbleRequest*>& requests, | 406 const std::vector<PermissionBubbleRequest*>& requests, |
| 418 const std::vector<bool>& values) { | 407 const std::vector<bool>& values) { |
| 419 if (bubble_delegate_) | 408 if (bubble_delegate_) |
| 420 bubble_delegate_->CloseBubble(); | 409 bubble_delegate_->CloseBubble(); |
| 421 | 410 |
| 422 bubble_delegate_ = new PermissionsBubbleDialogDelegateView( | 411 bubble_delegate_ = new PermissionsBubbleDialogDelegateView( |
|
tapted
2016/05/11 07:06:19
(so
bubble_delegate_ = new PermissionsBubbleDialo
Elly Fong-Jones
2016/05/11 19:16:38
Aha, I think I finally understand what is meant he
| |
| 423 GetAnchorView(), GetAnchorArrow(), this, requests, values); | 412 anchor_delegate_->GetAnchorView(), anchor_delegate_->GetAnchorPoint(), |
| 413 GetAnchorArrow(), this, requests, values); | |
| 424 | 414 |
| 425 // Set |parent_window| because some valid anchors can become hidden. | 415 // Set |parent_window| because some valid anchors can become hidden. |
| 426 views::Widget* widget = views::Widget::GetWidgetForNativeWindow( | 416 bubble_delegate_->set_parent_window( |
| 427 browser_->window()->GetNativeWindow()); | 417 platform_util::GetViewForWindow(browser_->window()->GetNativeWindow())); |
|
tapted
2016/05/11 07:06:19
I think set_parent_window needs to be called in Up
Elly Fong-Jones
2016/05/11 19:16:37
Just calling set_parent_window() does not seem to
| |
| 428 bubble_delegate_->set_parent_window(widget->GetNativeView()); | |
| 429 | 418 |
| 430 views::BubbleDialogDelegateView::CreateBubble(bubble_delegate_)->Show(); | 419 views::BubbleDialogDelegateView::CreateBubble(bubble_delegate_)->Show(); |
| 431 bubble_delegate_->SizeToContents(); | 420 bubble_delegate_->SizeToContents(); |
| 432 } | 421 } |
| 433 | 422 |
| 434 bool PermissionBubbleViewViews::CanAcceptRequestUpdate() { | 423 bool PermissionBubbleViewViews::CanAcceptRequestUpdate() { |
| 435 return !(bubble_delegate_ && bubble_delegate_->IsMouseHovered()); | 424 return !(bubble_delegate_ && bubble_delegate_->IsMouseHovered()); |
| 436 } | 425 } |
| 437 | 426 |
| 438 void PermissionBubbleViewViews::Hide() { | 427 void PermissionBubbleViewViews::Hide() { |
| 439 if (bubble_delegate_) { | 428 if (bubble_delegate_) { |
| 440 bubble_delegate_->CloseBubble(); | 429 bubble_delegate_->CloseBubble(); |
| 441 bubble_delegate_ = nullptr; | 430 bubble_delegate_ = nullptr; |
| 442 } | 431 } |
| 443 } | 432 } |
| 444 | 433 |
| 445 bool PermissionBubbleViewViews::IsVisible() { | 434 bool PermissionBubbleViewViews::IsVisible() { |
| 446 return bubble_delegate_ != nullptr; | 435 return bubble_delegate_ != nullptr; |
| 447 } | 436 } |
| 448 | 437 |
| 449 void PermissionBubbleViewViews::UpdateAnchorPosition() { | 438 void PermissionBubbleViewViews::UpdateAnchorPosition() { |
| 450 if (IsVisible()) | 439 if (IsVisible()) { |
| 451 bubble_delegate_->UpdateAnchor(GetAnchorView(), GetAnchorArrow()); | 440 bubble_delegate_->UpdateAnchor(anchor_delegate_->GetAnchorView(), |
| 441 anchor_delegate_->GetAnchorPoint(), | |
| 442 GetAnchorArrow()); | |
| 443 } | |
| 452 } | 444 } |
| 453 | 445 |
| 454 gfx::NativeWindow PermissionBubbleViewViews::GetNativeWindow() { | 446 gfx::NativeWindow PermissionBubbleViewViews::GetNativeWindow() { |
| 455 if (bubble_delegate_ && bubble_delegate_->GetWidget()) | 447 if (bubble_delegate_ && bubble_delegate_->GetWidget()) |
| 456 return bubble_delegate_->GetWidget()->GetNativeWindow(); | 448 return bubble_delegate_->GetWidget()->GetNativeWindow(); |
| 457 return nullptr; | 449 return nullptr; |
| 458 } | 450 } |
| 459 | 451 |
| 460 void PermissionBubbleViewViews::Closing() { | 452 void PermissionBubbleViewViews::Closing() { |
| 461 if (bubble_delegate_) | 453 if (bubble_delegate_) |
| 462 bubble_delegate_ = nullptr; | 454 bubble_delegate_ = nullptr; |
| 463 if (delegate_) | 455 if (delegate_) |
| 464 delegate_->Closing(); | 456 delegate_->Closing(); |
| 465 } | 457 } |
| 466 | 458 |
| 467 void PermissionBubbleViewViews::Toggle(int index, bool value) { | 459 void PermissionBubbleViewViews::Toggle(int index, bool value) { |
| 468 if (delegate_) | 460 if (delegate_) |
| 469 delegate_->ToggleAccept(index, value); | 461 delegate_->ToggleAccept(index, value); |
| 470 } | 462 } |
| 471 | 463 |
| 472 void PermissionBubbleViewViews::Accept() { | 464 void PermissionBubbleViewViews::Accept() { |
| 473 if (delegate_) | 465 if (delegate_) |
| 474 delegate_->Accept(); | 466 delegate_->Accept(); |
| 475 } | 467 } |
| 476 | 468 |
| 477 void PermissionBubbleViewViews::Deny() { | 469 void PermissionBubbleViewViews::Deny() { |
| 478 if (delegate_) | 470 if (delegate_) |
| 479 delegate_->Deny(); | 471 delegate_->Deny(); |
| 480 } | 472 } |
| OLD | NEW |