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.
5 #include "chrome/browser/ui/webui/options/chromeos/display_overscan_handler.h"
9 #include "ash/display/display_controller.h"
10 #include "ash/screen_util.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 #include "ui/gfx/screen.h"
27 // The value for the orientation of overscan operations.
28 const char kOrientationHorizontal
[] = "horizontal";
29 const char kOrientationVertical
[] = "vertical";
33 DisplayOverscanHandler::DisplayOverscanHandler() {
34 ash::Shell::GetScreen()->AddObserver(this);
37 DisplayOverscanHandler::~DisplayOverscanHandler() {
38 ash::Shell::GetScreen()->RemoveObserver(this);
41 void DisplayOverscanHandler::GetLocalizedValues(
42 base::DictionaryValue
* localized_strings
) {
43 RegisterTitle(localized_strings
, "displayOverscanPage",
44 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_TAB_TITLE
);
45 localized_strings
->SetString("shrinkAndExpand", l10n_util::GetStringUTF16(
46 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_SHRINK_EXPAND
));
47 localized_strings
->SetString("move", l10n_util::GetStringUTF16(
48 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_MOVE
));
49 localized_strings
->SetString("overscanReset", l10n_util::GetStringUTF16(
50 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_RESET_BUTTON_LABEL
));
51 localized_strings
->SetString("overscanOK", l10n_util::GetStringUTF16(
52 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_OK_BUTTON_LABEL
));
53 localized_strings
->SetString("overscanCancel", l10n_util::GetStringUTF16(
54 IDS_OPTIONS_SETTINGS_DISPLAY_OVERSCAN_CANCEL_BUTTON_LABEL
));
57 void DisplayOverscanHandler::RegisterMessages() {
58 web_ui()->RegisterMessageCallback(
60 base::Bind(&DisplayOverscanHandler::HandleStart
,
61 base::Unretained(this)));
62 web_ui()->RegisterMessageCallback(
64 base::Bind(&DisplayOverscanHandler::HandleCommit
,
65 base::Unretained(this)));
66 web_ui()->RegisterMessageCallback(
68 base::Bind(&DisplayOverscanHandler::HandleReset
,
69 base::Unretained(this)));
70 web_ui()->RegisterMessageCallback(
72 base::Bind(&DisplayOverscanHandler::HandleCancel
,
73 base::Unretained(this)));
74 web_ui()->RegisterMessageCallback(
76 base::Bind(&DisplayOverscanHandler::HandleMove
,
77 base::Unretained(this)));
78 web_ui()->RegisterMessageCallback(
80 base::Bind(&DisplayOverscanHandler::HandleResize
,
81 base::Unretained(this)));
84 void DisplayOverscanHandler::OnDisplayBoundsChanged(
85 const gfx::Display
& display
) {
88 void DisplayOverscanHandler::OnDisplayAdded(const gfx::Display
& new_display
) {
89 web_ui()->CallJavascriptFunction(
90 "options.DisplayOverscan.onOverscanCanceled");
93 void DisplayOverscanHandler::OnDisplayRemoved(const gfx::Display
& old_display
) {
94 web_ui()->CallJavascriptFunction(
95 "options.DisplayOverscan.onOverscanCanceled");
98 void DisplayOverscanHandler::HandleStart(const base::ListValue
* args
) {
99 int64 display_id
= gfx::Display::kInvalidDisplayID
;
100 std::string id_value
;
101 if (!args
->GetString(0, &id_value
)) {
102 LOG(ERROR
) << "Can't find ID";
106 if (!base::StringToInt64(id_value
, &display_id
) ||
107 display_id
== gfx::Display::kInvalidDisplayID
) {
108 LOG(ERROR
) << "Invalid parameter: " << id_value
;
112 const gfx::Display
& display
= ash::ScreenUtil::GetDisplayForId(display_id
);
113 DCHECK(display
.is_valid());
114 if (!display
.is_valid())
117 ash::DisplayController
* display_controller
=
118 ash::Shell::GetInstance()->display_controller();
119 overscan_calibrator_
.reset(new OverscanCalibrator(
121 display_controller
->GetOverscanInsets(display_id
)));
124 void DisplayOverscanHandler::HandleCommit(const base::ListValue
* unused_args
) {
125 if (overscan_calibrator_
.get()) {
126 overscan_calibrator_
->Commit();
127 overscan_calibrator_
.reset();
131 void DisplayOverscanHandler::HandleReset(const base::ListValue
* unused_args
) {
132 if (overscan_calibrator_
.get())
133 overscan_calibrator_
->Reset();
136 void DisplayOverscanHandler::HandleCancel(const base::ListValue
* unused_args
) {
137 overscan_calibrator_
.reset();
140 void DisplayOverscanHandler::HandleMove(const base::ListValue
* args
) {
141 std::string orientation
;
143 if (!args
->GetString(0, &orientation
)) {
144 LOG(ERROR
) << "The first argument must be orientation";
147 if (!args
->GetDouble(1, &length
)) {
148 LOG(ERROR
) << "The second argument must be a numeric";
152 if (!overscan_calibrator_
.get())
155 gfx::Insets insets
= overscan_calibrator_
->insets();
156 if (orientation
== kOrientationHorizontal
) {
157 insets
.Set(insets
.top(), insets
.left() + length
,
158 insets
.bottom(), insets
.right() - length
);
159 } else if (orientation
== kOrientationVertical
) {
160 insets
.Set(insets
.top() + length
, insets
.left(),
161 insets
.bottom() - length
, insets
.right());
163 LOG(ERROR
) << "The orientation must be '" << kOrientationHorizontal
164 << "' or '" << kOrientationVertical
<< "': "
168 overscan_calibrator_
->UpdateInsets(insets
);
171 void DisplayOverscanHandler::HandleResize(const base::ListValue
* args
) {
172 std::string orientation
;
174 if (!args
->GetString(0, &orientation
)) {
175 LOG(ERROR
) << "The first argument must be orientation";
178 if (!args
->GetDouble(1, &length
)) {
179 LOG(ERROR
) << "The second argument must be a numeric";
183 if (!overscan_calibrator_
.get())
186 gfx::Insets insets
= overscan_calibrator_
->insets();
187 if (orientation
== kOrientationHorizontal
) {
188 insets
.Set(insets
.top(), insets
.left() + length
,
189 insets
.bottom(), insets
.right() + length
);
190 } else if (orientation
== kOrientationVertical
) {
191 insets
.Set(insets
.top() + length
, insets
.left(),
192 insets
.bottom() + length
, insets
.right());
194 LOG(ERROR
) << "The orientation must be '" << kOrientationHorizontal
195 << "' or '" << kOrientationVertical
<< "': "
199 overscan_calibrator_
->UpdateInsets(insets
);
202 } // namespace options
203 } // namespace chromeos