| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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/webui/options2/chromeos/display_options_handler.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "ash/monitor/monitor_controller.h" |
| 10 #include "ash/shell.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/json/json_value_converter.h" |
| 13 #include "base/values.h" |
| 14 #include "chromeos/monitor/output_configurator.h" |
| 15 #include "content/public/browser/web_ui.h" |
| 16 #include "grit/generated_resources.h" |
| 17 #include "ui/aura/env.h" |
| 18 #include "ui/aura/monitor_manager.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/gfx/display.h" |
| 21 #include "ui/gfx/rect.h" |
| 22 |
| 23 using ash::internal::MonitorController; |
| 24 |
| 25 namespace chromeos { |
| 26 namespace options2 { |
| 27 |
| 28 DisplayOptionsHandler::DisplayOptionsHandler() { |
| 29 aura::Env::GetInstance()->monitor_manager()->AddObserver(this); |
| 30 } |
| 31 |
| 32 DisplayOptionsHandler::~DisplayOptionsHandler() { |
| 33 aura::Env::GetInstance()->monitor_manager()->RemoveObserver(this); |
| 34 } |
| 35 |
| 36 void DisplayOptionsHandler::GetLocalizedValues( |
| 37 DictionaryValue* localized_strings) { |
| 38 DCHECK(localized_strings); |
| 39 RegisterTitle(localized_strings, "displayOptionsPage", |
| 40 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_TAB_TITLE); |
| 41 localized_strings->SetString("start-mirroring", l10n_util::GetStringUTF16( |
| 42 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_START_MIRRORING)); |
| 43 localized_strings->SetString("stop-mirroring", l10n_util::GetStringUTF16( |
| 44 IDS_OPTIONS_SETTINGS_DISPLAY_OPTIONS_STOP_MIRRORING)); |
| 45 } |
| 46 |
| 47 void DisplayOptionsHandler::InitializeHandler() { |
| 48 DCHECK(web_ui()); |
| 49 UpdateDisplaySectionVisibility(); |
| 50 } |
| 51 |
| 52 void DisplayOptionsHandler::RegisterMessages() { |
| 53 web_ui()->RegisterMessageCallback( |
| 54 "getDisplayInfo", |
| 55 base::Bind(&DisplayOptionsHandler::HandleDisplayInfo, |
| 56 base::Unretained(this))); |
| 57 web_ui()->RegisterMessageCallback( |
| 58 "setMirroring", |
| 59 base::Bind(&DisplayOptionsHandler::HandleMirroring, |
| 60 base::Unretained(this))); |
| 61 web_ui()->RegisterMessageCallback( |
| 62 "setDisplayLayout", |
| 63 base::Bind(&DisplayOptionsHandler::HandleDisplayLayout, |
| 64 base::Unretained(this))); |
| 65 } |
| 66 |
| 67 void DisplayOptionsHandler::OnDisplayBoundsChanged( |
| 68 const gfx::Display& display) { |
| 69 SendDisplayInfo(); |
| 70 } |
| 71 |
| 72 void DisplayOptionsHandler::OnDisplayAdded(const gfx::Display& new_display) { |
| 73 UpdateDisplaySectionVisibility(); |
| 74 SendDisplayInfo(); |
| 75 } |
| 76 |
| 77 void DisplayOptionsHandler::OnDisplayRemoved(const gfx::Display& old_display) { |
| 78 UpdateDisplaySectionVisibility(); |
| 79 SendDisplayInfo(); |
| 80 } |
| 81 |
| 82 void DisplayOptionsHandler::UpdateDisplaySectionVisibility() { |
| 83 MonitorController* monitor_controller = |
| 84 ash::Shell::GetInstance()->monitor_controller(); |
| 85 aura::MonitorManager* monitor_manager = |
| 86 aura::Env::GetInstance()->monitor_manager(); |
| 87 chromeos::State output_state = |
| 88 ash::Shell::GetInstance()->output_configurator()->output_state(); |
| 89 if (monitor_controller->IsExtendedDesktopEnabled() && |
| 90 monitor_manager->GetNumDisplays() > 1 && |
| 91 output_state != chromeos::STATE_INVALID && |
| 92 output_state != chromeos::STATE_HEADLESS && |
| 93 output_state != chromeos::STATE_SINGLE) { |
| 94 web_ui()->CallJavascriptFunction( |
| 95 "options.BrowserOptions.showDisplayOptions"); |
| 96 } else { |
| 97 web_ui()->CallJavascriptFunction( |
| 98 "options.BrowserOptions.hideDisplayOptions"); |
| 99 } |
| 100 } |
| 101 |
| 102 void DisplayOptionsHandler::SendDisplayInfo() { |
| 103 aura::MonitorManager* monitor_manager = |
| 104 aura::Env::GetInstance()->monitor_manager(); |
| 105 chromeos::OutputConfigurator* output_configurator = |
| 106 ash::Shell::GetInstance()->output_configurator(); |
| 107 base::FundamentalValue mirroring( |
| 108 output_configurator->output_state() == chromeos::STATE_DUAL_MIRROR); |
| 109 |
| 110 base::ListValue displays; |
| 111 for (size_t i = 0; i < monitor_manager->GetNumDisplays(); ++i) { |
| 112 const gfx::Display& display = monitor_manager->GetDisplayAt(i); |
| 113 const gfx::Rect& bounds = display.bounds(); |
| 114 base::DictionaryValue* js_display = new base::DictionaryValue(); |
| 115 js_display->SetDouble("id", display.id()); |
| 116 js_display->SetDouble("x", bounds.x()); |
| 117 js_display->SetDouble("y", bounds.y()); |
| 118 js_display->SetDouble("width", bounds.width()); |
| 119 js_display->SetDouble("height", bounds.height()); |
| 120 displays.Set(i, js_display); |
| 121 } |
| 122 |
| 123 MonitorController* monitor_controller = |
| 124 ash::Shell::GetInstance()->monitor_controller(); |
| 125 base::FundamentalValue layout(static_cast<int>( |
| 126 monitor_controller->secondary_display_layout())); |
| 127 |
| 128 web_ui()->CallJavascriptFunction( |
| 129 "options.DisplayOptions.setDisplayInfo", |
| 130 mirroring, displays, layout); |
| 131 } |
| 132 |
| 133 void DisplayOptionsHandler::HandleDisplayInfo( |
| 134 const base::ListValue* unused_args) { |
| 135 SendDisplayInfo(); |
| 136 } |
| 137 |
| 138 void DisplayOptionsHandler::HandleMirroring(const base::ListValue* args) { |
| 139 DCHECK(!args->empty()); |
| 140 bool is_mirroring = false; |
| 141 args->GetBoolean(0, &is_mirroring); |
| 142 // We use 'PRIMARY_ONLY' for non-mirroring state for now. |
| 143 // TODO(mukai): fix this and support multiple display modes. |
| 144 chromeos::State new_state = |
| 145 is_mirroring ? STATE_DUAL_MIRROR : STATE_DUAL_PRIMARY_ONLY; |
| 146 ash::Shell::GetInstance()->output_configurator()->SetDisplayMode(new_state); |
| 147 SendDisplayInfo(); |
| 148 } |
| 149 |
| 150 void DisplayOptionsHandler::HandleDisplayLayout(const base::ListValue* args) { |
| 151 double layout = -1; |
| 152 if (!args->GetDouble(0, &layout)) { |
| 153 LOG(ERROR) << "Invalid parameter"; |
| 154 return; |
| 155 } |
| 156 |
| 157 ash::Shell::GetInstance()->monitor_controller()->SetSecondaryDisplayLayout( |
| 158 static_cast<MonitorController::SecondaryDisplayLayout>(layout)); |
| 159 SendDisplayInfo(); |
| 160 } |
| 161 |
| 162 } // namespace options2 |
| 163 } // namespace chromeos |
| OLD | NEW |