1 // Copyright (c) 2013 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/options/chromeos/display_overscan_handler.h"
6
7 #include <string>
8
9 #include "ash/display/display_controller.h"
10 #include "ash/screen_ash.h"
11 #include "ash/shell.h"
12 #include "base/bind.h"
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/chromeos/display/overscan_calibrator.h"
17 #include "content/public/browser/web_ui.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/gfx/display.h"
21
22 namespace chromeos {
23 namespace options {
24 namespace {
25
26 // The value for the orientation of overscan operations.
27 const char kOrientationHorizontal[] = "horizontal";
28 const char kOrientationVertical[] = "vertical";
29
30 }
31
DisplayOverscanHandler()32 DisplayOverscanHandler::DisplayOverscanHandler() {
33 ash::Shell::GetScreen()->AddObserver(this);
34 }
35
~DisplayOverscanHandler()36 DisplayOverscanHandler::~DisplayOverscanHandler() {
37 ash::Shell::GetScreen()->RemoveObserver(this);
38 }
39
GetLocalizedValues(DictionaryValue * localized_strings)40 void DisplayOverscanHandler::GetLocalizedValues(
41 DictionaryValue* localized_strings) {
42 RegisterTitle(localized_strings, "displayOverscanPage",
43 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_TAB_TITLE);
44 localized_strings->SetString("shrinkAndExpand", l10n_util::GetStringUTF16(
45 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_SHRINK_EXPAND));
46 localized_strings->SetString("move", l10n_util::GetStringUTF16(
47 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_MOVE));
48 localized_strings->SetString("overscanReset", l10n_util::GetStringUTF16(
49 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_RESET_BUTTON_LABEL));
50 localized_strings->SetString("overscanOK", l10n_util::GetStringUTF16(
51 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_OK_BUTTON_LABEL));
52 localized_strings->SetString("overscanCancel", l10n_util::GetStringUTF16(
53 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_CANCEL_BUTTON_LABEL));
54 }
55
RegisterMessages()56 void DisplayOverscanHandler::RegisterMessages() {
57 web_ui()->RegisterMessageCallback(
58 "start",
59 base::Bind(&DisplayOverscanHandler::HandleStart,
60 base::Unretained(this)));
61 web_ui()->RegisterMessageCallback(
62 "commit",
63 base::Bind(&DisplayOverscanHandler::HandleCommit,
64 base::Unretained(this)));
65 web_ui()->RegisterMessageCallback(
66 "reset",
67 base::Bind(&DisplayOverscanHandler::HandleReset,
68 base::Unretained(this)));
69 web_ui()->RegisterMessageCallback(
70 "cancel",
71 base::Bind(&DisplayOverscanHandler::HandleCancel,
72 base::Unretained(this)));
73 web_ui()->RegisterMessageCallback(
74 "move",
75 base::Bind(&DisplayOverscanHandler::HandleMove,
76 base::Unretained(this)));
77 web_ui()->RegisterMessageCallback(
78 "resize",
79 base::Bind(&DisplayOverscanHandler::HandleResize,
80 base::Unretained(this)));
81 }
82
OnDisplayBoundsChanged(const gfx::Display & display)83 void DisplayOverscanHandler::OnDisplayBoundsChanged(
84 const gfx::Display& display) {
85 }
86
OnDisplayAdded(const gfx::Display & new_display)87 void DisplayOverscanHandler::OnDisplayAdded(const gfx::Display& new_display) {
88 web_ui()->CallJavascriptFunction(
89 "options.DisplayOverscan.onOverscanCanceled");
90 }
91
OnDisplayRemoved(const gfx::Display & old_display)92 void DisplayOverscanHandler::OnDisplayRemoved(const gfx::Display& old_display) {
93 web_ui()->CallJavascriptFunction(
94 "options.DisplayOverscan.onOverscanCanceled");
95 }
96
HandleStart(const base::ListValue * args)97 void DisplayOverscanHandler::HandleStart(const base::ListValue* args) {
98 int64 display_id = gfx::Display::kInvalidDisplayID;
99 std::string id_value;
100 if (!args->GetString(0, &id_value)) {
101 LOG(ERROR) << "Can't find ID";
102 return;
103 }
104
105 if (!base::StringToInt64(id_value, &display_id) ||
106 display_id == gfx::Display::kInvalidDisplayID) {
107 LOG(ERROR) << "Invalid parameter: " << id_value;
108 return;
109 }
110
111 const gfx::Display& display = ash::ScreenAsh::GetDisplayForId(display_id);
112 DCHECK(display.is_valid());
113 if (!display.is_valid())
114 return;
115
116 ash::DisplayController* display_controller =
117 ash::Shell::GetInstance()->display_controller();
118 overscan_calibrator_.reset(new OverscanCalibrator(
119 display,
120 display_controller->GetOverscanInsets(display_id)));
121 }
122
HandleCommit(const base::ListValue * unused_args)123 void DisplayOverscanHandler::HandleCommit(const base::ListValue* unused_args) {
124 if (overscan_calibrator_.get()) {
125 overscan_calibrator_->Commit();
126 overscan_calibrator_.reset();
127 }
128 }
129
HandleReset(const base::ListValue * unused_args)130 void DisplayOverscanHandler::HandleReset(const base::ListValue* unused_args) {
131 if (overscan_calibrator_.get())
132 overscan_calibrator_->Reset();
133 }
134
HandleCancel(const base::ListValue * unused_args)135 void DisplayOverscanHandler::HandleCancel(const base::ListValue* unused_args) {
136 overscan_calibrator_.reset();
137 }
138
HandleMove(const base::ListValue * args)139 void DisplayOverscanHandler::HandleMove(const base::ListValue* args) {
140 std::string orientation;
141 double length;
142 if (!args->GetString(0, &orientation)) {
143 LOG(ERROR) << "The first argument must be orientation";
144 return;
145 }
146 if (!args->GetDouble(1, &length)) {
147 LOG(ERROR) << "The second argument must be a numeric";
148 return;
149 }
150
151 if (!overscan_calibrator_.get())
152 return;
153
154 gfx::Insets insets = overscan_calibrator_->insets();
155 if (orientation == kOrientationHorizontal) {
156 insets.Set(insets.top(), insets.left() + length,
157 insets.bottom(), insets.right() - length);
158 } else if (orientation == kOrientationVertical) {
159 insets.Set(insets.top() + length, insets.left(),
160 insets.bottom() - length, insets.right());
161 } else {
162 LOG(ERROR) << "The orientation must be '" << kOrientationHorizontal
163 << "' or '" << kOrientationVertical << "': "
164 << orientation;
165 return;
166 }
167 overscan_calibrator_->UpdateInsets(insets);
168 }
169
HandleResize(const base::ListValue * args)170 void DisplayOverscanHandler::HandleResize(const base::ListValue* args) {
171 std::string orientation;
172 double length;
173 if (!args->GetString(0, &orientation)) {
174 LOG(ERROR) << "The first argument must be orientation";
175 return;
176 }
177 if (!args->GetDouble(1, &length)) {
178 LOG(ERROR) << "The second argument must be a numeric";
179 return;
180 }
181
182 if (!overscan_calibrator_.get())
183 return;
184
185 gfx::Insets insets = overscan_calibrator_->insets();
186 if (orientation == kOrientationHorizontal) {
187 insets.Set(insets.top(), insets.left() + length,
188 insets.bottom(), insets.right() + length);
189 } else if (orientation == kOrientationVertical) {
190 insets.Set(insets.top() + length, insets.left(),
191 insets.bottom() + length, insets.right());
192 } else {
193 LOG(ERROR) << "The orientation must be '" << kOrientationHorizontal
194 << "' or '" << kOrientationVertical << "': "
195 << orientation;
196 return;
197 }
198 overscan_calibrator_->UpdateInsets(insets);
199 }
200
201 } // namespace options
202 } // namespace chromeos
203