Chromium Code Reviews| Index: chrome/browser/ui/toolbar/media_router_action_controller.cc |
| diff --git a/chrome/browser/ui/toolbar/media_router_action_controller.cc b/chrome/browser/ui/toolbar/media_router_action_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4a714945a82dae5a2ad22b4d8af81fbef9d2b62e |
| --- /dev/null |
| +++ b/chrome/browser/ui/toolbar/media_router_action_controller.cc |
| @@ -0,0 +1,87 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/toolbar/media_router_action_controller.h" |
| + |
| +#include "chrome/browser/extensions/component_migration_helper.h" |
|
msw
2016/09/16 22:22:25
nit: remove (redundant with header)
takumif
2016/09/17 00:24:30
Done.
|
| +#include "chrome/browser/media/router/media_router_factory.h" |
| +#include "chrome/browser/profiles/profile.h" |
|
msw
2016/09/16 22:22:25
nit: remove if kept in header
takumif
2016/09/17 00:24:30
Done.
|
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| +#include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| +#include "chrome/common/pref_names.h" |
| + |
| +namespace { |
| + |
| +const char (&kMediaRouterActionId)[] = |
|
msw
2016/09/16 22:22:25
nit/q: why not just inline |ComponentToolbarAction
takumif
2016/09/17 00:24:30
This is just to give it a shorter name. Is there a
msw
2016/09/17 00:37:11
I'd just use the full name everywhere, or re-use /
takumif
2016/09/17 00:50:39
Using the full name.
|
| + ComponentToolbarActionsFactory::kMediaRouterActionId; |
| + |
| +} |
|
msw
2016/09/16 22:22:25
nit: "} // namespace"
takumif
2016/09/17 00:24:30
Done.
|
| + |
| +MediaRouterActionController::MediaRouterActionController(Profile* profile) |
| + : MediaRouterActionController( |
| + profile, |
| + media_router::MediaRouterFactory::GetApiForBrowserContext(profile), |
| + ToolbarActionsModel::Get(profile), |
| + ToolbarActionsModel::Get(profile)->component_migration_helper()) {} |
| + |
| +MediaRouterActionController::~MediaRouterActionController() { |
| + UnregisterObserver(); // media_router::IssuesObserver. |
| +} |
| + |
| +void MediaRouterActionController::OnIssueUpdated( |
| + const media_router::Issue* issue) { |
| + has_issue_ = issue != nullptr; |
| + MaybeAddOrRemoveAction(); |
| +} |
| + |
| +void MediaRouterActionController::OnRoutesUpdated( |
| + const std::vector<media_router::MediaRoute>& routes, |
| + const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { |
| + has_local_display_route_ = |
| + std::find_if(routes.begin(), routes.end(), |
| + [](const media_router::MediaRoute& route) { |
| + return route.is_local() && route.for_display(); |
| + }) != routes.end(); |
| + |
| + MaybeAddOrRemoveAction(); |
| +} |
| + |
| +MediaRouterActionController::MediaRouterActionController( |
| + Profile* profile, |
| + media_router::MediaRouter* router, |
| + extensions::ComponentMigrationHelper::ComponentActionDelegate* |
| + component_action_delegate, |
| + extensions::ComponentMigrationHelper* component_migration_helper) |
| + : media_router::IssuesObserver(router), |
| + media_router::MediaRoutesObserver(router), |
| + profile_(profile), |
| + component_action_delegate_(component_action_delegate), |
| + component_migration_helper_(component_migration_helper), |
| + has_issue_(false), |
| + has_local_display_route_(false) { |
| + DCHECK(profile_); |
| + DCHECK(component_action_delegate_); |
| + RegisterObserver(); // media_router::IssuesObserver. |
| + pref_change_registrar_.Init(profile->GetPrefs()); |
| + pref_change_registrar_.Add( |
| + prefs::kToolbarMigratedComponentActionStatus, |
| + base::Bind(&MediaRouterActionController::MaybeAddOrRemoveAction, |
| + base::Unretained(this))); |
| +} |
| + |
| +void MediaRouterActionController::MaybeAddOrRemoveAction() { |
| + if (ShouldEnableAction()) { |
| + if (!component_action_delegate_->HasComponentAction(kMediaRouterActionId)) |
| + component_action_delegate_->AddComponentAction(kMediaRouterActionId); |
| + } else if (component_action_delegate_ |
| + ->HasComponentAction(kMediaRouterActionId)) { |
|
Devlin
2016/09/16 22:37:32
was this git cl format'd?
takumif
2016/09/17 00:24:30
No. Will use git cl format.
|
| + component_action_delegate_->RemoveComponentAction(kMediaRouterActionId); |
| + } |
| +} |
| + |
| +bool MediaRouterActionController::ShouldEnableAction() const { |
| + return has_local_display_route_ || has_issue_ || |
| + component_migration_helper_->GetComponentActionPref(kMediaRouterActionId); |
| +} |