Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/toolbar/media_router_action_controller.h" | |
| 6 | |
| 7 #include "chrome/browser/extensions/component_migration_helper.h" | |
| 8 #include "chrome/browser/media/router/media_router_factory.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/ui/browser.h" | |
| 11 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | |
| 12 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 const char (&kMediaRouterActionId)[] = | |
| 18 ComponentToolbarActionsFactory::kMediaRouterActionId; | |
| 19 | |
| 20 } | |
| 21 | |
| 22 MediaRouterActionController::MediaRouterActionController(Profile* profile) | |
| 23 : MediaRouterActionController( | |
| 24 profile, | |
| 25 media_router::MediaRouterFactory::GetApiForBrowserContext(profile), | |
| 26 ToolbarActionsModel::Get(profile), | |
| 27 ToolbarActionsModel::Get(profile)->component_migration_helper()) {} | |
| 28 | |
| 29 MediaRouterActionController::~MediaRouterActionController() { | |
| 30 UnregisterObserver(); // media_router::IssuesObserver. | |
| 31 } | |
| 32 | |
| 33 void MediaRouterActionController::OnIssueUpdated( | |
| 34 const media_router::Issue* issue) { | |
| 35 has_issue_ = issue != nullptr; | |
| 36 MaybeAddOrRemoveAction(); | |
| 37 } | |
| 38 | |
| 39 void MediaRouterActionController::OnRoutesUpdated( | |
| 40 const std::vector<media_router::MediaRoute>& routes, | |
| 41 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { | |
| 42 has_local_display_route_ = | |
| 43 std::find_if(routes.begin(), routes.end(), | |
| 44 [](const media_router::MediaRoute& route) { | |
| 45 return route.is_local() && route.for_display(); | |
| 46 }) != routes.end(); | |
| 47 | |
| 48 MaybeAddOrRemoveAction(); | |
| 49 } | |
| 50 | |
| 51 MediaRouterActionController::MediaRouterActionController( | |
| 52 Profile* profile, | |
| 53 media_router::MediaRouter* router, | |
| 54 extensions::ComponentMigrationHelper::ComponentActionDelegate* | |
| 55 component_action_delegate, | |
| 56 extensions::ComponentMigrationHelper* component_migration_helper) | |
| 57 : media_router::IssuesObserver(router), | |
| 58 media_router::MediaRoutesObserver(router), | |
| 59 profile_(profile), | |
| 60 component_action_delegate_(component_action_delegate), | |
| 61 component_migration_helper_(component_migration_helper), | |
| 62 has_issue_(false), | |
| 63 has_local_display_route_(false) { | |
| 64 DCHECK(profile_); | |
| 65 DCHECK(component_action_delegate_); | |
| 66 RegisterObserver(); // media_router::IssuesObserver. | |
| 67 pref_change_registrar_.Init(profile->GetPrefs()); | |
| 68 pref_change_registrar_.Add( | |
| 69 prefs::kToolbarMigratedComponentActionStatus, | |
| 70 base::Bind(&MediaRouterActionController::MaybeAddOrRemoveAction, | |
| 71 base::Unretained(this))); | |
| 72 } | |
| 73 | |
| 74 void MediaRouterActionController::MaybeAddOrRemoveAction() { | |
| 75 if (ShouldEnableAction()) { | |
| 76 if (!component_action_delegate_->HasComponentAction(kMediaRouterActionId)) | |
| 77 component_action_delegate_->AddComponentAction(kMediaRouterActionId); | |
| 78 } else { | |
| 79 if (component_action_delegate_->HasComponentAction(kMediaRouterActionId)) | |
|
Devlin
2016/09/15 21:08:42
nit:
else {
if (x) {
}
}
is identical to
els
takumif
2016/09/16 21:04:05
Fixed.
| |
| 80 component_action_delegate_->RemoveComponentAction(kMediaRouterActionId); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 bool MediaRouterActionController::ShouldEnableAction() const { | |
| 85 return has_local_display_route_ || has_issue_ || | |
| 86 component_migration_helper_->GetComponentActionPref(kMediaRouterActionId); | |
| 87 } | |
| OLD | NEW |