dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 1 | // Copyright 2015 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 | |
James Cook | 6316a55 | 2017-03-05 21:46:21 | [diff] [blame] | 5 | #include "ash/common/accessibility_types.h" |
James Cook | 643b718 | 2017-03-05 22:02:58 | [diff] [blame^] | 6 | #include "ash/common/shelf/shelf_layout_manager.h" |
| 7 | #include "ash/common/shelf/wm_shelf.h" |
James Cook | 6316a55 | 2017-03-05 21:46:21 | [diff] [blame] | 8 | #include "ash/common/wm_window.h" |
jamescook | 8800b823 | 2016-10-19 12:46:27 | [diff] [blame] | 9 | #include "ash/public/cpp/shell_window_ids.h" |
dmazzoni | d47388d | 2016-03-31 16:03:51 | [diff] [blame] | 10 | #include "ash/root_window_controller.h" |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 11 | #include "ash/shell.h" |
avi | 8a07d5389 | 2015-12-24 22:13:53 | [diff] [blame] | 12 | #include "base/macros.h" |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 13 | #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" |
| 14 | #include "chrome/browser/chromeos/accessibility/chromevox_panel.h" |
kundaji | 14f8baa8 | 2016-11-08 18:34:23 | [diff] [blame] | 15 | #include "chrome/browser/data_use_measurement/data_use_web_contents_observer.h" |
dmazzoni | 5ba638d | 2016-01-07 22:58:54 | [diff] [blame] | 16 | #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 17 | #include "chrome/common/extensions/extension_constants.h" |
| 18 | #include "content/public/browser/web_contents.h" |
dmazzoni | 5ba638d | 2016-01-07 22:58:54 | [diff] [blame] | 19 | #include "extensions/browser/view_type_utils.h" |
oshima | d5c972e | 2016-04-28 23:17:14 | [diff] [blame] | 20 | #include "ui/display/screen.h" |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 21 | #include "ui/views/controls/webview/webview.h" |
| 22 | #include "ui/views/layout/fill_layout.h" |
| 23 | #include "ui/views/widget/widget.h" |
| 24 | #include "ui/wm/core/shadow_types.h" |
| 25 | #include "ui/wm/core/window_animations.h" |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | const int kPanelHeight = 35; |
| 30 | const char kChromeVoxPanelRelativeUrl[] = "/cvox2/background/panel.html"; |
| 31 | const char kFullscreenURLFragment[] = "fullscreen"; |
| 32 | const char kDisableSpokenFeedbackURLFragment[] = "close"; |
dtseng | 8624c9d | 2016-03-04 16:15:56 | [diff] [blame] | 33 | const char kFocusURLFragment[] = "focus"; |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | class ChromeVoxPanelWebContentsObserver : public content::WebContentsObserver { |
| 38 | public: |
| 39 | ChromeVoxPanelWebContentsObserver(content::WebContents* web_contents, |
| 40 | ChromeVoxPanel* panel) |
| 41 | : content::WebContentsObserver(web_contents), panel_(panel) {} |
| 42 | ~ChromeVoxPanelWebContentsObserver() override {} |
| 43 | |
| 44 | // content::WebContentsObserver overrides. |
| 45 | void DidFirstVisuallyNonEmptyPaint() override { |
| 46 | panel_->DidFirstVisuallyNonEmptyPaint(); |
| 47 | } |
| 48 | |
| 49 | void DidFinishNavigation( |
| 50 | content::NavigationHandle* navigation_handle) override { |
| 51 | // The ChromeVox panel uses the URL fragment to communicate state |
| 52 | // to this panel host. |
| 53 | std::string fragment = web_contents()->GetLastCommittedURL().ref(); |
| 54 | if (fragment == kDisableSpokenFeedbackURLFragment) |
| 55 | panel_->DisableSpokenFeedback(); |
| 56 | else if (fragment == kFullscreenURLFragment) |
| 57 | panel_->EnterFullscreen(); |
dtseng | 8624c9d | 2016-03-04 16:15:56 | [diff] [blame] | 58 | else if (fragment == kFocusURLFragment) |
| 59 | panel_->Focus(); |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 60 | else |
| 61 | panel_->ExitFullscreen(); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | ChromeVoxPanel* panel_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(ChromeVoxPanelWebContentsObserver); |
| 68 | }; |
| 69 | |
| 70 | ChromeVoxPanel::ChromeVoxPanel(content::BrowserContext* browser_context) |
dmazzoni | d47388d | 2016-03-31 16:03:51 | [diff] [blame] | 71 | : widget_(nullptr), web_view_(nullptr), panel_fullscreen_(false) { |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 72 | std::string url("chrome-extension://"); |
| 73 | url += extension_misc::kChromeVoxExtensionId; |
| 74 | url += kChromeVoxPanelRelativeUrl; |
| 75 | |
| 76 | views::WebView* web_view = new views::WebView(browser_context); |
dmazzoni | 5ba638d | 2016-01-07 22:58:54 | [diff] [blame] | 77 | content::WebContents* contents = web_view->GetWebContents(); |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 78 | web_contents_observer_.reset( |
dmazzoni | 5ba638d | 2016-01-07 22:58:54 | [diff] [blame] | 79 | new ChromeVoxPanelWebContentsObserver(contents, this)); |
kundaji | 14f8baa8 | 2016-11-08 18:34:23 | [diff] [blame] | 80 | data_use_measurement::DataUseWebContentsObserver::CreateForWebContents( |
| 81 | contents); |
dmazzoni | 5ba638d | 2016-01-07 22:58:54 | [diff] [blame] | 82 | extensions::SetViewType(contents, extensions::VIEW_TYPE_COMPONENT); |
| 83 | extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( |
| 84 | contents); |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 85 | web_view->LoadInitialURL(GURL(url)); |
| 86 | web_view_ = web_view; |
| 87 | |
| 88 | widget_ = new views::Widget(); |
| 89 | views::Widget::InitParams params( |
| 90 | views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 91 | aura::Window* root_window = ash::Shell::GetPrimaryRootWindow(); |
| 92 | params.parent = ash::Shell::GetContainer( |
| 93 | root_window, ash::kShellWindowId_SettingBubbleContainer); |
| 94 | params.delegate = this; |
| 95 | params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
| 96 | params.bounds = gfx::Rect(0, 0, root_window->bounds().width(), |
| 97 | root_window->bounds().height()); |
| 98 | widget_->Init(params); |
estade | ba7b9d7 | 2017-01-04 20:06:06 | [diff] [blame] | 99 | SetShadowElevation(widget_->GetNativeWindow(), wm::ShadowElevation::MEDIUM); |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 100 | |
oshima | d5c972e | 2016-04-28 23:17:14 | [diff] [blame] | 101 | display::Screen::GetScreen()->AddObserver(this); |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | ChromeVoxPanel::~ChromeVoxPanel() { |
oshima | d5c972e | 2016-04-28 23:17:14 | [diff] [blame] | 105 | display::Screen::GetScreen()->RemoveObserver(this); |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | aura::Window* ChromeVoxPanel::GetRootWindow() { |
| 109 | return GetWidget()->GetNativeWindow()->GetRootWindow(); |
| 110 | } |
| 111 | |
| 112 | void ChromeVoxPanel::Close() { |
| 113 | widget_->Close(); |
| 114 | } |
| 115 | |
| 116 | void ChromeVoxPanel::DidFirstVisuallyNonEmptyPaint() { |
| 117 | widget_->Show(); |
dmazzoni | 39d6d4b | 2016-03-29 22:18:00 | [diff] [blame] | 118 | UpdatePanelHeight(); |
| 119 | } |
| 120 | |
| 121 | void ChromeVoxPanel::UpdatePanelHeight() { |
jamescook | 1cad77e9 | 2016-08-31 00:02:26 | [diff] [blame] | 122 | ash::WmShelf* shelf = |
sky | 5dbc91d5 | 2017-01-13 00:26:14 | [diff] [blame] | 123 | ash::WmShelf::ForWindow(ash::WmWindow::Get(GetRootWindow())); |
jamescook | 1cad77e9 | 2016-08-31 00:02:26 | [diff] [blame] | 124 | if (!shelf->IsShelfInitialized()) |
dmazzoni | 39d6d4b | 2016-03-29 22:18:00 | [diff] [blame] | 125 | return; |
| 126 | |
| 127 | ash::ShelfLayoutManager* shelf_layout_manager = shelf->shelf_layout_manager(); |
| 128 | if (shelf_layout_manager) |
| 129 | shelf_layout_manager->SetChromeVoxPanelHeight(kPanelHeight); |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void ChromeVoxPanel::EnterFullscreen() { |
dtseng | 8624c9d | 2016-03-04 16:15:56 | [diff] [blame] | 133 | Focus(); |
dmazzoni | d47388d | 2016-03-31 16:03:51 | [diff] [blame] | 134 | panel_fullscreen_ = true; |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 135 | UpdateWidgetBounds(); |
| 136 | } |
| 137 | |
| 138 | void ChromeVoxPanel::ExitFullscreen() { |
dmazzoni | d47388d | 2016-03-31 16:03:51 | [diff] [blame] | 139 | widget_->Deactivate(); |
dmazzoni | 6915ba26 | 2016-02-11 23:47:44 | [diff] [blame] | 140 | widget_->widget_delegate()->set_can_activate(false); |
dmazzoni | d47388d | 2016-03-31 16:03:51 | [diff] [blame] | 141 | panel_fullscreen_ = false; |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 142 | UpdateWidgetBounds(); |
| 143 | } |
| 144 | |
| 145 | void ChromeVoxPanel::DisableSpokenFeedback() { |
| 146 | chromeos::AccessibilityManager::Get()->EnableSpokenFeedback( |
jamescook | a7f8dab | 2016-06-21 01:44:03 | [diff] [blame] | 147 | false, ash::A11Y_NOTIFICATION_NONE); |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 148 | } |
| 149 | |
dtseng | 8624c9d | 2016-03-04 16:15:56 | [diff] [blame] | 150 | void ChromeVoxPanel::Focus() { |
| 151 | widget_->widget_delegate()->set_can_activate(true); |
| 152 | widget_->Activate(); |
| 153 | web_view_->RequestFocus(); |
| 154 | } |
| 155 | |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 156 | const views::Widget* ChromeVoxPanel::GetWidget() const { |
| 157 | return widget_; |
| 158 | } |
| 159 | |
| 160 | views::Widget* ChromeVoxPanel::GetWidget() { |
| 161 | return widget_; |
| 162 | } |
| 163 | |
| 164 | void ChromeVoxPanel::DeleteDelegate() { |
| 165 | delete this; |
| 166 | } |
| 167 | |
| 168 | views::View* ChromeVoxPanel::GetContentsView() { |
| 169 | return web_view_; |
| 170 | } |
| 171 | |
oshima | d5c972e | 2016-04-28 23:17:14 | [diff] [blame] | 172 | void ChromeVoxPanel::OnDisplayMetricsChanged(const display::Display& display, |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 173 | uint32_t changed_metrics) { |
| 174 | UpdateWidgetBounds(); |
| 175 | } |
| 176 | |
| 177 | void ChromeVoxPanel::UpdateWidgetBounds() { |
| 178 | gfx::Rect bounds(GetRootWindow()->bounds().size()); |
dmazzoni | d47388d | 2016-03-31 16:03:51 | [diff] [blame] | 179 | if (!panel_fullscreen_) |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 180 | bounds.set_height(kPanelHeight); |
dmazzoni | d47388d | 2016-03-31 16:03:51 | [diff] [blame] | 181 | |
| 182 | // If we're in full-screen mode, give the panel a height of 0 unless |
| 183 | // it's active. |
| 184 | if (ash::GetRootWindowController(GetRootWindow()) |
| 185 | ->GetWindowForFullscreenMode() && |
| 186 | !widget_->IsActive()) { |
| 187 | bounds.set_height(0); |
| 188 | } |
| 189 | |
dmazzoni | 94a4f88 | 2015-11-13 05:46:52 | [diff] [blame] | 190 | widget_->SetBounds(bounds); |
| 191 | } |