Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / app_modal / javascript_app_modal_dialog.cc
blob1452bd37a3f663b0686c57a1b012cb2c92ed683b
1 // 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.
5 #include "components/app_modal/javascript_app_modal_dialog.h"
7 #include "components/app_modal/javascript_dialog_manager.h"
8 #include "components/app_modal/javascript_native_dialog_factory.h"
9 #include "content/public/browser/web_contents.h"
10 #include "ui/gfx/text_elider.h"
11 #include "url/origin.h"
13 namespace app_modal {
14 namespace {
16 // Control maximum sizes of various texts passed to us from javascript.
17 #if defined(OS_POSIX) && !defined(OS_MACOSX)
18 // Two-dimensional eliding. Reformat the text of the message dialog
19 // inserting line breaks because otherwise a single long line can overflow
20 // the message dialog (and crash/hang the GTK, depending on the version).
21 const int kMessageTextMaxRows = 32;
22 const int kMessageTextMaxCols = 132;
23 const int kDefaultPromptMaxRows = 24;
24 const int kDefaultPromptMaxCols = 132;
25 void EnforceMaxTextSize(const base::string16& in_string,
26 base::string16* out_string) {
27 gfx::ElideRectangleString(in_string, kMessageTextMaxRows,
28 kMessageTextMaxCols, false, out_string);
30 void EnforceMaxPromptSize(const base::string16& in_string,
31 base::string16* out_string) {
32 gfx::ElideRectangleString(in_string, kDefaultPromptMaxRows,
33 kDefaultPromptMaxCols, false, out_string);
35 #else
36 // One-dimensional eliding. Trust the window system to break the string
37 // appropriately, but limit its overall length to something reasonable.
38 const int kMessageTextMaxSize = 3000;
39 const int kDefaultPromptMaxSize = 2000;
40 void EnforceMaxTextSize(const base::string16& in_string,
41 base::string16* out_string) {
42 gfx::ElideString(in_string, kMessageTextMaxSize, out_string);
44 void EnforceMaxPromptSize(const base::string16& in_string,
45 base::string16* out_string) {
46 gfx::ElideString(in_string, kDefaultPromptMaxSize, out_string);
48 #endif
50 } // namespace
52 ChromeJavaScriptDialogExtraData::ChromeJavaScriptDialogExtraData()
53 : has_already_shown_a_dialog_(false),
54 suppress_javascript_messages_(false) {}
56 JavaScriptAppModalDialog::JavaScriptAppModalDialog(
57 content::WebContents* web_contents,
58 ExtraDataMap* extra_data_map,
59 const base::string16& title,
60 content::JavaScriptMessageType javascript_message_type,
61 const base::string16& message_text,
62 const base::string16& default_prompt_text,
63 bool display_suppress_checkbox,
64 bool is_before_unload_dialog,
65 bool is_reload,
66 const content::JavaScriptDialogManager::DialogClosedCallback& callback)
67 : AppModalDialog(web_contents, title),
68 extra_data_map_(extra_data_map),
69 javascript_message_type_(javascript_message_type),
70 display_suppress_checkbox_(display_suppress_checkbox),
71 is_before_unload_dialog_(is_before_unload_dialog),
72 is_reload_(is_reload),
73 callback_(callback),
74 use_override_prompt_text_(false) {
75 EnforceMaxTextSize(message_text, &message_text_);
76 EnforceMaxPromptSize(default_prompt_text, &default_prompt_text_);
79 JavaScriptAppModalDialog::~JavaScriptAppModalDialog() {
82 NativeAppModalDialog* JavaScriptAppModalDialog::CreateNativeDialog() {
83 return JavaScriptDialogManager::GetInstance()
84 ->native_dialog_factory()
85 ->CreateNativeJavaScriptDialog(this);
88 bool JavaScriptAppModalDialog::IsJavaScriptModalDialog() {
89 return true;
92 void JavaScriptAppModalDialog::Invalidate() {
93 if (!IsValid())
94 return;
96 AppModalDialog::Invalidate();
97 if (!callback_.is_null()) {
98 callback_.Run(false, base::string16());
99 callback_.Reset();
101 if (native_dialog())
102 CloseModalDialog();
105 void JavaScriptAppModalDialog::OnCancel(bool suppress_js_messages) {
106 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame
107 // will receive its activation messages before this dialog receives
108 // WM_DESTROY. The parent frame would then try to activate any modal dialogs
109 // that were still open in the ModalDialogQueue, which would send activation
110 // back to this one. The framework should be improved to handle this, so this
111 // is a temporary workaround.
112 CompleteDialog();
114 NotifyDelegate(false, base::string16(), suppress_js_messages);
117 void JavaScriptAppModalDialog::OnAccept(const base::string16& prompt_text,
118 bool suppress_js_messages) {
119 base::string16 prompt_text_to_use = prompt_text;
120 // This is only for testing.
121 if (use_override_prompt_text_)
122 prompt_text_to_use = override_prompt_text_;
124 CompleteDialog();
125 NotifyDelegate(true, prompt_text_to_use, suppress_js_messages);
128 void JavaScriptAppModalDialog::OnClose() {
129 NotifyDelegate(false, base::string16(), false);
132 void JavaScriptAppModalDialog::SetOverridePromptText(
133 const base::string16& override_prompt_text) {
134 override_prompt_text_ = override_prompt_text;
135 use_override_prompt_text_ = true;
138 void JavaScriptAppModalDialog::NotifyDelegate(bool success,
139 const base::string16& user_input,
140 bool suppress_js_messages) {
141 if (!IsValid())
142 return;
144 if (!callback_.is_null()) {
145 callback_.Run(success, user_input);
146 callback_.Reset();
149 // The callback_ above may delete web_contents_, thus removing the extra
150 // data from the map owned by ::JavaScriptDialogManager. Make sure
151 // to only use the data if still present. http://crbug.com/236476
152 ExtraDataMap::iterator extra_data =
153 extra_data_map_->find(GetSerializedOriginForWebContents(web_contents()));
154 if (extra_data != extra_data_map_->end()) {
155 extra_data->second.has_already_shown_a_dialog_ = true;
156 extra_data->second.suppress_javascript_messages_ = suppress_js_messages;
159 // On Views, we can end up coming through this code path twice :(.
160 // See crbug.com/63732.
161 AppModalDialog::Invalidate();
164 // static
165 std::string JavaScriptAppModalDialog::GetSerializedOriginForWebContents(
166 content::WebContents* contents) {
167 if (!contents)
168 return url::Origin().Serialize();
169 return url::Origin(contents->GetLastCommittedURL()).Serialize();
172 } // namespace app_modal