OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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 // This file contains the implementation of TestWebViewDelegate, which serves |
| 6 // as the WebViewDelegate for the TestShellWebHost. The host is expected to |
| 7 // have initialized a MessageLoop before these methods are called. |
| 8 |
| 9 #include "webkit/tools/test_shell/test_webview_delegate.h" |
| 10 |
| 11 #include <objidl.h> |
| 12 #include <shlobj.h> |
| 13 #include <shlwapi.h> |
| 14 |
| 15 #include "base/gfx/point.h" |
| 16 #include "base/message_loop.h" |
| 17 #include "base/string_util.h" |
| 18 #include "base/trace_event.h" |
| 19 #include "net/base/net_errors.h" |
| 20 #include "webkit/glue/webdatasource.h" |
| 21 #include "webkit/glue/webdropdata.h" |
| 22 #include "webkit/glue/weberror.h" |
| 23 #include "webkit/glue/webframe.h" |
| 24 #include "webkit/glue/webpreferences.h" |
| 25 #include "webkit/glue/weburlrequest.h" |
| 26 #include "webkit/glue/webkit_glue.h" |
| 27 #include "webkit/glue/webview.h" |
| 28 #include "webkit/glue/plugins/plugin_list.h" |
| 29 #include "webkit/glue/plugins/webplugin_delegate_impl.h" |
| 30 #include "webkit/glue/window_open_disposition.h" |
| 31 #include "webkit/tools/test_shell/drag_delegate.h" |
| 32 #include "webkit/tools/test_shell/drop_delegate.h" |
| 33 #include "webkit/tools/test_shell/test_navigation_controller.h" |
| 34 #include "webkit/tools/test_shell/test_shell.h" |
| 35 |
| 36 namespace { |
| 37 |
| 38 // Adds a file called "DRTFakeFile" to |data_object| (CF_HDROP). Use to fake |
| 39 // dragging a file. |
| 40 void AddDRTFakeFileToDataObject(IDataObject* data_object) { |
| 41 STGMEDIUM medium = {0}; |
| 42 medium.tymed = TYMED_HGLOBAL; |
| 43 |
| 44 const char filename[] = "DRTFakeFile"; |
| 45 const int filename_len = arraysize(filename); |
| 46 |
| 47 // Allocate space for the DROPFILES struct, filename, and 2 null characters. |
| 48 medium.hGlobal = GlobalAlloc(GPTR, sizeof(DROPFILES) + filename_len + 2); |
| 49 DCHECK(medium.hGlobal); |
| 50 DROPFILES* drop_files = static_cast<DROPFILES*>(GlobalLock(medium.hGlobal)); |
| 51 drop_files->pFiles = sizeof(DROPFILES); |
| 52 drop_files->fWide = 0; // Filenames are ascii |
| 53 strcpy_s(reinterpret_cast<char*>(drop_files) + sizeof(DROPFILES), |
| 54 filename_len, filename); |
| 55 GlobalUnlock(medium.hGlobal); |
| 56 |
| 57 FORMATETC file_desc_fmt = {CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL}; |
| 58 data_object->SetData(&file_desc_fmt, &medium, TRUE); |
| 59 } |
| 60 |
| 61 } // namespace |
| 62 |
| 63 // WebViewDelegate ----------------------------------------------------------- |
| 64 |
| 65 TestWebViewDelegate::~TestWebViewDelegate() { |
| 66 if (custom_cursor_) |
| 67 DestroyIcon(custom_cursor_); |
| 68 RevokeDragDrop(shell_->webViewWnd()); |
| 69 } |
| 70 |
| 71 WebPluginDelegate* TestWebViewDelegate::CreatePluginDelegate( |
| 72 WebView* webview, |
| 73 const GURL& url, |
| 74 const std::string& mime_type, |
| 75 const std::string& clsid, |
| 76 std::string* actual_mime_type) { |
| 77 HWND hwnd = GetContainingWindow(webview); |
| 78 if (!hwnd) |
| 79 return NULL; |
| 80 |
| 81 bool allow_wildcard = true; |
| 82 WebPluginInfo info; |
| 83 if (!NPAPI::PluginList::Singleton()->GetPluginInfo(url, mime_type, clsid, |
| 84 allow_wildcard, &info, |
| 85 actual_mime_type)) |
| 86 return NULL; |
| 87 |
| 88 if (actual_mime_type && !actual_mime_type->empty()) |
| 89 return WebPluginDelegateImpl::Create(info.file, *actual_mime_type, hwnd); |
| 90 else |
| 91 return WebPluginDelegateImpl::Create(info.file, mime_type, hwnd); |
| 92 } |
| 93 |
| 94 void TestWebViewDelegate::ShowJavaScriptAlert(const std::wstring& message) { |
| 95 } |
| 96 |
| 97 void TestWebViewDelegate::StartDragging(WebView* webview, |
| 98 const WebDropData& drop_data) { |
| 99 |
| 100 if (!drag_delegate_) |
| 101 drag_delegate_ = new TestDragDelegate(shell_->webViewWnd(), |
| 102 shell_->webView()); |
| 103 if (webkit_glue::IsLayoutTestMode()) { |
| 104 if (shell_->layout_test_controller()->ShouldAddFileToPasteboard()) { |
| 105 // Add a file called DRTFakeFile to the drag&drop clipboard. |
| 106 AddDRTFakeFileToDataObject(drop_data.data_object); |
| 107 } |
| 108 |
| 109 // When running a test, we need to fake a drag drop operation otherwise |
| 110 // Windows waits for real mouse events to know when the drag is over. |
| 111 EventSendingController::DoDragDrop(drop_data.data_object); |
| 112 } else { |
| 113 const DWORD ok_effect = DROPEFFECT_COPY | DROPEFFECT_LINK | DROPEFFECT_MOVE; |
| 114 DWORD effect; |
| 115 HRESULT res = DoDragDrop(drop_data.data_object, drag_delegate_.get(), |
| 116 ok_effect, &effect); |
| 117 DCHECK(DRAGDROP_S_DROP == res || DRAGDROP_S_CANCEL == res); |
| 118 } |
| 119 webview->DragSourceSystemDragEnded(); |
| 120 } |
| 121 |
| 122 void TestWebViewDelegate::Show(WebWidget* webwidget, WindowOpenDisposition) { |
| 123 if (webwidget == shell_->webView()) { |
| 124 ShowWindow(shell_->mainWnd(), SW_SHOW); |
| 125 UpdateWindow(shell_->mainWnd()); |
| 126 } else if (webwidget == shell_->popup()) { |
| 127 ShowWindow(shell_->popupWnd(), SW_SHOW); |
| 128 UpdateWindow(shell_->popupWnd()); |
| 129 } |
| 130 } |
| 131 |
| 132 void TestWebViewDelegate::CloseWidgetSoon(WebWidget* webwidget) { |
| 133 if (webwidget == shell_->webView()) { |
| 134 PostMessage(shell_->mainWnd(), WM_CLOSE, 0, 0); |
| 135 } else if (webwidget == shell_->popup()) { |
| 136 shell_->ClosePopup(); |
| 137 } |
| 138 } |
| 139 |
| 140 void TestWebViewDelegate::SetCursor(WebWidget* webwidget, |
| 141 const WebCursor& cursor) { |
| 142 if (WebWidgetHost* host = GetHostForWidget(webwidget)) { |
| 143 if (custom_cursor_) { |
| 144 DestroyIcon(custom_cursor_); |
| 145 custom_cursor_ = NULL; |
| 146 } |
| 147 if (cursor.IsCustom()) { |
| 148 custom_cursor_ = cursor.GetCustomCursor(); |
| 149 host->SetCursor(custom_cursor_); |
| 150 } else { |
| 151 HINSTANCE mod_handle = GetModuleHandle(NULL); |
| 152 host->SetCursor(cursor.GetCursor(mod_handle)); |
| 153 } |
| 154 } |
| 155 } |
| 156 |
| 157 void TestWebViewDelegate::GetWindowRect(WebWidget* webwidget, |
| 158 gfx::Rect* out_rect) { |
| 159 if (WebWidgetHost* host = GetHostForWidget(webwidget)) { |
| 160 RECT rect; |
| 161 ::GetWindowRect(host->window_handle(), &rect); |
| 162 *out_rect = gfx::Rect(rect); |
| 163 } |
| 164 } |
| 165 |
| 166 void TestWebViewDelegate::SetWindowRect(WebWidget* webwidget, |
| 167 const gfx::Rect& rect) { |
| 168 if (webwidget == shell_->webView()) { |
| 169 // ignored |
| 170 } else if (webwidget == shell_->popup()) { |
| 171 MoveWindow(shell_->popupWnd(), |
| 172 rect.x(), rect.y(), rect.width(), rect.height(), FALSE); |
| 173 } |
| 174 } |
| 175 |
| 176 void TestWebViewDelegate::GetRootWindowRect(WebWidget* webwidget, |
| 177 gfx::Rect* out_rect) { |
| 178 if (WebWidgetHost* host = GetHostForWidget(webwidget)) { |
| 179 RECT rect; |
| 180 HWND root_window = ::GetAncestor(host->window_handle(), GA_ROOT); |
| 181 ::GetWindowRect(root_window, &rect); |
| 182 *out_rect = gfx::Rect(rect); |
| 183 } |
| 184 } |
| 185 |
| 186 void TestWebViewDelegate::RunModal(WebWidget* webwidget) { |
| 187 Show(webwidget, NEW_WINDOW); |
| 188 |
| 189 WindowList* wl = TestShell::windowList(); |
| 190 for (WindowList::const_iterator i = wl->begin(); i != wl->end(); ++i) { |
| 191 if (*i != shell_->mainWnd()) |
| 192 EnableWindow(*i, FALSE); |
| 193 } |
| 194 |
| 195 shell_->set_is_modal(true); |
| 196 MessageLoop::current()->Run(); |
| 197 |
| 198 for (WindowList::const_iterator i = wl->begin(); i != wl->end(); ++i) |
| 199 EnableWindow(*i, TRUE); |
| 200 } |
| 201 |
| 202 // Private methods ----------------------------------------------------------- |
| 203 |
| 204 void TestWebViewDelegate::SetPageTitle(const std::wstring& title) { |
| 205 // The Windows test shell, pre-refactoring, ignored this. *shrug* |
| 206 } |
| 207 |
| 208 void TestWebViewDelegate::SetAddressBarURL(const GURL& url) { |
| 209 std::wstring url_string = UTF8ToWide(url.spec()); |
| 210 SendMessage(shell_->editWnd(), WM_SETTEXT, 0, |
| 211 reinterpret_cast<LPARAM>(url_string.c_str())); |
| 212 } |
| 213 |
OLD | NEW |