display_overscan_handler.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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_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"
22
23namespace chromeos {
24namespace options {
25namespace {
26
27// The value for the orientation of overscan operations.
28const char kOrientationHorizontal[] = "horizontal";
29const char kOrientationVertical[] = "vertical";
30
31}
32
33DisplayOverscanHandler::DisplayOverscanHandler() {
34  ash::Shell::GetScreen()->AddObserver(this);
35}
36
37DisplayOverscanHandler::~DisplayOverscanHandler() {
38  ash::Shell::GetScreen()->RemoveObserver(this);
39}
40
41void 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));
55}
56
57void DisplayOverscanHandler::RegisterMessages() {
58  web_ui()->RegisterMessageCallback(
59      "start",
60      base::Bind(&DisplayOverscanHandler::HandleStart,
61                 base::Unretained(this)));
62  web_ui()->RegisterMessageCallback(
63      "commit",
64      base::Bind(&DisplayOverscanHandler::HandleCommit,
65                 base::Unretained(this)));
66  web_ui()->RegisterMessageCallback(
67      "reset",
68      base::Bind(&DisplayOverscanHandler::HandleReset,
69                 base::Unretained(this)));
70  web_ui()->RegisterMessageCallback(
71      "cancel",
72      base::Bind(&DisplayOverscanHandler::HandleCancel,
73                 base::Unretained(this)));
74  web_ui()->RegisterMessageCallback(
75      "move",
76      base::Bind(&DisplayOverscanHandler::HandleMove,
77                 base::Unretained(this)));
78  web_ui()->RegisterMessageCallback(
79      "resize",
80      base::Bind(&DisplayOverscanHandler::HandleResize,
81                 base::Unretained(this)));
82}
83
84void DisplayOverscanHandler::OnDisplayBoundsChanged(
85    const gfx::Display& display) {
86}
87
88void DisplayOverscanHandler::OnDisplayAdded(const gfx::Display& new_display) {
89  web_ui()->CallJavascriptFunction(
90      "options.DisplayOverscan.onOverscanCanceled");
91}
92
93void DisplayOverscanHandler::OnDisplayRemoved(const gfx::Display& old_display) {
94  web_ui()->CallJavascriptFunction(
95      "options.DisplayOverscan.onOverscanCanceled");
96}
97
98void 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";
103    return;
104  }
105
106  if (!base::StringToInt64(id_value, &display_id) ||
107      display_id == gfx::Display::kInvalidDisplayID) {
108    LOG(ERROR) << "Invalid parameter: " << id_value;
109    return;
110  }
111
112  const gfx::Display& display = ash::ScreenUtil::GetDisplayForId(display_id);
113  DCHECK(display.is_valid());
114  if (!display.is_valid())
115    return;
116
117  ash::DisplayController* display_controller =
118      ash::Shell::GetInstance()->display_controller();
119  overscan_calibrator_.reset(new OverscanCalibrator(
120      display,
121      display_controller->GetOverscanInsets(display_id)));
122}
123
124void DisplayOverscanHandler::HandleCommit(const base::ListValue* unused_args) {
125  if (overscan_calibrator_.get()) {
126    overscan_calibrator_->Commit();
127    overscan_calibrator_.reset();
128  }
129}
130
131void DisplayOverscanHandler::HandleReset(const base::ListValue* unused_args) {
132  if (overscan_calibrator_.get())
133    overscan_calibrator_->Reset();
134}
135
136void DisplayOverscanHandler::HandleCancel(const base::ListValue* unused_args) {
137  overscan_calibrator_.reset();
138}
139
140void DisplayOverscanHandler::HandleMove(const base::ListValue* args) {
141  std::string orientation;
142  double length;
143  if (!args->GetString(0, &orientation)) {
144    LOG(ERROR) << "The first argument must be orientation";
145    return;
146  }
147  if (!args->GetDouble(1, &length)) {
148    LOG(ERROR) << "The second argument must be a numeric";
149    return;
150  }
151
152  if (!overscan_calibrator_.get())
153    return;
154
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());
162  } else {
163    LOG(ERROR) << "The orientation must be '" << kOrientationHorizontal
164               << "' or '" << kOrientationVertical << "': "
165               << orientation;
166    return;
167  }
168  overscan_calibrator_->UpdateInsets(insets);
169}
170
171void DisplayOverscanHandler::HandleResize(const base::ListValue* args) {
172  std::string orientation;
173  double length;
174  if (!args->GetString(0, &orientation)) {
175    LOG(ERROR) << "The first argument must be orientation";
176    return;
177  }
178  if (!args->GetDouble(1, &length)) {
179    LOG(ERROR) << "The second argument must be a numeric";
180    return;
181  }
182
183  if (!overscan_calibrator_.get())
184    return;
185
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());
193  } else {
194    LOG(ERROR) << "The orientation must be '" << kOrientationHorizontal
195               << "' or '" << kOrientationVertical << "': "
196               << orientation;
197    return;
198  }
199  overscan_calibrator_->UpdateInsets(insets);
200}
201
202}  // namespace options
203}  // namespace chromeos
204