Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: ui/arc/notification/arc_custom_notification_view.cc

Issue 2834763003: [Notifications] Fix crashes. (Closed)
Patch Set: Address comments. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/arc/notification/arc_custom_notification_view.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "ui/arc/notification/arc_custom_notification_view.h" 5 #include "ui/arc/notification/arc_custom_notification_view.h"
6 6
7 #include "ash/wm/window_util.h" 7 #include "ash/wm/window_util.h"
8 #include "base/auto_reset.h" 8 #include "base/auto_reset.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "components/exo/notification_surface.h" 10 #include "components/exo/notification_surface.h"
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 settings_button_ = new ControlButton(this); 287 settings_button_ = new ControlButton(this);
288 settings_button_->SetImage(views::CustomButton::STATE_NORMAL, 288 settings_button_->SetImage(views::CustomButton::STATE_NORMAL,
289 message_center::GetSettingsIcon()); 289 message_center::GetSettingsIcon());
290 settings_button_->SetAccessibleName(l10n_util::GetStringUTF16( 290 settings_button_->SetAccessibleName(l10n_util::GetStringUTF16(
291 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME)); 291 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME));
292 settings_button_->SetTooltipText(l10n_util::GetStringUTF16( 292 settings_button_->SetTooltipText(l10n_util::GetStringUTF16(
293 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME)); 293 IDS_MESSAGE_NOTIFICATION_SETTINGS_BUTTON_ACCESSIBLE_NAME));
294 control_buttons_view_->AddChildView(settings_button_); 294 control_buttons_view_->AddChildView(settings_button_);
295 } 295 }
296 296
297 void ArcCustomNotificationView::CreateFloatingControlButtons() { 297 void ArcCustomNotificationView::MaybeCreateFloatingControlButtons() {
298 // Floating close button is a transient child of |surface_| and also part 298 // Floating close button is a transient child of |surface_| and also part
299 // of the hosting widget's focus chain. It could only be created when both 299 // of the hosting widget's focus chain. It could only be created when both
300 // are present. 300 // are present. Further, if we are being destroyed (|item_| is null), don't
301 if (!surface_ || !GetWidget()) 301 // create the control buttons.
302 if (!surface_ || !GetWidget() || !item_)
302 return; 303 return;
303 304
304 // Creates the control_buttons_view_, which collects all control buttons into 305 // Creates the control_buttons_view_, which collects all control buttons into
305 // a horizontal box. 306 // a horizontal box.
306 control_buttons_view_ = new views::View(); 307 control_buttons_view_ = new views::View();
307 control_buttons_view_->SetLayoutManager( 308 control_buttons_view_->SetLayoutManager(
308 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); 309 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
309 310
310 if (item_ && item_->IsOpeningSettingsSupported()) 311 if (item_->IsOpeningSettingsSupported())
311 CreateSettingsButton(); 312 CreateSettingsButton();
312 if (item_ && !item_->pinned()) 313 if (!item_->pinned())
313 CreateCloseButton(); 314 CreateCloseButton();
314 315
315 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); 316 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
316 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 317 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
317 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 318 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
318 params.parent = surface_->window(); 319 params.parent = surface_->window();
319 320
320 floating_control_buttons_widget_.reset(new views::Widget); 321 floating_control_buttons_widget_.reset(new views::Widget);
321 floating_control_buttons_widget_->Init(params); 322 floating_control_buttons_widget_->Init(params);
322 floating_control_buttons_widget_->SetContentsView(control_buttons_view_); 323 floating_control_buttons_widget_->SetContentsView(control_buttons_view_);
(...skipping 20 matching lines...) Expand all
343 surface_->window()->RemoveObserver(this); 344 surface_->window()->RemoveObserver(this);
344 surface_->window()->RemovePreTargetHandler(event_forwarder_.get()); 345 surface_->window()->RemovePreTargetHandler(event_forwarder_.get());
345 } 346 }
346 347
347 surface_ = surface; 348 surface_ = surface;
348 349
349 if (surface_ && surface_->window()) { 350 if (surface_ && surface_->window()) {
350 surface_->window()->AddObserver(this); 351 surface_->window()->AddObserver(this);
351 surface_->window()->AddPreTargetHandler(event_forwarder_.get()); 352 surface_->window()->AddPreTargetHandler(event_forwarder_.get());
352 353
353 CreateFloatingControlButtons(); 354 MaybeCreateFloatingControlButtons();
354 355
355 if (GetWidget()) 356 if (GetWidget())
356 AttachSurface(); 357 AttachSurface();
357 } 358 }
358 } 359 }
359 360
360 void ArcCustomNotificationView::UpdatePreferredSize() { 361 void ArcCustomNotificationView::UpdatePreferredSize() {
361 gfx::Size preferred_size = 362 gfx::Size preferred_size =
362 surface_ ? surface_->GetSize() : item_ ? item_->snapshot().size() 363 surface_ ? surface_->GetSize() : item_ ? item_->snapshot().size()
363 : gfx::Size(); 364 : gfx::Size();
364 if (preferred_size.IsEmpty()) 365 if (preferred_size.IsEmpty())
365 return; 366 return;
366 367
367 if (preferred_size.width() != message_center::kNotificationWidth) { 368 if (preferred_size.width() != message_center::kNotificationWidth) {
368 const float scale = static_cast<float>(message_center::kNotificationWidth) / 369 const float scale = static_cast<float>(message_center::kNotificationWidth) /
369 preferred_size.width(); 370 preferred_size.width();
370 preferred_size.SetSize(message_center::kNotificationWidth, 371 preferred_size.SetSize(message_center::kNotificationWidth,
371 preferred_size.height() * scale); 372 preferred_size.height() * scale);
372 } 373 }
373 374
374 SetPreferredSize(preferred_size); 375 SetPreferredSize(preferred_size);
375 } 376 }
376 377
377 void ArcCustomNotificationView::UpdateControlButtonsVisibility() { 378 void ArcCustomNotificationView::UpdateControlButtonsVisibility() {
378 if (!surface_) 379 if (!surface_)
379 return; 380 return;
380 381
382 // TODO(edcourtney, yhanada): Creating the floating control widget here is not
383 // correct. This function may be called during the destruction of
384 // |floating_control_buttons_widget_|. This can lead to memory corruption.
385 // Rather than creating it here, we should fix the behaviour of OnMouseExited
386 // and OnMouseEntered for ARC notifications in MessageCenterView. See
387 // crbug.com/714587 and crbug.com/709862.
381 if (!floating_control_buttons_widget_) { 388 if (!floating_control_buttons_widget_) {
382 if (GetWidget()) 389 // This may update |floating_control_buttons_widget_|.
383 CreateFloatingControlButtons(); 390 MaybeCreateFloatingControlButtons();
384 else 391 if (!floating_control_buttons_widget_)
385 return; 392 return;
386 } 393 }
387 394
388 const bool target_visiblity = 395 const bool target_visiblity =
389 IsMouseHovered() || (close_button_ && close_button_->HasFocus()) || 396 IsMouseHovered() || (close_button_ && close_button_->HasFocus()) ||
390 (settings_button_ && settings_button_->HasFocus()); 397 (settings_button_ && settings_button_->HasFocus());
391 if (target_visiblity == floating_control_buttons_widget_->IsVisible()) 398 if (target_visiblity == floating_control_buttons_widget_->IsVisible())
392 return; 399 return;
393 400
394 if (target_visiblity) 401 if (target_visiblity)
395 floating_control_buttons_widget_->Show(); 402 floating_control_buttons_widget_->Show();
396 else 403 else
397 floating_control_buttons_widget_->Hide(); 404 floating_control_buttons_widget_->Hide();
398 } 405 }
399 406
400 void ArcCustomNotificationView::UpdatePinnedState() { 407 void ArcCustomNotificationView::UpdatePinnedState() {
401 DCHECK(item_); 408 if (!item_)
409 return;
402 410
403 if (item_->pinned() && close_button_) { 411 if (item_->pinned() && close_button_) {
404 control_buttons_view_->RemoveChildView(close_button_.get()); 412 control_buttons_view_->RemoveChildView(close_button_.get());
405 close_button_.reset(); 413 close_button_.reset();
406 Layout(); 414 Layout();
407 } else if (!item_->pinned() && !close_button_) { 415 } else if (!item_->pinned() && !close_button_) {
408 CreateCloseButton(); 416 CreateCloseButton();
409 Layout(); 417 Layout();
410 } 418 }
411 } 419 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 453
446 void ArcCustomNotificationView::StartControlButtonsColorAnimation() { 454 void ArcCustomNotificationView::StartControlButtonsColorAnimation() {
447 if (control_button_color_animation_) 455 if (control_button_color_animation_)
448 control_button_color_animation_->End(); 456 control_button_color_animation_->End();
449 control_button_color_animation_.reset(new gfx::LinearAnimation(this)); 457 control_button_color_animation_.reset(new gfx::LinearAnimation(this));
450 control_button_color_animation_->SetDuration(kBackgroundColorChangeDuration); 458 control_button_color_animation_->SetDuration(kBackgroundColorChangeDuration);
451 control_button_color_animation_->Start(); 459 control_button_color_animation_->Start();
452 } 460 }
453 461
454 bool ArcCustomNotificationView::ShouldUpdateControlButtonsColor() const { 462 bool ArcCustomNotificationView::ShouldUpdateControlButtonsColor() const {
455 DCHECK(item_); 463 // Don't update the control button color when we are about to be destroyed.
464 if (!item_)
465 return false;
456 466
457 if (settings_button_ && 467 if (settings_button_ &&
458 settings_button_->background()->get_color() != 468 settings_button_->background()->get_color() !=
459 GetControlButtonBackgroundColor(item_->shown_contents())) 469 GetControlButtonBackgroundColor(item_->shown_contents()))
460 return true; 470 return true;
461 if (close_button_ && 471 if (close_button_ &&
462 close_button_->background()->get_color() != 472 close_button_->background()->get_color() !=
463 GetControlButtonBackgroundColor(item_->shown_contents())) 473 GetControlButtonBackgroundColor(item_->shown_contents()))
464 return true; 474 return true;
465 return false; 475 return false;
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 } 728 }
719 if (close_button_) { 729 if (close_button_) {
720 close_button_->set_background( 730 close_button_->set_background(
721 views::Background::CreateSolidBackground(current_color)); 731 views::Background::CreateSolidBackground(current_color));
722 close_button_->SchedulePaint(); 732 close_button_->SchedulePaint();
723 } 733 }
724 } 734 }
725 } 735 }
726 736
727 } // namespace arc 737 } // namespace arc
OLDNEW
« no previous file with comments | « ui/arc/notification/arc_custom_notification_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698