summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/extensions/extension_install_view_controller.mm
blob: d621a69406ca9153afd06fa8829589033cd4ef86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
// Copyright (c) 2012 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.

#import "chrome/browser/ui/cocoa/extensions/extension_install_view_controller.h"

#include "base/auto_reset.h"
#include "base/i18n/rtl.h"
#include "base/mac/bundle_locations.h"
#include "base/mac/mac_util.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/bundle_installer.h"
#import "chrome/browser/ui/chrome_style.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/page_navigator.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_urls.h"
#include "skia/ext/skia_utils_mac.h"
#import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#import "ui/base/cocoa/controls/hyperlink_button_cell.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/gfx/image/image_skia_util_mac.h"

using content::OpenURLParams;
using content::Referrer;
using extensions::BundleInstaller;

namespace {

// A collection of attributes (bitmask) for how to draw a cell, the expand
// marker and the text in the cell.
enum CellAttributesMask {
  kBoldText                = 1 << 0,
  kNoExpandMarker          = 1 << 1,
  kUseBullet               = 1 << 2,
  kAutoExpandCell          = 1 << 3,
  kUseCustomLinkCell       = 1 << 4,
  kCanExpand               = 1 << 5,
};

typedef NSUInteger CellAttributes;

}  // namespace.

@interface ExtensionInstallViewController ()
- (BOOL)isBundleInstall;
- (BOOL)hasWebstoreData;
- (void)appendRatingStar:(const gfx::ImageSkia*)skiaImage;
- (void)onOutlineViewRowCountDidChange;
- (NSDictionary*)buildItemWithTitle:(NSString*)title
                     cellAttributes:(CellAttributes)cellAttributes
                           children:(NSArray*)children;
- (NSDictionary*)buildDetailToggleItem:(size_t)type
                 permissionsDetailIndex:(size_t)index;
- (NSArray*)buildWarnings:(const ExtensionInstallPrompt::Prompt&)prompt;
// Adds permissions of |type| from |prompt| to |children| and returns the
// the appropriate permissions header. If no permissions are found, NULL is
// returned.
- (NSString*)
appendPermissionsForPrompt:(const ExtensionInstallPrompt::Prompt&)prompt
                  withType:(ExtensionInstallPrompt::PermissionsType)type
                  children:(NSMutableArray*)children;
- (void)updateViewFrame:(NSRect)frame;
@end

@interface DetailToggleHyperlinkButtonCell : HyperlinkButtonCell {
  NSUInteger permissionsDetailIndex_;
  ExtensionInstallPrompt::DetailsType permissionsDetailType_;
  SEL linkClickedAction_;
}

@property(assign, nonatomic) NSUInteger permissionsDetailIndex;
@property(assign, nonatomic)
    ExtensionInstallPrompt::DetailsType permissionsDetailType;
@property(assign, nonatomic) SEL linkClickedAction;

@end

namespace {

// Padding above the warnings separator, we must also subtract this when hiding
// it.
const CGFloat kWarningsSeparatorPadding = 14;

// The left padding for the link cell.
const CGFloat kLinkCellPaddingLeft = 3;

// Maximum height we will adjust controls to when trying to accomodate their
// contents.
const CGFloat kMaxControlHeight = 250;

NSString* const kTitleKey = @"title";
NSString* const kChildrenKey = @"children";
NSString* const kCellAttributesKey = @"cellAttributes";
NSString* const kPermissionsDetailIndex = @"permissionsDetailIndex";
NSString* const kPermissionsDetailType = @"permissionsDetailType";

// Adjust the |control|'s height so that its content is not clipped.
// This also adds the change in height to the |totalOffset| and shifts the
// control down by that amount.
void OffsetControlVerticallyToFitContent(NSControl* control,
                                         CGFloat* totalOffset) {
  // Adjust the control's height so that its content is not clipped.
  NSRect currentRect = [control frame];
  NSRect fitRect = currentRect;
  fitRect.size.height = kMaxControlHeight;
  CGFloat desiredHeight = [[control cell] cellSizeForBounds:fitRect].height;
  CGFloat offset = desiredHeight - NSHeight(currentRect);

  [control setFrameSize:NSMakeSize(NSWidth(currentRect),
                                   NSHeight(currentRect) + offset)];

  *totalOffset += offset;

  // Move the control vertically by the new total offset.
  NSPoint origin = [control frame].origin;
  origin.y -= *totalOffset;
  [control setFrameOrigin:origin];
}

// Gets the desired height of |outlineView|. Simply using the view's frame
// doesn't work if an animation is pending.
CGFloat GetDesiredOutlineViewHeight(NSOutlineView* outlineView) {
  CGFloat height = 0;
  for (NSInteger i = 0; i < [outlineView numberOfRows]; ++i)
    height += NSHeight([outlineView rectOfRow:i]);
  return height;
}

void OffsetOutlineViewVerticallyToFitContent(NSOutlineView* outlineView,
                                             CGFloat* totalOffset) {
  NSScrollView* scrollView = [outlineView enclosingScrollView];
  NSRect frame = [scrollView frame];
  CGFloat desiredHeight = GetDesiredOutlineViewHeight(outlineView);
  if (desiredHeight > kMaxControlHeight)
    desiredHeight = kMaxControlHeight;
  CGFloat offset = desiredHeight - NSHeight(frame);
  frame.size.height += offset;

  *totalOffset += offset;

  // Move the control vertically by the new total offset.
  frame.origin.y -= *totalOffset;
  [scrollView setFrame:frame];
}

void AppendRatingStarsShim(const gfx::ImageSkia* skiaImage, void* data) {
  ExtensionInstallViewController* controller =
      static_cast<ExtensionInstallViewController*>(data);
  [controller appendRatingStar:skiaImage];
}

void DrawBulletInFrame(NSRect frame) {
  NSRect rect;
  rect.size.width = std::min(NSWidth(frame), NSHeight(frame)) * 0.25;
  rect.size.height = NSWidth(rect);
  rect.origin.x = frame.origin.x + (NSWidth(frame) - NSWidth(rect)) / 2.0;
  rect.origin.y = frame.origin.y + (NSHeight(frame) - NSHeight(rect)) / 2.0;
  rect = NSIntegralRect(rect);

  [[NSColor colorWithCalibratedWhite:0.0 alpha:0.42] set];
  [[NSBezierPath bezierPathWithOvalInRect:rect] fill];
}

bool HasAttribute(id item, CellAttributesMask attributeMask) {
  return [[item objectForKey:kCellAttributesKey] intValue] & attributeMask;
}

}  // namespace

@implementation ExtensionInstallViewController

@synthesize iconView = iconView_;
@synthesize titleField = titleField_;
@synthesize itemsField = itemsField_;
@synthesize cancelButton = cancelButton_;
@synthesize okButton = okButton_;
@synthesize outlineView = outlineView_;
@synthesize warningsSeparator = warningsSeparator_;
@synthesize ratingStars = ratingStars_;
@synthesize ratingCountField = ratingCountField_;
@synthesize userCountField = userCountField_;
@synthesize storeLinkButton = storeLinkButton_;

- (id)initWithNavigator:(content::PageNavigator*)navigator
               delegate:(ExtensionInstallPrompt::Delegate*)delegate
                 prompt:(scoped_refptr<ExtensionInstallPrompt::Prompt>)prompt {
  // We use a different XIB in the case of bundle installs, installs with
  // webstore data, or no permission warnings. These are laid out nicely for
  // the data they display.
  NSString* nibName = nil;
  if (prompt->type() == ExtensionInstallPrompt::BUNDLE_INSTALL_PROMPT) {
    nibName = @"ExtensionInstallPromptBundle";
  } else if (prompt->has_webstore_data()) {
    nibName = @"ExtensionInstallPromptWebstoreData";
  } else if (!prompt->ShouldShowPermissions() &&
             prompt->GetRetainedFileCount() == 0 &&
             prompt->GetRetainedDeviceCount() == 0) {
    nibName = @"ExtensionInstallPromptNoWarnings";
  } else {
    nibName = @"ExtensionInstallPrompt";
  }

  if ((self = [super initWithNibName:nibName
                              bundle:base::mac::FrameworkBundle()])) {
    navigator_ = navigator;
    delegate_ = delegate;
    prompt_ = prompt;
    warnings_.reset([[self buildWarnings:*prompt] retain]);
  }
  return self;
}

- (IBAction)storeLinkClicked:(id)sender {
  GURL store_url(extension_urls::GetWebstoreItemDetailURLPrefix() +
                 prompt_->extension()->id());
  navigator_->OpenURL(OpenURLParams(
      store_url, Referrer(), NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK,
      false));

  delegate_->InstallUIAbort(/*user_initiated=*/true);
}

- (IBAction)cancel:(id)sender {
  delegate_->InstallUIAbort(/*user_initiated=*/true);
}

- (IBAction)ok:(id)sender {
  delegate_->InstallUIProceed();
}

- (void)awakeFromNib {
  // Set control labels.
  [titleField_ setStringValue:base::SysUTF16ToNSString(prompt_->GetHeading())];
  NSRect okButtonRect;
  if (prompt_->HasAcceptButtonLabel()) {
    [okButton_ setTitle:base::SysUTF16ToNSString(
        prompt_->GetAcceptButtonLabel())];
  } else {
    [okButton_ removeFromSuperview];
    okButtonRect = [okButton_ frame];
    okButton_ = nil;
  }
  [cancelButton_ setTitle:prompt_->HasAbortButtonLabel() ?
      base::SysUTF16ToNSString(prompt_->GetAbortButtonLabel()) :
      l10n_util::GetNSString(IDS_CANCEL)];
  if ([self hasWebstoreData]) {
    prompt_->AppendRatingStars(AppendRatingStarsShim, self);
    [ratingCountField_ setStringValue:base::SysUTF16ToNSString(
        prompt_->GetRatingCount())];
    [userCountField_ setStringValue:base::SysUTF16ToNSString(
        prompt_->GetUserCount())];
    [[storeLinkButton_ cell] setUnderlineOnHover:YES];
    [[storeLinkButton_ cell] setTextColor:
        gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor())];
  }

  // The bundle install dialog has no icon.
  if (![self isBundleInstall])
    [iconView_ setImage:prompt_->icon().ToNSImage()];

  // The dialog is laid out in the NIB exactly how we want it assuming that
  // each label fits on one line. However, for each label, we want to allow
  // wrapping onto multiple lines. So we accumulate an offset by measuring how
  // big each label wants to be, and comparing it to how big it actually is.
  // Then we shift each label down and resize by the appropriate amount, then
  // finally resize the window.
  CGFloat totalOffset = 0.0;

  OffsetControlVerticallyToFitContent(titleField_, &totalOffset);

  // Resize |okButton_| and |cancelButton_| to fit the button labels, but keep
  // them right-aligned.
  NSSize buttonDelta;
  if (okButton_) {
    buttonDelta = [GTMUILocalizerAndLayoutTweaker sizeToFitView:okButton_];
    if (buttonDelta.width) {
      [okButton_ setFrame:NSOffsetRect([okButton_ frame],
                                       -buttonDelta.width, 0)];
      [cancelButton_ setFrame:NSOffsetRect([cancelButton_ frame],
                                           -buttonDelta.width, 0)];
    }
  } else {
    // Make |cancelButton_| right-aligned in the absence of |okButton_|.
    NSRect cancelButtonRect = [cancelButton_ frame];
    cancelButtonRect.origin.x =
        NSMaxX(okButtonRect) - NSWidth(cancelButtonRect);
    [cancelButton_ setFrame:cancelButtonRect];
  }
  buttonDelta = [GTMUILocalizerAndLayoutTweaker sizeToFitView:cancelButton_];
  if (buttonDelta.width) {
    [cancelButton_ setFrame:NSOffsetRect([cancelButton_ frame],
                                         -buttonDelta.width, 0)];
  }

  if ([self isBundleInstall]) {
    // We display the list of extension names as a simple text string, seperated
    // by newlines.
    BundleInstaller::ItemList items = prompt_->bundle()->GetItemsWithState(
        BundleInstaller::Item::STATE_PENDING);

    NSMutableString* joinedItems = [NSMutableString string];
    for (size_t i = 0; i < items.size(); ++i) {
      if (i > 0)
        [joinedItems appendString:@"\n"];
      [joinedItems appendString:base::SysUTF16ToNSString(
          items[i].GetNameForDisplay())];
    }
    [itemsField_ setStringValue:joinedItems];

    // Adjust the controls to fit the list of extensions.
    OffsetControlVerticallyToFitContent(itemsField_, &totalOffset);
  }

  // If there are any warnings, retained devices or retained files, then we
  // have to do some special layout.
  if (prompt_->ShouldShowPermissions() || prompt_->GetRetainedFileCount() > 0) {
    NSSize spacing = [outlineView_ intercellSpacing];
    spacing.width += 2;
    spacing.height += 2;
    [outlineView_ setIntercellSpacing:spacing];
    [[[[outlineView_ tableColumns] objectAtIndex:0] dataCell] setWraps:YES];
    for (id item in warnings_.get())
      [self expandItemAndChildren:item];

    // Adjust the outline view to fit the warnings.
    OffsetOutlineViewVerticallyToFitContent(outlineView_, &totalOffset);
  } else if ([self hasWebstoreData] || [self isBundleInstall]) {
    // Installs with webstore data and bundle installs that don't have a
    // permissions section need to hide controls related to that and shrink the
    // window by the space they take up.
    NSRect hiddenRect = NSUnionRect([warningsSeparator_ frame],
                                    [[outlineView_ enclosingScrollView] frame]);
    [warningsSeparator_ setHidden:YES];
    [[outlineView_ enclosingScrollView] setHidden:YES];
    totalOffset -= NSHeight(hiddenRect) + kWarningsSeparatorPadding;
  }

  // If necessary, adjust the window size.
  if (totalOffset) {
    NSRect currentRect = [[self view] bounds];
    currentRect.size.height += totalOffset;
    [self updateViewFrame:currentRect];
  }
}

- (BOOL)isBundleInstall {
  return prompt_->type() == ExtensionInstallPrompt::BUNDLE_INSTALL_PROMPT;
}

- (BOOL)hasWebstoreData {
  return prompt_->has_webstore_data();
}

- (void)appendRatingStar:(const gfx::ImageSkia*)skiaImage {
  NSImage* image = gfx::NSImageFromImageSkiaWithColorSpace(
      *skiaImage, base::mac::GetSystemColorSpace());
  NSRect frame = NSMakeRect(0, 0, skiaImage->width(), skiaImage->height());
  base::scoped_nsobject<NSImageView> view(
      [[NSImageView alloc] initWithFrame:frame]);
  [view setImage:image];

  // Add this star after all the other ones
  CGFloat maxStarRight = 0;
  if ([[ratingStars_ subviews] count]) {
    maxStarRight = NSMaxX([[[ratingStars_ subviews] lastObject] frame]);
  }
  NSRect starBounds = NSMakeRect(maxStarRight, 0,
                                 skiaImage->width(), skiaImage->height());
  [view setFrame:starBounds];
  [ratingStars_ addSubview:view];
}

- (void)onOutlineViewRowCountDidChange {
  // Force the outline view to update.
  [outlineView_ reloadData];

  CGFloat totalOffset = 0.0;
  OffsetOutlineViewVerticallyToFitContent(outlineView_, &totalOffset);
  if (totalOffset) {
    NSRect currentRect = [[self view] bounds];
    currentRect.size.height += totalOffset;
    [self updateViewFrame:currentRect];
  }
}

- (id)outlineView:(NSOutlineView*)outlineView
            child:(NSInteger)index
           ofItem:(id)item {
  if (!item)
    return [warnings_ objectAtIndex:index];
  if ([item isKindOfClass:[NSDictionary class]])
    return [[item objectForKey:kChildrenKey] objectAtIndex:index];
  NOTREACHED();
  return nil;
}

- (BOOL)outlineView:(NSOutlineView*)outlineView
   isItemExpandable:(id)item {
  return [self outlineView:outlineView numberOfChildrenOfItem:item] > 0;
}

- (NSInteger)outlineView:(NSOutlineView*)outlineView
  numberOfChildrenOfItem:(id)item {
  if (!item)
    return [warnings_ count];

  if ([item isKindOfClass:[NSDictionary class]])
    return [[item objectForKey:kChildrenKey] count];

  NOTREACHED();
  return 0;
}

- (id)outlineView:(NSOutlineView*)outlineView
    objectValueForTableColumn:(NSTableColumn *)tableColumn
                       byItem:(id)item {
  return [item objectForKey:kTitleKey];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView
   shouldExpandItem:(id)item {
  return HasAttribute(item, kCanExpand);
}

- (void)outlineViewItemDidExpand:sender {
  // Call via run loop to avoid animation glitches.
  [self performSelector:@selector(onOutlineViewRowCountDidChange)
             withObject:nil
             afterDelay:0];
}

- (void)outlineViewItemDidCollapse:sender {
  // Call via run loop to avoid animation glitches.
  [self performSelector:@selector(onOutlineViewRowCountDidChange)
             withObject:nil
             afterDelay:0];
}

- (CGFloat)outlineView:(NSOutlineView *)outlineView
     heightOfRowByItem:(id)item {
  // Prevent reentrancy due to the frameOfCellAtColumn:row: call below.
  if (isComputingRowHeight_)
    return 1;
  base::AutoReset<BOOL> reset(&isComputingRowHeight_, YES);

  NSCell* cell = [[[outlineView_ tableColumns] objectAtIndex:0] dataCell];
  [cell setStringValue:[item objectForKey:kTitleKey]];
  NSRect bounds = NSZeroRect;
  NSInteger row = [outlineView_ rowForItem:item];
  bounds.size.width = NSWidth([outlineView_ frameOfCellAtColumn:0 row:row]);
  bounds.size.height = kMaxControlHeight;

  return [cell cellSizeForBounds:bounds].height;
}

- (BOOL)outlineView:(NSOutlineView*)outlineView
    shouldShowOutlineCellForItem:(id)item {
  return !HasAttribute(item, kNoExpandMarker);
}

- (BOOL)outlineView:(NSOutlineView*)outlineView
    shouldTrackCell:(NSCell*)cell
     forTableColumn:(NSTableColumn*)tableColumn
               item:(id)item {
  return HasAttribute(item, kUseCustomLinkCell);
}

- (void)outlineView:(NSOutlineView*)outlineView
    willDisplayCell:(id)cell
     forTableColumn:(NSTableColumn *)tableColumn
               item:(id)item {
  if (HasAttribute(item, kBoldText))
    [cell setFont:[NSFont boldSystemFontOfSize:12.0]];
  else
    [cell setFont:[NSFont systemFontOfSize:12.0]];
}

- (void)outlineView:(NSOutlineView *)outlineView
    willDisplayOutlineCell:(id)cell
            forTableColumn:(NSTableColumn *)tableColumn
                      item:(id)item {
  if (HasAttribute(item, kNoExpandMarker)) {
    [cell setImagePosition:NSNoImage];
    return;
  }

  if (HasAttribute(item, kUseBullet)) {
    // Replace disclosure triangles with bullet lists for leaf nodes.
    [cell setImagePosition:NSNoImage];
    DrawBulletInFrame([outlineView_ frameOfOutlineCellAtRow:
        [outlineView_ rowForItem:item]]);
    return;
  }

  // Reset image to default value.
  [cell setImagePosition:NSImageOverlaps];
}

- (BOOL)outlineView:(NSOutlineView *)outlineView
   shouldSelectItem:(id)item {
  return false;
}

- (NSCell*)outlineView:(NSOutlineView*)outlineView
    dataCellForTableColumn:(NSTableColumn*)tableColumn
                  item:(id)item {
  if (HasAttribute(item, kUseCustomLinkCell)) {
    base::scoped_nsobject<DetailToggleHyperlinkButtonCell> cell(
        [[DetailToggleHyperlinkButtonCell alloc] initTextCell:@""]);
    [cell setTarget:self];
    [cell setLinkClickedAction:@selector(onToggleDetailsLinkClicked:)];
    [cell setAlignment:NSLeftTextAlignment];
    [cell setUnderlineOnHover:YES];
    [cell setTextColor:
        gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor())];

    size_t detailsIndex =
        [[item objectForKey:kPermissionsDetailIndex] unsignedIntegerValue];
    [cell setPermissionsDetailIndex:detailsIndex];

    ExtensionInstallPrompt::DetailsType detailsType =
        static_cast<ExtensionInstallPrompt::DetailsType>(
            [[item objectForKey:kPermissionsDetailType] unsignedIntegerValue]);
    [cell setPermissionsDetailType:detailsType];

    if (prompt_->GetIsShowingDetails(detailsType, detailsIndex)) {
      [cell setTitle:
          l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_HIDE_DETAILS)];
    } else {
      [cell setTitle:
          l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_SHOW_DETAILS)];
    }

    return cell.autorelease();
  } else {
    return [tableColumn dataCell];
  }
}

- (void)expandItemAndChildren:(id)item {
  if (HasAttribute(item, kAutoExpandCell))
    [outlineView_ expandItem:item expandChildren:NO];

  for (id child in [item objectForKey:kChildrenKey])
    [self expandItemAndChildren:child];
}

- (void)onToggleDetailsLinkClicked:(id)sender {
  size_t index = [sender permissionsDetailIndex];
  ExtensionInstallPrompt::DetailsType type = [sender permissionsDetailType];
  prompt_->SetIsShowingDetails(
      type, index, !prompt_->GetIsShowingDetails(type, index));

  warnings_.reset([[self buildWarnings:*prompt_] retain]);
  [outlineView_ reloadData];

  for (id item in warnings_.get())
    [self expandItemAndChildren:item];
}

- (NSDictionary*)buildItemWithTitle:(NSString*)title
                     cellAttributes:(CellAttributes)cellAttributes
                           children:(NSArray*)children {
  if (!children || ([children count] == 0 && cellAttributes & kUseBullet)) {
    // Add a dummy child even though this is a leaf node. This will cause
    // the outline view to show a disclosure triangle for this item.
    // This is later overriden in willDisplayOutlineCell: to draw a bullet
    // instead. (The bullet could be placed in the title instead but then
    // the bullet wouldn't line up with disclosure triangles of sibling nodes.)
    children = [NSArray arrayWithObject:[NSDictionary dictionary]];
  } else {
    cellAttributes = cellAttributes | kCanExpand;
  }

  return @{
    kTitleKey : title,
    kChildrenKey : children,
    kCellAttributesKey : [NSNumber numberWithInt:cellAttributes],
    kPermissionsDetailIndex : @0ul,
    kPermissionsDetailType : @0ul,
  };
}

- (NSDictionary*)buildDetailToggleItem:(size_t)type
                permissionsDetailIndex:(size_t)index {
  return @{
    kTitleKey : @"",
    kChildrenKey : @[ @{} ],
    kCellAttributesKey : [NSNumber numberWithInt:kUseCustomLinkCell |
                                                 kNoExpandMarker],
    kPermissionsDetailIndex : [NSNumber numberWithUnsignedInteger:index],
    kPermissionsDetailType : [NSNumber numberWithUnsignedInteger:type],
  };
}

- (NSArray*)buildWarnings:(const ExtensionInstallPrompt::Prompt&)prompt {
  NSMutableArray* warnings = [NSMutableArray array];
  NSString* heading = nil;
  NSString* withheldHeading = nil;

  bool hasPermissions = prompt.GetPermissionCount(
      ExtensionInstallPrompt::PermissionsType::ALL_PERMISSIONS);
  CellAttributes warningCellAttributes =
      kBoldText | kAutoExpandCell | kNoExpandMarker;
  if (prompt.ShouldShowPermissions()) {
    NSMutableArray* children = [NSMutableArray array];
    NSMutableArray* withheldChildren = [NSMutableArray array];

    heading =
        [self appendPermissionsForPrompt:prompt
                                withType:ExtensionInstallPrompt::PermissionsType
                                             ::REGULAR_PERMISSIONS
                                children:children];
    withheldHeading =
        [self appendPermissionsForPrompt:prompt
                                withType:ExtensionInstallPrompt::PermissionsType
                                             ::WITHHELD_PERMISSIONS
                                children:withheldChildren];

    if (!hasPermissions) {
      [children addObject:
          [self buildItemWithTitle:
              l10n_util::GetNSString(IDS_EXTENSION_NO_SPECIAL_PERMISSIONS)
                    cellAttributes:kUseBullet
                          children:nil]];
      heading = @"";
    }

    if (heading) {
      [warnings addObject:[self buildItemWithTitle:heading
                                    cellAttributes:warningCellAttributes
                                          children:children]];
    }

    // Add withheld permissions to the prompt if they exist.
    if (withheldHeading) {
      [warnings addObject:[self buildItemWithTitle:withheldHeading
                                    cellAttributes:warningCellAttributes
                                          children:withheldChildren]];
    }
  }

  if (prompt.GetRetainedFileCount() > 0) {
    const ExtensionInstallPrompt::DetailsType type =
        ExtensionInstallPrompt::RETAINED_FILES_DETAILS;

    NSMutableArray* children = [NSMutableArray array];

    if (prompt.GetIsShowingDetails(type, 0)) {
      for (size_t i = 0; i < prompt.GetRetainedFileCount(); ++i) {
        NSString* title = SysUTF16ToNSString(prompt.GetRetainedFile(i));
        [children addObject:[self buildItemWithTitle:title
                                      cellAttributes:kUseBullet
                                            children:nil]];
      }
    }

    NSString* title = SysUTF16ToNSString(prompt.GetRetainedFilesHeading());
    [warnings addObject:[self buildItemWithTitle:title
                                  cellAttributes:warningCellAttributes
                                        children:children]];

    // Add a row for the link.
    [warnings addObject:
        [self buildDetailToggleItem:type permissionsDetailIndex:0]];
  }

  if (prompt.GetRetainedDeviceCount() > 0) {
    const ExtensionInstallPrompt::DetailsType type =
        ExtensionInstallPrompt::RETAINED_DEVICES_DETAILS;

    NSMutableArray* children = [NSMutableArray array];

    if (prompt.GetIsShowingDetails(type, 0)) {
      for (size_t i = 0; i < prompt.GetRetainedDeviceCount(); ++i) {
        NSString* title =
            SysUTF16ToNSString(prompt.GetRetainedDeviceMessageString(i));
        [children addObject:[self buildItemWithTitle:title
                                      cellAttributes:kUseBullet
                                            children:nil]];
      }
    }

    NSString* title = SysUTF16ToNSString(prompt.GetRetainedDevicesHeading());
    [warnings addObject:[self buildItemWithTitle:title
                                  cellAttributes:warningCellAttributes
                                        children:children]];

    // Add a row for the link.
    [warnings
        addObject:[self buildDetailToggleItem:type permissionsDetailIndex:0]];
  }

  return warnings;
}

- (NSString*)
appendPermissionsForPrompt:(const ExtensionInstallPrompt::Prompt&)prompt
                 withType:(ExtensionInstallPrompt::PermissionsType)type
                 children:(NSMutableArray*)children {
  size_t permissionsCount = prompt.GetPermissionCount(type);
  if (permissionsCount == 0)
    return NULL;

  for (size_t i = 0; i < permissionsCount; ++i) {
    NSDictionary* item = [self
        buildItemWithTitle:SysUTF16ToNSString(prompt.GetPermission(i, type))
            cellAttributes:kUseBullet
                  children:nil];
    [children addObject:item];

    // If there are additional details, add them below this item.
    if (!prompt.GetPermissionsDetails(i, type).empty()) {
      if (prompt.GetIsShowingDetails(
              ExtensionInstallPrompt::PERMISSIONS_DETAILS, i)) {
        item =
            [self buildItemWithTitle:SysUTF16ToNSString(
                                         prompt.GetPermissionsDetails(i, type))
                      cellAttributes:kNoExpandMarker
                            children:nil];
        [children addObject:item];
      }

      // Add a row for the link.
      [children addObject:
          [self buildDetailToggleItem:type permissionsDetailIndex:i]];
    }
  }

  return SysUTF16ToNSString(prompt.GetPermissionsHeading(type));
}

- (void)updateViewFrame:(NSRect)frame {
  NSWindow* window = [[self view] window];
  [window setFrame:[window frameRectForContentRect:frame] display:YES];
  [[self view] setFrame:frame];
}

@end


@implementation DetailToggleHyperlinkButtonCell

@synthesize permissionsDetailIndex = permissionsDetailIndex_;
@synthesize permissionsDetailType = permissionsDetailType_;
@synthesize linkClickedAction = linkClickedAction_;

+ (BOOL)prefersTrackingUntilMouseUp {
  return YES;
}

- (NSRect)drawingRectForBounds:(NSRect)rect {
  NSRect rectInset = NSMakeRect(rect.origin.x + kLinkCellPaddingLeft,
                                rect.origin.y,
                                rect.size.width - kLinkCellPaddingLeft,
                                rect.size.height);
  return [super drawingRectForBounds:rectInset];
}

- (NSUInteger)hitTestForEvent:(NSEvent*)event
                       inRect:(NSRect)cellFrame
                       ofView:(NSView*)controlView {
  NSUInteger hitTestResult =
      [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
  if ((hitTestResult & NSCellHitContentArea) != 0)
    hitTestResult |= NSCellHitTrackableArea;
  return hitTestResult;
}

- (void)handleLinkClicked {
  [NSApp sendAction:linkClickedAction_ to:[self target] from:self];
}

- (BOOL)trackMouse:(NSEvent*)event
            inRect:(NSRect)cellFrame
            ofView:(NSView*)controlView
      untilMouseUp:(BOOL)flag {
  BOOL result = YES;
  NSUInteger hitTestResult =
      [self hitTestForEvent:event inRect:cellFrame ofView:controlView];
  if ((hitTestResult & NSCellHitContentArea) != 0) {
    result = [super trackMouse:event
                        inRect:cellFrame
                        ofView:controlView
                  untilMouseUp:flag];
    event = [NSApp currentEvent];
    hitTestResult =
        [self hitTestForEvent:event inRect:cellFrame ofView:controlView];
    if ((hitTestResult & NSCellHitContentArea) != 0)
      [self handleLinkClicked];
  }
  return result;
}

- (NSArray*)accessibilityActionNames {
  return [[super accessibilityActionNames]
      arrayByAddingObject:NSAccessibilityPressAction];
}

- (void)accessibilityPerformAction:(NSString*)action {
  if ([action isEqualToString:NSAccessibilityPressAction])
    [self handleLinkClicked];
  else
    [super accessibilityPerformAction:action];
}

@end