blob: 5f0da86d50dc7b0ebea6cfacbb5376399b86372b [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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 "ash/keyboard_overlay/keyboard_overlay_delegate.h"
6
7#include <algorithm>
8
9#include "ash/shell.h"
10#include "base/bind.h"
11#include "base/memory/weak_ptr.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010012#include "base/strings/utf_string_conversions.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "base/values.h"
14#include "content/public/browser/web_ui.h"
15#include "content/public/browser/web_ui_message_handler.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000016#include "ui/aura/root_window.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017#include "ui/base/l10n/l10n_util.h"
18#include "ui/gfx/screen.h"
19#include "ui/views/controls/webview/web_dialog_view.h"
20#include "ui/views/widget/widget.h"
21
22using content::WebContents;
23using content::WebUIMessageHandler;
24
25namespace {
26
27const int kBaseWidth = 1252;
28const int kBaseHeight = 516;
29const int kHorizontalMargin = 28;
30
31// A message handler for detecting the timing when the web contents is painted.
32class PaintMessageHandler
33 : public WebUIMessageHandler,
34 public base::SupportsWeakPtr<PaintMessageHandler> {
35 public:
36 explicit PaintMessageHandler(views::Widget* widget) : widget_(widget) {}
37 virtual ~PaintMessageHandler() {}
38
39 // WebUIMessageHandler implementation.
40 virtual void RegisterMessages() OVERRIDE;
41
42 private:
43 void DidPaint(const ListValue* args);
44
45 views::Widget* widget_;
46
47 DISALLOW_COPY_AND_ASSIGN(PaintMessageHandler);
48};
49
50void PaintMessageHandler::RegisterMessages() {
51 web_ui()->RegisterMessageCallback(
52 "didPaint",
53 base::Bind(&PaintMessageHandler::DidPaint, base::Unretained(this)));
54}
55
56void PaintMessageHandler::DidPaint(const ListValue* args) {
57 // Show the widget after the web content has been painted.
58 widget_->Show();
59}
60
61} // namespace
62
63namespace ash {
64
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010065KeyboardOverlayDelegate::KeyboardOverlayDelegate(const base::string16& title,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000066 const GURL& url)
67 : title_(title),
68 url_(url),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000069 widget_(NULL) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000070}
71
72KeyboardOverlayDelegate::~KeyboardOverlayDelegate() {
73}
74
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000075views::Widget* KeyboardOverlayDelegate::Show(views::WebDialogView* view) {
76 widget_ = new views::Widget;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000077 views::Widget::InitParams params(
78 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000079 params.context = Shell::GetPrimaryRootWindow();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000080 params.delegate = view;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000081 widget_->Init(params);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000082
83 // Show the widget at the bottom of the work area.
84 gfx::Size size;
85 GetDialogSize(&size);
86 const gfx::Rect& rect = Shell::GetScreen()->GetDisplayNearestWindow(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000087 widget_->GetNativeView()).work_area();
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010088 gfx::Rect bounds(rect.x() + (rect.width() - size.width()) / 2,
89 rect.bottom() - size.height(),
Torne (Richard Coles)58218062012-11-14 11:43:16 +000090 size.width(),
91 size.height());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000092 widget_->SetBounds(bounds);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000093
94 // The widget will be shown when the web contents gets ready to display.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000095 return widget_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000096}
97
98ui::ModalType KeyboardOverlayDelegate::GetDialogModalType() const {
99 return ui::MODAL_TYPE_SYSTEM;
100}
101
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100102base::string16 KeyboardOverlayDelegate::GetDialogTitle() const {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103 return title_;
104}
105
106GURL KeyboardOverlayDelegate::GetDialogContentURL() const {
107 return url_;
108}
109
110void KeyboardOverlayDelegate::GetWebUIMessageHandlers(
111 std::vector<WebUIMessageHandler*>* handlers) const {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000112 handlers->push_back(new PaintMessageHandler(widget_));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000113}
114
115void KeyboardOverlayDelegate::GetDialogSize(
116 gfx::Size* size) const {
117 using std::min;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000118 DCHECK(widget_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000119 gfx::Rect rect = ash::Shell::GetScreen()->GetDisplayNearestWindow(
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +0100120 widget_->GetNativeView()).work_area();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000121 const int width = min(kBaseWidth, rect.width() - kHorizontalMargin);
122 const int height = width * kBaseHeight / kBaseWidth;
123 size->SetSize(width, height);
124}
125
126std::string KeyboardOverlayDelegate::GetDialogArgs() const {
127 return "[]";
128}
129
130void KeyboardOverlayDelegate::OnDialogClosed(
131 const std::string& json_retval) {
132 delete this;
133 return;
134}
135
136void KeyboardOverlayDelegate::OnCloseContents(WebContents* source,
137 bool* out_close_dialog) {
138}
139
140bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const {
141 return false;
142}
143
144bool KeyboardOverlayDelegate::HandleContextMenu(
145 const content::ContextMenuParams& params) {
146 return true;
147}
148
149} // namespace ash