From a112832e7f2b611a814f68b6f1335159f8f4dec0 Mon Sep 17 00:00:00 2001 From: "yaar@chromium.org" Date: Tue, 6 Oct 2009 18:38:46 +0000 Subject: Move runFileChooser from webview_delegate to WebViewClient. More details here: http://code.google.com/p/chromium/issues/detail?id=23385 Review URL: http://codereview.chromium.org/259031 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28134 0039d316-1c4b-4281-b951-d872f2087c98 --- webkit/api/public/WebFileChooserCompletion.h | 51 +++++++++++++++++++ webkit/api/public/WebViewClient.h | 18 +++---- webkit/api/src/WebFileChooserCompletionImpl.cpp | 60 +++++++++++++++++++++++ webkit/api/src/WebFileChooserCompletionImpl.h | 59 ++++++++++++++++++++++ webkit/glue/chrome_client_impl.cc | 65 +++++++++---------------- webkit/glue/webview_delegate.h | 23 --------- webkit/tools/test_shell/test_webview_delegate.h | 4 ++ webkit/webkit.gyp | 3 ++ 8 files changed, 210 insertions(+), 73 deletions(-) create mode 100644 webkit/api/public/WebFileChooserCompletion.h create mode 100644 webkit/api/src/WebFileChooserCompletionImpl.cpp create mode 100644 webkit/api/src/WebFileChooserCompletionImpl.h (limited to 'webkit') diff --git a/webkit/api/public/WebFileChooserCompletion.h b/webkit/api/public/WebFileChooserCompletion.h new file mode 100644 index 0000000..85c1f7e --- /dev/null +++ b/webkit/api/public/WebFileChooserCompletion.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef WebFileChooserCompletion_h +#define WebFileChooserCompletion_h + +namespace WebKit { + class WebString; + template class WebVector; + + // Gets called back when WebViewClient finished choosing a file. + class WebFileChooserCompletion { + public: + // Called with zero or more file names. Zero-lengthed vector means that + // the user cancelled or that file choosing failed. The callback instance + // is destroyed when this method is called. + virtual void didChooseFile(const WebVector& fileNames) = 0; + protected: + virtual ~WebFileChooserCompletion() {} + }; + +} // namespace WebKit + +#endif diff --git a/webkit/api/public/WebViewClient.h b/webkit/api/public/WebViewClient.h index 60691fa..7f9eb70 100644 --- a/webkit/api/public/WebViewClient.h +++ b/webkit/api/public/WebViewClient.h @@ -33,6 +33,7 @@ #include "WebDragOperation.h" #include "WebEditingAction.h" +#include "WebFileChooserCompletion.h" #include "WebTextAffinity.h" #include "WebWidgetClient.h" @@ -153,6 +154,14 @@ namespace WebKit { // Dialogs ------------------------------------------------------------- + // This method returns immediately after showing the dialog. When the + // dialog is closed, it should call the WebFileChooserCompletion to + // pass the results of the dialog. Returns false if + // WebFileChooseCompletion will never be called. + virtual bool runFileChooser( + bool multiSelect, const WebString& title, + const WebString& initialValue, WebFileChooserCompletion*) = 0; + // Displays a modal alert dialog containing the given message. Returns // once the user dismisses the dialog. virtual void runModalAlertDialog( @@ -180,15 +189,6 @@ namespace WebKit { virtual bool runModalBeforeUnloadDialog( WebFrame*, const WebString& message) = 0; - // This method returns immediately after showing the dialog. When the - // dialog is closed, it should call the WebFileChooserCompletion to - // pass the results of the dialog. - // FIXME hook this up - //virtual void runFileChooser( - // bool multiSelect, const WebString& title, - // const WebString& initialValue, WebFileChooserCompletion*) = 0; - - // UI ------------------------------------------------------------------ // Called when script modifies window.status diff --git a/webkit/api/src/WebFileChooserCompletionImpl.cpp b/webkit/api/src/WebFileChooserCompletionImpl.cpp new file mode 100644 index 0000000..4152dc5 --- /dev/null +++ b/webkit/api/src/WebFileChooserCompletionImpl.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebFileChooserCompletionImpl.h" + +namespace WebKit { + +WebFileChooserCompletionImpl::WebFileChooserCompletionImpl(PassRefPtr chooser) + : m_fileChooser(chooser) +{ +} + +WebFileChooserCompletionImpl::~WebFileChooserCompletionImpl() +{ +} + +void WebFileChooserCompletionImpl::didChooseFile(const WebVector& fileNames) +{ + if (fileNames.size() == 1) + m_fileChooser->chooseFile(fileNames[0]); + else { + // This clause handles a case of file_names.size()==0 too. + Vector paths; + for (size_t i = 0; i < fileNames.size(); ++i) + paths.append(fileNames[i]); + m_fileChooser->chooseFiles(paths); + } + // This object is no longer needed. + delete this; +} + +} // namespace WebKit diff --git a/webkit/api/src/WebFileChooserCompletionImpl.h b/webkit/api/src/WebFileChooserCompletionImpl.h new file mode 100644 index 0000000..6c262a8 --- /dev/null +++ b/webkit/api/src/WebFileChooserCompletionImpl.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef WebFileChooserCompletionImpl_h +#define WebFileChooserCompletionImpl_h + +#include "FileChooser.h" +#include +// FIXME: This relative path is a temporary hack to support using this +// header from webkit/glue. +#include "../public/WebFileChooserCompletion.h" +#include "../public/WebString.h" +#include "../public/WebVector.h" + +using WebKit::WebFileChooserCompletion; +using WebKit::WebString; +using WebKit::WebVector; + +namespace WebKit { + + class WebFileChooserCompletionImpl : public WebFileChooserCompletion { + public: + WebFileChooserCompletionImpl(PassRefPtr chooser); + ~WebFileChooserCompletionImpl(); + virtual void didChooseFile(const WebVector& fileNames); + private: + RefPtr m_fileChooser; + }; + +} // namespace WebKit + +#endif diff --git a/webkit/glue/chrome_client_impl.cc b/webkit/glue/chrome_client_impl.cc index 4ea1f40..691340d 100644 --- a/webkit/glue/chrome_client_impl.cc +++ b/webkit/glue/chrome_client_impl.cc @@ -31,6 +31,7 @@ #include "googleurl/src/gurl.h" #include "webkit/api/public/WebConsoleMessage.h" #include "webkit/api/public/WebCursorInfo.h" +#include "webkit/api/public/WebFileChooserCompletion.h" #include "webkit/api/public/WebFrameClient.h" #include "webkit/api/public/WebInputEvent.h" #include "webkit/api/public/WebKit.h" @@ -38,7 +39,9 @@ #include "webkit/api/public/WebRect.h" #include "webkit/api/public/WebTextDirection.h" #include "webkit/api/public/WebURLRequest.h" +#include "webkit/api/public/WebViewClient.h" #include "webkit/api/src/NotificationPresenterImpl.h" +#include "webkit/api/src/WebFileChooserCompletionImpl.h" #include "webkit/api/src/WrappedResourceRequest.h" #include "webkit/glue/chrome_client_impl.h" #include "webkit/glue/glue_util.h" @@ -53,6 +56,7 @@ using WebCore::PopupItem; using WebKit::WebConsoleMessage; using WebKit::WebCursorInfo; +using WebKit::WebFileChooserCompletionImpl; using WebKit::WebInputEvent; using WebKit::WebMouseEvent; using WebKit::WebNavigationPolicy; @@ -63,37 +67,10 @@ using WebKit::WebTextDirection; using WebKit::WebURL; using WebKit::WebURLRequest; using WebKit::WebVector; +using WebKit::WebViewClient; using WebKit::WebWidget; using WebKit::WrappedResourceRequest; -// Callback class that's given to the WebViewDelegate during a file choose -// operation. -class WebFileChooserCallbackImpl : public WebFileChooserCallback { - public: - WebFileChooserCallbackImpl(PassRefPtr file_chooser) - : file_chooser_(file_chooser) { - } - - virtual void OnFileChoose(const std::vector& file_names) { - if (file_names.size() == 1) { - file_chooser_->chooseFile( - webkit_glue::FilePathStringToString(file_names.front().value())); - } else { - // This clause handles a case of file_names.size()==0 too. - Vector paths; - for (std::vector::const_iterator filename = - file_names.begin(); filename != file_names.end(); ++filename) { - paths.append(webkit_glue::FilePathStringToString((*filename).value())); - } - file_chooser_->chooseFiles(paths); - } - } - - private: - RefPtr file_chooser_; - DISALLOW_COPY_AND_ASSIGN(WebFileChooserCallbackImpl); -}; - ChromeClientImpl::ChromeClientImpl(WebViewImpl* webview) : webview_(webview), toolbars_visible_(true), @@ -543,21 +520,27 @@ void ChromeClientImpl::exceededDatabaseQuota(WebCore::Frame* frame, } void ChromeClientImpl::runOpenPanel(WebCore::Frame* frame, - PassRefPtr fileChooser) { - WebViewDelegate* delegate = webview_->delegate(); - if (!delegate) + PassRefPtr file_chooser) { + WebViewClient* client = webview_->client(); + if (!client) return; - bool multiple_files = fileChooser->allowsMultipleFiles(); - - FilePath suggestion; - if (fileChooser->filenames().size() > 0) - suggestion = FilePath( - webkit_glue::StringToFilePathString(fileChooser->filenames()[0])); - - WebFileChooserCallbackImpl* chooser = - new WebFileChooserCallbackImpl(fileChooser); - delegate->RunFileChooser(multiple_files, string16(), suggestion, chooser); + bool multiple_files = file_chooser->allowsMultipleFiles(); + + WebString suggestion; + if (file_chooser->filenames().size() > 0) + suggestion = webkit_glue::StringToWebString(file_chooser->filenames()[0]); + + WebFileChooserCompletionImpl* chooser_completion = + new WebFileChooserCompletionImpl(file_chooser); + bool ok = client->runFileChooser(multiple_files, + WebString(), + suggestion, + chooser_completion); + if (!ok) { + // Choosing failed, so do callback with an empty list. + chooser_completion->didChooseFile(WebVector()); + } } void ChromeClientImpl::popupOpened(PopupContainer* popup_container, diff --git a/webkit/glue/webview_delegate.h b/webkit/glue/webview_delegate.h index 72e1997..5ad7b09 100644 --- a/webkit/glue/webview_delegate.h +++ b/webkit/glue/webview_delegate.h @@ -53,18 +53,6 @@ class WebDevToolsAgentDelegate; class WebView; struct ContextMenuMediaParams; -// Interface passed in to the WebViewDelegate to receive notification of the -// result of an open file dialog. -class WebFileChooserCallback { - public: - WebFileChooserCallback() {} - virtual ~WebFileChooserCallback() {} - virtual void OnFileChoose(const std::vector& file_names) { } - - private: - DISALLOW_COPY_AND_ASSIGN(WebFileChooserCallback); -}; - // TODO(darin): Eliminate WebViewDelegate in favor of WebViewClient. class WebViewDelegate : public WebKit::WebViewClient { public: @@ -115,17 +103,6 @@ class WebViewDelegate : public WebKit::WebViewClient { // UIDelegate -------------------------------------------------------------- - // Called to display a file chooser prompt. The prompt should be pre- - // populated with the given initial_filename string. The WebViewDelegate - // will own the WebFileChooserCallback object and is responsible for - // freeing it. - virtual void RunFileChooser(bool multi_select, - const string16& title, - const FilePath& initial_filename, - WebFileChooserCallback* file_chooser) { - delete file_chooser; - } - // @abstract Shows a context menu with commands relevant to a specific // element on the current page. // @param webview The WebView sending the delegate method. diff --git a/webkit/tools/test_shell/test_webview_delegate.h b/webkit/tools/test_shell/test_webview_delegate.h index ccf4404..68a6b46 100644 --- a/webkit/tools/test_shell/test_webview_delegate.h +++ b/webkit/tools/test_shell/test_webview_delegate.h @@ -126,6 +126,10 @@ class TestWebViewDelegate : public WebViewDelegate, virtual bool isShowingSpellingUI() { return false; } virtual void updateSpellingUIWithMisspelledWord( const WebKit::WebString& word) {} + virtual bool runFileChooser( + bool multi_select, const WebKit::WebString& title, + const WebKit::WebString& initial_value, + WebKit::WebFileChooserCompletion* chooser_completion){ return false; } virtual void runModalAlertDialog( WebKit::WebFrame* frame, const WebKit::WebString& message); virtual bool runModalConfirmDialog( diff --git a/webkit/webkit.gyp b/webkit/webkit.gyp index 7bb9705..f04c262 100644 --- a/webkit/webkit.gyp +++ b/webkit/webkit.gyp @@ -94,6 +94,7 @@ 'api/public/WebDataSource.h', 'api/public/WebDragData.h', 'api/public/WebEditingAction.h', + 'api/public/WebFileChooserCompletion.h', 'api/public/WebFindOptions.h', 'api/public/WebFrame.h', 'api/public/WebFrameClient.h', @@ -190,6 +191,8 @@ 'api/src/WebDataSourceImpl.cpp', 'api/src/WebDataSourceImpl.h', 'api/src/WebDragData.cpp', + 'api/src/WebFileChooserCompletionImpl.cpp', + 'api/src/WebFileChooserCompletionImpl.h', 'api/src/WebFontCache.cpp', 'api/src/WebForm.cpp', 'api/src/WebHistoryItem.cpp', -- cgit v1.1