Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/ui/toolbar/media_router_action_controller.h" | 5 #include "chrome/browser/ui/toolbar/media_router_action_controller.h" |
| 6 | 6 |
| 7 #include "chrome/browser/media/router/media_router_factory.h" | 7 #include "chrome/browser/media/router/media_router_factory.h" |
| 8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | 9 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 10 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" | 10 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| 11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 12 | 12 |
| 13 MediaRouterActionController::MediaRouterActionController(Profile* profile) | 13 MediaRouterActionController::MediaRouterActionController(Profile* profile) |
| 14 : MediaRouterActionController( | 14 : MediaRouterActionController( |
| 15 profile, | 15 profile, |
| 16 media_router::MediaRouterFactory::GetApiForBrowserContext(profile), | 16 media_router::MediaRouterFactory::GetApiForBrowserContext(profile), |
| 17 ToolbarActionsModel::Get(profile), | 17 ToolbarActionsModel::Get(profile), |
| 18 ToolbarActionsModel::Get(profile)->component_migration_helper()) {} | 18 ToolbarActionsModel::Get(profile)->component_migration_helper()) { |
| 19 DCHECK(component_action_delegate_); | |
| 20 DCHECK(component_migration_helper_); | |
| 21 } | |
| 19 | 22 |
| 20 MediaRouterActionController::~MediaRouterActionController() { | 23 MediaRouterActionController::~MediaRouterActionController() { |
| 21 UnregisterObserver(); // media_router::IssuesObserver. | 24 UnregisterObserver(); // media_router::IssuesObserver. |
|
mark a. foltz
2016/10/17 18:57:40
Can you add a DCHECK() here that dialogs_shown_ ==
takumif
2016/10/27 23:06:38
Done.
| |
| 22 } | 25 } |
| 23 | 26 |
| 24 void MediaRouterActionController::OnIssueUpdated( | 27 void MediaRouterActionController::OnIssueUpdated( |
| 25 const media_router::Issue* issue) { | 28 const media_router::Issue* issue) { |
| 26 has_issue_ = issue != nullptr; | 29 has_issue_ = issue != nullptr; |
| 27 MaybeAddOrRemoveAction(); | 30 MaybeAddOrRemoveAction(); |
| 28 } | 31 } |
| 29 | 32 |
| 30 void MediaRouterActionController::OnRoutesUpdated( | 33 void MediaRouterActionController::OnRoutesUpdated( |
| 31 const std::vector<media_router::MediaRoute>& routes, | 34 const std::vector<media_router::MediaRoute>& routes, |
| 32 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { | 35 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { |
| 33 has_local_display_route_ = | 36 has_local_display_route_ = |
| 34 std::find_if(routes.begin(), routes.end(), | 37 std::find_if(routes.begin(), routes.end(), |
| 35 [](const media_router::MediaRoute& route) { | 38 [](const media_router::MediaRoute& route) { |
| 36 return route.is_local() && route.for_display(); | 39 return route.is_local() && route.for_display(); |
| 37 }) != routes.end(); | 40 }) != routes.end(); |
| 38 | 41 |
| 39 MaybeAddOrRemoveAction(); | 42 MaybeAddOrRemoveAction(); |
| 40 } | 43 } |
| 41 | 44 |
| 45 void MediaRouterActionController::OnDialogShown() { | |
| 46 dialog_count_++; | |
| 47 MaybeAddOrRemoveAction(); | |
| 48 } | |
| 49 | |
| 50 void MediaRouterActionController::OnDialogHidden() { | |
| 51 DCHECK_NE(dialog_count_, 0u); | |
| 52 if (dialog_count_) | |
| 53 dialog_count_--; | |
| 54 MaybeAddOrRemoveAction(); | |
| 55 } | |
| 56 | |
| 42 MediaRouterActionController::MediaRouterActionController( | 57 MediaRouterActionController::MediaRouterActionController( |
| 43 Profile* profile, | 58 Profile* profile, |
| 44 media_router::MediaRouter* router, | 59 media_router::MediaRouter* router, |
| 45 extensions::ComponentMigrationHelper::ComponentActionDelegate* | 60 extensions::ComponentMigrationHelper::ComponentActionDelegate* |
| 46 component_action_delegate, | 61 component_action_delegate, |
| 47 extensions::ComponentMigrationHelper* component_migration_helper) | 62 extensions::ComponentMigrationHelper* component_migration_helper) |
| 48 : media_router::IssuesObserver(router), | 63 : media_router::IssuesObserver(router), |
| 49 media_router::MediaRoutesObserver(router), | 64 media_router::MediaRoutesObserver(router), |
| 50 profile_(profile), | 65 profile_(profile), |
| 51 component_action_delegate_(component_action_delegate), | 66 component_action_delegate_(component_action_delegate), |
| 52 component_migration_helper_(component_migration_helper) { | 67 component_migration_helper_(component_migration_helper) { |
| 53 DCHECK(profile_); | 68 DCHECK(profile_); |
| 54 DCHECK(component_action_delegate_); | |
| 55 RegisterObserver(); // media_router::IssuesObserver. | 69 RegisterObserver(); // media_router::IssuesObserver. |
| 56 pref_change_registrar_.Init(profile->GetPrefs()); | 70 pref_change_registrar_.Init(profile->GetPrefs()); |
| 57 pref_change_registrar_.Add( | 71 pref_change_registrar_.Add( |
| 58 prefs::kToolbarMigratedComponentActionStatus, | 72 prefs::kToolbarMigratedComponentActionStatus, |
| 59 base::Bind(&MediaRouterActionController::MaybeAddOrRemoveAction, | 73 base::Bind(&MediaRouterActionController::MaybeAddOrRemoveAction, |
| 60 base::Unretained(this))); | 74 base::Unretained(this))); |
| 61 } | 75 } |
| 62 | 76 |
| 63 void MediaRouterActionController::MaybeAddOrRemoveAction() { | 77 void MediaRouterActionController::MaybeAddOrRemoveAction() { |
| 64 if (ShouldEnableAction()) { | 78 if (ShouldEnableAction()) { |
| 65 if (!component_action_delegate_->HasComponentAction( | 79 if (!component_action_delegate_->HasComponentAction( |
| 66 ComponentToolbarActionsFactory::kMediaRouterActionId)) | 80 ComponentToolbarActionsFactory::kMediaRouterActionId)) { |
| 67 component_action_delegate_->AddComponentAction( | 81 component_action_delegate_->AddComponentAction( |
| 68 ComponentToolbarActionsFactory::kMediaRouterActionId); | 82 ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 83 } | |
| 69 } else if (component_action_delegate_->HasComponentAction( | 84 } else if (component_action_delegate_->HasComponentAction( |
| 70 ComponentToolbarActionsFactory::kMediaRouterActionId)) { | 85 ComponentToolbarActionsFactory::kMediaRouterActionId)) { |
| 71 component_action_delegate_->RemoveComponentAction( | 86 component_action_delegate_->RemoveComponentAction( |
| 72 ComponentToolbarActionsFactory::kMediaRouterActionId); | 87 ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 73 } | 88 } |
| 74 } | 89 } |
| 75 | 90 |
| 76 bool MediaRouterActionController::ShouldEnableAction() const { | 91 bool MediaRouterActionController::ShouldEnableAction() const { |
| 77 return has_local_display_route_ || has_issue_ || | 92 return has_local_display_route_ || has_issue_ || dialog_count_ || |
| 78 component_migration_helper_->GetComponentActionPref( | 93 component_migration_helper_->GetComponentActionPref( |
| 79 ComponentToolbarActionsFactory::kMediaRouterActionId); | 94 ComponentToolbarActionsFactory::kMediaRouterActionId); |
| 80 } | 95 } |
| OLD | NEW |