summaryrefslogtreecommitdiffstats
path: root/content
diff options
context:
space:
mode:
authoravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-03 18:02:07 +0000
committeravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-03 18:02:07 +0000
commit3ab9cb81731759c6e11f8c5296c1b75a7d2dadf1 (patch)
tree9344ece34a634b18d3e226c738471159d32484a6 /content
parenteadd4e31649b1a9da5d47864ff26ccd06b45edba (diff)
downloadchromium_src-3ab9cb81731759c6e11f8c5296c1b75a7d2dadf1.zip
chromium_src-3ab9cb81731759c6e11f8c5296c1b75a7d2dadf1.tar.gz
chromium_src-3ab9cb81731759c6e11f8c5296c1b75a7d2dadf1.tar.bz2
Remove JS dialog dependency from content.
BUG=71097 TEST=all types of javascript dialogs work, onbeforeunload dialogs work too Review URL: http://codereview.chromium.org/7096016 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@87806 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r--content/browser/DEPS2
-rw-r--r--content/browser/javascript_dialogs.h80
-rw-r--r--content/browser/renderer_host/render_view_host_browsertest.cc3
-rw-r--r--content/browser/tab_contents/tab_contents.cc97
-rw-r--r--content/browser/tab_contents/tab_contents.h34
-rw-r--r--content/browser/tab_contents/tab_contents_delegate.cc40
-rw-r--r--content/browser/tab_contents/tab_contents_delegate.h9
-rw-r--r--content/content_browser.gypi1
8 files changed, 187 insertions, 79 deletions
diff --git a/content/browser/DEPS b/content/browser/DEPS
index c2e7d07..2f4ed5e 100644
--- a/content/browser/DEPS
+++ b/content/browser/DEPS
@@ -54,8 +54,6 @@ include_rules = [
"+chrome/browser/utility_process_host.h",
- "+chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h",
- "+chrome/browser/ui/app_modal_dialogs/message_box_handler.h",
"+chrome/browser/ui/crypto_module_password_dialog.h",
"+chrome/common/chrome_constants.h",
diff --git a/content/browser/javascript_dialogs.h b/content/browser/javascript_dialogs.h
new file mode 100644
index 0000000..caeac36
--- /dev/null
+++ b/content/browser/javascript_dialogs.h
@@ -0,0 +1,80 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_BROWSER_JAVASCRIPT_DIALOG_DELEGATE_H_
+#define CONTENT_BROWSER_JAVASCRIPT_DIALOG_DELEGATE_H_
+#pragma once
+
+#include "base/string16.h"
+#include "ui/gfx/native_widget_types.h"
+
+class ExtensionHost;
+class GURL;
+class Profile;
+class TabContents;
+
+namespace IPC {
+class Message;
+}
+
+namespace content {
+
+// A class that invokes a JavaScript dialog must implement this interface to
+// allow the dialog implementation to get needed information and return results.
+class JavaScriptDialogDelegate {
+ public:
+ // This callback is invoked when the dialog is closed.
+ virtual void OnDialogClosed(IPC::Message* reply_msg,
+ bool success,
+ const string16& user_input) = 0;
+
+ // Returns the root native window with which to associate the dialog.
+ virtual gfx::NativeWindow GetDialogRootWindow() = 0;
+
+ // Returns the TabContents implementing this delegate, or NULL if there is
+ // none. TODO(avi): This breaks encapsulation and in general sucks; figure out
+ // a better way of doing this.
+ virtual TabContents* AsTabContents() = 0;
+
+ // Returns the ExtensionHost implementing this delegate, or NULL if there is
+ // none. TODO(avi): This is even suckier than AsTabContents above as it breaks
+ // layering; figure out a better way of doing this. http://crbug.com/84604
+ virtual ExtensionHost* AsExtensionHost() = 0;
+
+ protected:
+ virtual ~JavaScriptDialogDelegate() {}
+};
+
+
+// An interface consisting of methods that can be called to produce JavaScript
+// dialogs.
+class JavaScriptDialogCreator {
+ public:
+ // Displays a JavaScript dialog. |did_suppress_message| will not be nil; if
+ // |true| is returned in it, the caller will handle faking the reply.
+ // TODO(avi): Remove Profile from this call; http://crbug.com/84601
+ virtual void RunJavaScriptDialog(JavaScriptDialogDelegate* delegate,
+ const GURL& frame_url,
+ int dialog_flags,
+ const string16& message_text,
+ const string16& default_prompt_text,
+ IPC::Message* reply_message,
+ bool* did_suppress_message,
+ Profile* profile) = 0;
+
+ // Displays a dialog asking the user if they want to leave a page.
+ virtual void RunBeforeUnloadDialog(JavaScriptDialogDelegate* delegate,
+ const string16& message_text,
+ IPC::Message* reply_message) = 0;
+
+ // Resets any saved JavaScript dialog state for the delegate.
+ virtual void ResetJavaScriptState(JavaScriptDialogDelegate* delegate) = 0;
+
+ protected:
+ virtual ~JavaScriptDialogCreator() {}
+};
+
+} // namespace content
+
+#endif // CONTENT_BROWSER_JAVASCRIPT_DIALOG_DELEGATE_H_
diff --git a/content/browser/renderer_host/render_view_host_browsertest.cc b/content/browser/renderer_host/render_view_host_browsertest.cc
index 2b46ab8..116dbec 100644
--- a/content/browser/renderer_host/render_view_host_browsertest.cc
+++ b/content/browser/renderer_host/render_view_host_browsertest.cc
@@ -1,8 +1,9 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/time.h"
+#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
diff --git a/content/browser/tab_contents/tab_contents.cc b/content/browser/tab_contents/tab_contents.cc
index 8aedfa7..e7dd30b 100644
--- a/content/browser/tab_contents/tab_contents.cc
+++ b/content/browser/tab_contents/tab_contents.cc
@@ -18,8 +18,6 @@
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/profiles/profile.h"
-#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
-#include "chrome/common/chrome_constants.h"
#include "content/browser/child_process_security_policy.h"
#include "content/browser/content_browser_client.h"
#include "content/browser/host_zoom_map.h"
@@ -188,7 +186,6 @@ TabContents::TabContents(Profile* profile,
#if defined(OS_WIN)
message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
#endif
- suppress_javascript_messages_(false),
is_showing_before_unload_dialog_(false),
opener_web_ui_type_(WebUI::kNoWebUI),
closed_by_user_gesture_(false),
@@ -221,6 +218,10 @@ TabContents::~TabContents() {
// We don't want any notifications while we're running our destructor.
registrar_.RemoveAll();
+ // Clear out any JavaScript state.
+ if (delegate_)
+ delegate_->GetJavaScriptDialogCreator()->ResetJavaScriptState(this);
+
NotifyDisconnected();
// First cleanly close all child windows.
@@ -1089,10 +1090,11 @@ void TabContents::DidNavigateAnyFramePostCommit(
RenderViewHost* render_view_host,
const content::LoadCommittedDetails& details,
const ViewHostMsg_FrameNavigate_Params& params) {
- // If we navigate, start showing messages again. This does nothing to prevent
+ // If we navigate, reset JavaScript state. This does nothing to prevent
// a malicious script from spamming messages, since the script could just
// reload the page to stop blocking.
- suppress_javascript_messages_ = false;
+ if (delegate_)
+ delegate_->GetJavaScriptDialogCreator()->ResetJavaScriptState(this);
// Notify observers about navigation.
FOR_EACH_OBSERVER(TabContentsObserver, observers_,
@@ -1559,47 +1561,34 @@ void TabContents::RunJavaScriptMessage(
const int flags,
IPC::Message* reply_msg,
bool* did_suppress_message) {
- // Suppress javascript messages when requested and when inside a constrained
- // popup window (because that activates them and breaks them out of the
- // constrained window jail).
- // Also suppress messages when showing an interstitial. The interstitial is
- // shown over the previous page, we don't want the hidden page dialogs to
- // interfere with the interstitial.
+ // Suppress JavaScript dialogs when requested. Also suppress messages when
+ // showing an interstitial as it's shown over the previous page and we don't
+ // want the hidden page's dialogs to interfere with the interstitial.
bool suppress_this_message =
rvh->is_swapped_out() ||
- suppress_javascript_messages_ ||
showing_interstitial_page() ||
- (delegate() && delegate()->ShouldSuppressDialogs());
- if (delegate())
- suppress_this_message |=
- (delegate()->GetConstrainingContents(this) != this);
-
- *did_suppress_message = suppress_this_message;
+ !delegate_ ||
+ delegate_->ShouldSuppressDialogs();
if (!suppress_this_message) {
- base::TimeDelta time_since_last_message(
- base::TimeTicks::Now() - last_javascript_message_dismissal_);
- bool show_suppress_checkbox = false;
- // Show a checkbox offering to suppress further messages if this message is
- // being displayed within kJavascriptMessageExpectedDelay of the last one.
- if (time_since_last_message <
- base::TimeDelta::FromMilliseconds(
- chrome::kJavascriptMessageExpectedDelay))
- show_suppress_checkbox = true;
-
- RunJavascriptMessageBox(profile(),
- this,
- frame_url,
- flags,
- UTF16ToWideHack(message),
- UTF16ToWideHack(default_prompt),
- show_suppress_checkbox,
- reply_msg);
- } else {
- // If we are suppressing messages, just reply as is if the user immediately
+ delegate_->GetJavaScriptDialogCreator()->RunJavaScriptDialog(
+ this,
+ frame_url,
+ flags,
+ message,
+ default_prompt,
+ reply_msg,
+ &suppress_this_message,
+ profile());
+ }
+
+ if (suppress_this_message) {
+ // If we are suppressing messages, just reply as if the user immediately
// pressed "Cancel".
- OnMessageBoxClosed(reply_msg, false, std::wstring());
+ OnDialogClosed(reply_msg, false, string16());
}
+
+ *did_suppress_message = suppress_this_message;
}
void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
@@ -1607,14 +1596,21 @@ void TabContents::RunBeforeUnloadConfirm(const RenderViewHost* rvh,
IPC::Message* reply_msg) {
if (delegate())
delegate()->WillRunBeforeUnloadConfirm();
- bool suppress_this_message = rvh->is_swapped_out() ||
- (delegate() && delegate()->ShouldSuppressDialogs());
+
+ bool suppress_this_message =
+ rvh->is_swapped_out() ||
+ !delegate_ ||
+ delegate_->ShouldSuppressDialogs();
if (suppress_this_message) {
render_view_host()->JavaScriptDialogClosed(reply_msg, true, string16());
return;
}
+
is_showing_before_unload_dialog_ = true;
- RunBeforeUnloadDialog(this, UTF16ToWideHack(message), reply_msg);
+ delegate_->GetJavaScriptDialogCreator()->RunBeforeUnloadDialog(
+ this,
+ message,
+ reply_msg);
}
WebPreferences TabContents::GetWebkitPrefs() {
@@ -1812,14 +1808,11 @@ void TabContents::Observe(NotificationType type,
}
}
-gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
- return view_->GetTopLevelNativeWindow();
-}
+// Overridden from JavaScriptDialogDelegate
-void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
- bool success,
- const std::wstring& user_input) {
- last_javascript_message_dismissal_ = base::TimeTicks::Now();
+void TabContents::OnDialogClosed(IPC::Message* reply_msg,
+ bool success,
+ const string16& user_input) {
if (is_showing_before_unload_dialog_ && !success) {
// If a beforeunload dialog is canceled, we need to stop the throbber from
// spinning, since we forced it to start spinning in Navigate.
@@ -1830,11 +1823,11 @@ void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
is_showing_before_unload_dialog_ = false;
render_view_host()->JavaScriptDialogClosed(reply_msg,
success,
- WideToUTF16Hack(user_input));
+ user_input);
}
-void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
- set_suppress_javascript_messages(suppress_message_boxes);
+gfx::NativeWindow TabContents::GetDialogRootWindow() {
+ return view_->GetTopLevelNativeWindow();
}
TabContents* TabContents::AsTabContents() {
diff --git a/content/browser/tab_contents/tab_contents.h b/content/browser/tab_contents/tab_contents.h
index 8d07b29..164195f 100644
--- a/content/browser/tab_contents/tab_contents.h
+++ b/content/browser/tab_contents/tab_contents.h
@@ -14,7 +14,7 @@
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
-#include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h"
+#include "content/browser/javascript_dialogs.h"
#include "content/browser/renderer_host/render_view_host_delegate.h"
#include "content/browser/tab_contents/constrained_window.h"
#include "content/browser/tab_contents/navigation_controller.h"
@@ -61,7 +61,7 @@ class TabContents : public PageNavigator,
public NotificationObserver,
public RenderViewHostDelegate,
public RenderViewHostManager::Delegate,
- public JavaScriptAppModalDialogDelegate,
+ public content::JavaScriptDialogDelegate,
public net::NetworkChangeNotifier::OnlineStateObserver {
public:
// Flags passed to the TabContentsDelegate.NavigationStateChanged to tell it
@@ -383,12 +383,6 @@ class TabContents : public PageNavigator,
// Misc state & callbacks ----------------------------------------------------
- // Set whether the contents should block javascript message boxes or not.
- // Default is not to block any message boxes.
- void set_suppress_javascript_messages(bool suppress_javascript_messages) {
- suppress_javascript_messages_ = suppress_javascript_messages;
- }
-
// Returns true if the active NavigationEntry's page_id equals page_id.
bool IsActiveEntry(int32 page_id);
@@ -445,14 +439,13 @@ class TabContents : public PageNavigator,
}
bool closed_by_user_gesture() const { return closed_by_user_gesture_; }
- // Overridden from JavaScriptAppModalDialogDelegate:
- virtual void OnMessageBoxClosed(IPC::Message* reply_msg,
- bool success,
- const std::wstring& user_input);
- virtual void SetSuppressMessageBoxes(bool suppress_message_boxes);
- virtual gfx::NativeWindow GetMessageBoxRootWindow();
- virtual TabContents* AsTabContents();
- virtual ExtensionHost* AsExtensionHost();
+ // Overridden from JavaScriptDialogDelegate:
+ virtual void OnDialogClosed(IPC::Message* reply_msg,
+ bool success,
+ const string16& user_input) OVERRIDE;
+ virtual gfx::NativeWindow GetDialogRootWindow() OVERRIDE;
+ virtual TabContents* AsTabContents() OVERRIDE;
+ virtual ExtensionHost* AsExtensionHost() OVERRIDE;
// The BookmarkDragDelegate is used to forward bookmark drag and drop events
// to extensions.
@@ -672,7 +665,7 @@ class TabContents : public PageNavigator,
const GURL& frame_url,
const int flags,
IPC::Message* reply_msg,
- bool* did_suppress_message);
+ bool* did_suppress_message) OVERRIDE;
virtual void RunBeforeUnloadConfirm(const RenderViewHost* rvh,
const string16& message,
IPC::Message* reply_msg);
@@ -824,13 +817,6 @@ class TabContents : public PageNavigator,
base::win::ScopedHandle message_box_active_;
#endif
- // The time that the last javascript message was dismissed.
- base::TimeTicks last_javascript_message_dismissal_;
-
- // True if the user has decided to block future javascript messages. This is
- // reset on navigations to false on navigations.
- bool suppress_javascript_messages_;
-
// Set to true when there is an active "before unload" dialog. When true,
// we've forced the throbber to start in Navigate, and we need to remember to
// turn it off in OnJavaScriptMessageBoxClosed if the navigation is canceled.
diff --git a/content/browser/tab_contents/tab_contents_delegate.cc b/content/browser/tab_contents/tab_contents_delegate.cc
index 0f140c8..df709a7 100644
--- a/content/browser/tab_contents/tab_contents_delegate.cc
+++ b/content/browser/tab_contents/tab_contents_delegate.cc
@@ -4,6 +4,9 @@
#include "content/browser/tab_contents/tab_contents_delegate.h"
+#include "base/compiler_specific.h"
+#include "base/memory/singleton.h"
+#include "content/browser/javascript_dialogs.h"
#include "content/common/url_constants.h"
#include "ui/gfx/rect.h"
@@ -197,5 +200,42 @@ void TabContentsDelegate::DidNavigateMainFramePostCommit(
const MainFrameCommitDetails& details) {
}
+// A stubbed-out version of JavaScriptDialogCreator that doesn't do anything.
+class JavaScriptDialogCreatorStub : public content::JavaScriptDialogCreator {
+ public:
+ static JavaScriptDialogCreatorStub* GetInstance() {
+ return Singleton<JavaScriptDialogCreatorStub>::get();
+ }
+
+ virtual void RunJavaScriptDialog(content::JavaScriptDialogDelegate* delegate,
+ const GURL& frame_url,
+ int dialog_flags,
+ const string16& message_text,
+ const string16& default_prompt_text,
+ IPC::Message* reply_message,
+ bool* did_suppress_message,
+ Profile* profile) OVERRIDE {
+ *did_suppress_message = true;
+ }
+
+ virtual void RunBeforeUnloadDialog(
+ content::JavaScriptDialogDelegate* delegate,
+ const string16& message_text,
+ IPC::Message* reply_message) OVERRIDE {
+ delegate->OnDialogClosed(reply_message, true, string16());
+ }
+
+ virtual void ResetJavaScriptState(
+ content::JavaScriptDialogDelegate* delegate) OVERRIDE {
+ }
+ private:
+ friend struct DefaultSingletonTraits<JavaScriptDialogCreatorStub>;
+};
+
+content::JavaScriptDialogCreator*
+TabContentsDelegate::GetJavaScriptDialogCreator() {
+ return JavaScriptDialogCreatorStub::GetInstance();
+}
+
TabContentsDelegate::~TabContentsDelegate() {
}
diff --git a/content/browser/tab_contents/tab_contents_delegate.h b/content/browser/tab_contents/tab_contents_delegate.h
index 16d4e7a..0645128 100644
--- a/content/browser/tab_contents/tab_contents_delegate.h
+++ b/content/browser/tab_contents/tab_contents_delegate.h
@@ -15,6 +15,10 @@
#include "ui/gfx/native_widget_types.h"
#include "webkit/glue/window_open_disposition.h"
+namespace content {
+class JavaScriptDialogCreator;
+}
+
namespace gfx {
class Point;
class Rect;
@@ -299,6 +303,11 @@ class TabContentsDelegate {
TabContents* tab,
const MainFrameCommitDetails& details);
+ // Returns a pointer to a service to create JavaScript dialogs. The default
+ // pointer returned is to a stub service that marks all dialogs as suppressed
+ // and displays nothing.
+ virtual content::JavaScriptDialogCreator* GetJavaScriptDialogCreator();
+
protected:
virtual ~TabContentsDelegate();
};
diff --git a/content/content_browser.gypi b/content/content_browser.gypi
index a14ad66..69f03dc 100644
--- a/content/content_browser.gypi
+++ b/content/content_browser.gypi
@@ -165,6 +165,7 @@
'browser/in_process_webkit/webkit_context.h',
'browser/in_process_webkit/webkit_thread.cc',
'browser/in_process_webkit/webkit_thread.h',
+ 'browser/javascript_dialogs.h',
'browser/load_notification_details.h',
'browser/media_stream/media_stream_provider.cc',
'browser/media_stream/media_stream_provider.h',