Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1235)

Side by Side Diff: chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.mm

Issue 140323005: Truncate the Javascript alert message if the length is too long on Mac port (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Truncate the alert message Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 19
20 const int kSlotsPerLine = 50;
21 const int kMessageTextMaxSlots = 2000;
22
20 // Helper object that receives the notification that the dialog/sheet is 23 // Helper object that receives the notification that the dialog/sheet is
21 // going away. Is responsible for cleaning itself up. 24 // going away. Is responsible for cleaning itself up.
22 @interface JavaScriptAppModalDialogHelper : NSObject<NSAlertDelegate> { 25 @interface JavaScriptAppModalDialogHelper : NSObject<NSAlertDelegate> {
23 @private 26 @private
24 base::scoped_nsobject<NSAlert> alert_; 27 base::scoped_nsobject<NSAlert> alert_;
25 NSTextField* textField_; // WEAK; owned by alert_ 28 NSTextField* textField_; // WEAK; owned by alert_
26 } 29 }
27 30
28 - (NSAlert*)alert; 31 - (NSAlert*)alert;
29 - (NSTextField*)textField; 32 - (NSTextField*)textField;
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 alert_ = [helper_ alert]; 142 alert_ = [helper_ alert];
140 NSTextField* field = nil; 143 NSTextField* field = nil;
141 if (text_field) { 144 if (text_field) {
142 field = [helper_ textField]; 145 field = [helper_ textField];
143 [field setStringValue:base::SysUTF16ToNSString( 146 [field setStringValue:base::SysUTF16ToNSString(
144 dialog_->default_prompt_text())]; 147 dialog_->default_prompt_text())];
145 } 148 }
146 [alert_ setDelegate:helper_]; 149 [alert_ setDelegate:helper_];
147 NSString* informative_text = 150 NSString* informative_text =
148 base::SysUTF16ToNSString(dialog_->message_text()); 151 base::SysUTF16ToNSString(dialog_->message_text());
152
153 // Truncate long JS alerts - crbug.com/331219
154 NSArray* info_array =
155 [informative_text componentsSeparatedByCharactersInSet:
156 [NSCharacterSet newlineCharacterSet]];
157 informative_text = [[NSString alloc] init];
Avi (use Gerrit) 2014/01/24 01:53:24 This leaks. You can do informative_text = @"";
158 int occupied_slots = 0;
159 for (NSString* info_text in info_array) {
160 int new_slots =
161 info_text.length < kSlotsPerLine ? kSlotsPerLine : info_text.length;
Avi (use Gerrit) 2014/01/24 01:53:24 std::max?
162 int free_slots = kMessageTextMaxSlots - occupied_slots;
Avi (use Gerrit) 2014/01/24 01:53:24 You recompute free_slots each time; easier is to m
163 if (free_slots >= new_slots) {
164 informative_text = [informative_text stringByAppendingString:info_text];
165 informative_text = [informative_text stringByAppendingString:@"\n"];
166 occupied_slots += new_slots;
167 } else {
168 if ((int)info_text.length > free_slots)
Avi (use Gerrit) 2014/01/24 01:53:24 Alleviate the need for a cast if you make free_slo
169 info_text = [info_text substringToIndex:free_slots];
Avi (use Gerrit) 2014/01/24 01:53:24 Indent 2 for an if().
170 informative_text = [informative_text stringByAppendingString:info_text];
171 informative_text = [informative_text stringByAppendingString:@"\n..."];
Avi (use Gerrit) 2014/01/24 01:53:24 What bums me out is that we have gfx::TruncateStri
172 break;
173 }
174 }
175
149 [alert_ setInformativeText:informative_text]; 176 [alert_ setInformativeText:informative_text];
150 NSString* message_text = 177 NSString* message_text =
151 base::SysUTF16ToNSString(dialog_->title()); 178 base::SysUTF16ToNSString(dialog_->title());
152 [alert_ setMessageText:message_text]; 179 [alert_ setMessageText:message_text];
153 [alert_ addButtonWithTitle:default_button]; 180 [alert_ addButtonWithTitle:default_button];
154 if (!one_button) { 181 if (!one_button) {
155 NSButton* other = [alert_ addButtonWithTitle:other_button]; 182 NSButton* other = [alert_ addButtonWithTitle:other_button];
156 [other setKeyEquivalent:@"\e"]; 183 [other setKeyEquivalent:@"\e"];
157 } 184 }
158 if (dialog_->display_suppress_checkbox()) { 185 if (dialog_->display_suppress_checkbox()) {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 325
299 //////////////////////////////////////////////////////////////////////////////// 326 ////////////////////////////////////////////////////////////////////////////////
300 // NativeAppModalDialog, public: 327 // NativeAppModalDialog, public:
301 328
302 // static 329 // static
303 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( 330 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt(
304 JavaScriptAppModalDialog* dialog, 331 JavaScriptAppModalDialog* dialog,
305 gfx::NativeWindow parent_window) { 332 gfx::NativeWindow parent_window) {
306 return new JavaScriptAppModalDialogCocoa(dialog); 333 return new JavaScriptAppModalDialogCocoa(dialog);
307 } 334 }
OLDNEW
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698