OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.h" | 5 #include "chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #import "base/mac/foundation_util.h" | 11 #import "base/mac/foundation_util.h" |
12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
13 #import "chrome/browser/chrome_browser_application_mac.h" | 13 #import "chrome/browser/chrome_browser_application_mac.h" |
14 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" | 14 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
16 #include "grit/ui_strings.h" | 16 #include "grit/ui_strings.h" |
17 #include "ui/base/l10n/l10n_util_mac.h" | 17 #include "ui/base/l10n/l10n_util_mac.h" |
18 #include "ui/base/ui_base_types.h" | 18 #include "ui/base/ui_base_types.h" |
| 19 #include "ui/gfx/text_elider.h" |
| 20 |
| 21 const int kSlotsPerLine = 50; |
| 22 const int kMessageTextMaxSlots = 2000; |
19 | 23 |
20 // Helper object that receives the notification that the dialog/sheet is | 24 // Helper object that receives the notification that the dialog/sheet is |
21 // going away. Is responsible for cleaning itself up. | 25 // going away. Is responsible for cleaning itself up. |
22 @interface JavaScriptAppModalDialogHelper : NSObject<NSAlertDelegate> { | 26 @interface JavaScriptAppModalDialogHelper : NSObject<NSAlertDelegate> { |
23 @private | 27 @private |
24 base::scoped_nsobject<NSAlert> alert_; | 28 base::scoped_nsobject<NSAlert> alert_; |
25 NSTextField* textField_; // WEAK; owned by alert_ | 29 NSTextField* textField_; // WEAK; owned by alert_ |
26 } | 30 } |
27 | 31 |
28 - (NSAlert*)alert; | 32 - (NSAlert*)alert; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 alert_ = [helper_ alert]; | 143 alert_ = [helper_ alert]; |
140 NSTextField* field = nil; | 144 NSTextField* field = nil; |
141 if (text_field) { | 145 if (text_field) { |
142 field = [helper_ textField]; | 146 field = [helper_ textField]; |
143 [field setStringValue:base::SysUTF16ToNSString( | 147 [field setStringValue:base::SysUTF16ToNSString( |
144 dialog_->default_prompt_text())]; | 148 dialog_->default_prompt_text())]; |
145 } | 149 } |
146 [alert_ setDelegate:helper_]; | 150 [alert_ setDelegate:helper_]; |
147 NSString* informative_text = | 151 NSString* informative_text = |
148 base::SysUTF16ToNSString(dialog_->message_text()); | 152 base::SysUTF16ToNSString(dialog_->message_text()); |
| 153 |
| 154 // Truncate long JS alerts - crbug.com/331219 |
| 155 NSCharacterSet* newline_char_set = [NSCharacterSet newlineCharacterSet]; |
| 156 for (size_t index = 0, slots_count = 0; index < informative_text.length; |
| 157 ++index) { |
| 158 unichar current_char = [informative_text characterAtIndex:index]; |
| 159 if ([newline_char_set characterIsMember:current_char]) |
| 160 slots_count += kSlotsPerLine; |
| 161 else |
| 162 slots_count++; |
| 163 if (slots_count > kMessageTextMaxSlots) { |
| 164 base::string16 info_text = base::SysNSStringToUTF16(informative_text); |
| 165 informative_text = base::SysUTF16ToNSString( |
| 166 gfx::TruncateString(info_text, index)); |
| 167 break; |
| 168 } |
| 169 } |
| 170 |
149 [alert_ setInformativeText:informative_text]; | 171 [alert_ setInformativeText:informative_text]; |
150 NSString* message_text = | 172 NSString* message_text = |
151 base::SysUTF16ToNSString(dialog_->title()); | 173 base::SysUTF16ToNSString(dialog_->title()); |
152 [alert_ setMessageText:message_text]; | 174 [alert_ setMessageText:message_text]; |
153 [alert_ addButtonWithTitle:default_button]; | 175 [alert_ addButtonWithTitle:default_button]; |
154 if (!one_button) { | 176 if (!one_button) { |
155 NSButton* other = [alert_ addButtonWithTitle:other_button]; | 177 NSButton* other = [alert_ addButtonWithTitle:other_button]; |
156 [other setKeyEquivalent:@"\e"]; | 178 [other setKeyEquivalent:@"\e"]; |
157 } | 179 } |
158 if (dialog_->display_suppress_checkbox()) { | 180 if (dialog_->display_suppress_checkbox()) { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 320 |
299 //////////////////////////////////////////////////////////////////////////////// | 321 //////////////////////////////////////////////////////////////////////////////// |
300 // NativeAppModalDialog, public: | 322 // NativeAppModalDialog, public: |
301 | 323 |
302 // static | 324 // static |
303 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( | 325 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( |
304 JavaScriptAppModalDialog* dialog, | 326 JavaScriptAppModalDialog* dialog, |
305 gfx::NativeWindow parent_window) { | 327 gfx::NativeWindow parent_window) { |
306 return new JavaScriptAppModalDialogCocoa(dialog); | 328 return new JavaScriptAppModalDialogCocoa(dialog); |
307 } | 329 } |
OLD | NEW |