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

Unified Diff: chrome/browser/ui/toolbar/media_router_action_controller.cc

Issue 2294973002: Create MediaRouterActionController and MediaRouterUIService (Closed)
Patch Set: Add MediaRouterUIService (KeyedService) Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
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..b0aa357b6a37f7d1e14c239f29bcbfdac6034b1b
--- /dev/null
+++ b/chrome/browser/ui/toolbar/media_router_action_controller.cc
@@ -0,0 +1,84 @@
+// 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"
+#include "chrome/browser/media/router/media_router_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#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"
+
+MediaRouterActionController::MediaRouterActionController(Profile* profile)
+ : MediaRouterActionController(
+ profile,
+ media_router::MediaRouterFactory::GetApiForBrowserContext(profile),
+ ToolbarActionsModel::Get(profile),
+ ToolbarActionsModel::Get(profile)->component_migration_helper()) {}
+
+MediaRouterActionController::MediaRouterActionController(
+ Profile* profile,
+ media_router::MediaRouter* router,
+ extensions::ComponentMigrationHelper::ComponentActionDelegate*
imcheng 2016/09/13 23:13:10 nit: I think we should extract ComponentActionDele
takumif 2016/09/14 04:00:52 I'm not sure where that'd go. ComponentMigrationHe
+ 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());
mark a. foltz 2016/09/13 18:23:05 I wonder if changing the pref in incognito affects
takumif 2016/09/14 04:00:52 Pref changes made while in incognito do persist. I
+ pref_change_registrar_.Add(
+ prefs::kToolbarMigratedComponentActionStatus,
+ base::Bind(&MediaRouterActionController::MaybeAddOrRemoveAction,
+ base::Unretained(this)));
+}
+
+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();
+}
+
+void MediaRouterActionController::MaybeAddOrRemoveAction() {
+ const std::string& action_id =
+ ComponentToolbarActionsFactory::kMediaRouterActionId;
mark a. foltz 2016/09/13 18:23:05 Can you write using ComponentToolbarActionsFacto
takumif 2016/09/14 04:00:52 using ComponentToolbarActionsFactory::kMediaRouter
+
+ if (IsActionEnabled()) {
+ if (!component_action_delegate_->HasComponentAction(action_id))
+ component_action_delegate_->AddComponentAction(action_id);
+ } else {
+ if (component_action_delegate_->HasComponentAction(action_id))
+ component_action_delegate_->RemoveComponentAction(action_id);
+ }
+}
+
+bool MediaRouterActionController::IsActionEnabled() {
imcheng 2016/09/13 23:13:10 I think this method makes more sense if it is name
takumif 2016/09/14 04:00:52 Renamed.
+ return component_migration_helper_->GetComponentActionPref(
imcheng 2016/09/13 23:13:10 nit: as a small optimization, we can reorder the s
takumif 2016/09/14 04:00:52 Done.
+ ComponentToolbarActionsFactory::kMediaRouterActionId) ||
+ has_local_display_route_ || has_issue_;
+}

Powered by Google App Engine
This is Rietveld 408576698