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/display/display_manager.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 "chrome/grit/generated_resources.h"
18 #include "content/public/browser/web_ui.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::OnDisplayAdded(const gfx::Display
& new_display
) {
85 if (!overscan_calibrator_
)
88 web_ui()->CallJavascriptFunction(
89 "options.DisplayOverscan.onOverscanCanceled");
92 void DisplayOverscanHandler::OnDisplayRemoved(const gfx::Display
& old_display
) {
93 if (!overscan_calibrator_
)
96 web_ui()->CallJavascriptFunction(
97 "options.DisplayOverscan.onOverscanCanceled");
100 void DisplayOverscanHandler::OnDisplayMetricsChanged(const gfx::Display
&,
104 void DisplayOverscanHandler::HandleStart(const base::ListValue
* args
) {
105 int64 display_id
= gfx::Display::kInvalidDisplayID
;
106 std::string id_value
;
107 if (!args
->GetString(0, &id_value
)) {
108 LOG(ERROR
) << "Can't find ID";
112 if (!base::StringToInt64(id_value
, &display_id
) ||
113 display_id
== gfx::Display::kInvalidDisplayID
) {
114 LOG(ERROR
) << "Invalid parameter: " << id_value
;
118 const gfx::Display
& display
=
119 ash::Shell::GetInstance()->display_manager()->GetDisplayForId(display_id
);
120 DCHECK(display
.is_valid());
121 if (!display
.is_valid())
124 ash::DisplayController
* display_controller
=
125 ash::Shell::GetInstance()->display_controller();
126 overscan_calibrator_
.reset(new OverscanCalibrator(
128 display_controller
->GetOverscanInsets(display_id
)));
131 void DisplayOverscanHandler::HandleCommit(const base::ListValue
* unused_args
) {
132 if (overscan_calibrator_
.get()) {
133 overscan_calibrator_
->Commit();
134 overscan_calibrator_
.reset();
138 void DisplayOverscanHandler::HandleReset(const base::ListValue
* unused_args
) {
139 if (overscan_calibrator_
.get())
140 overscan_calibrator_
->Reset();
143 void DisplayOverscanHandler::HandleCancel(const base::ListValue
* unused_args
) {
144 overscan_calibrator_
.reset();
147 void DisplayOverscanHandler::HandleMove(const base::ListValue
* args
) {
148 std::string orientation
;
150 if (!args
->GetString(0, &orientation
)) {
151 LOG(ERROR
) << "The first argument must be orientation";
154 if (!args
->GetDouble(1, &length
)) {
155 LOG(ERROR
) << "The second argument must be a numeric";
159 if (!overscan_calibrator_
.get())
162 gfx::Insets insets
= overscan_calibrator_
->insets();
163 if (orientation
== kOrientationHorizontal
) {
164 insets
.Set(insets
.top(), insets
.left() + length
,
165 insets
.bottom(), insets
.right() - length
);
166 } else if (orientation
== kOrientationVertical
) {
167 insets
.Set(insets
.top() + length
, insets
.left(),
168 insets
.bottom() - length
, insets
.right());
170 LOG(ERROR
) << "The orientation must be '" << kOrientationHorizontal
171 << "' or '" << kOrientationVertical
<< "': "
175 overscan_calibrator_
->UpdateInsets(insets
);
178 void DisplayOverscanHandler::HandleResize(const base::ListValue
* args
) {
179 std::string orientation
;
181 if (!args
->GetString(0, &orientation
)) {
182 LOG(ERROR
) << "The first argument must be orientation";
185 if (!args
->GetDouble(1, &length
)) {
186 LOG(ERROR
) << "The second argument must be a numeric";
190 if (!overscan_calibrator_
.get())
193 gfx::Insets insets
= overscan_calibrator_
->insets();
194 if (orientation
== kOrientationHorizontal
) {
195 insets
.Set(insets
.top(), insets
.left() + length
,
196 insets
.bottom(), insets
.right() + length
);
197 } else if (orientation
== kOrientationVertical
) {
198 insets
.Set(insets
.top() + length
, insets
.left(),
199 insets
.bottom() + length
, insets
.right());
201 LOG(ERROR
) << "The orientation must be '" << kOrientationHorizontal
202 << "' or '" << kOrientationVertical
<< "': "
206 overscan_calibrator_
->UpdateInsets(insets
);
209 } // namespace options
210 } // namespace chromeos