Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / extension_installed_bubble_controller.mm
blob061fc6f86e5f57872d03c57d5056369287829fb4
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 #import "chrome/browser/ui/cocoa/extensions/extension_installed_bubble_controller.h"
7 #include "base/i18n/rtl.h"
8 #include "base/mac/bundle_locations.h"
9 #include "base/mac/mac_util.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/api/commands/command_service.h"
14 #include "chrome/browser/extensions/bundle_installer.h"
15 #include "chrome/browser/extensions/extension_action.h"
16 #include "chrome/browser/extensions/extension_action_manager.h"
17 #include "chrome/browser/extensions/extension_install_ui.h"
18 #include "chrome/browser/signin/signin_promo.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_navigator.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/chrome_style.h"
23 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
24 #include "chrome/browser/ui/cocoa/browser_window_controller.h"
25 #include "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
26 #include "chrome/browser/ui/cocoa/hover_close_button.h"
27 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
28 #include "chrome/browser/ui/cocoa/info_bubble_view.h"
29 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
30 #include "chrome/browser/ui/cocoa/new_tab_button.h"
31 #include "chrome/browser/ui/cocoa/tabs/tab_strip_view.h"
32 #include "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
33 #include "chrome/browser/ui/singleton_tabs.h"
34 #include "chrome/browser/ui/sync/sync_promo_ui.h"
35 #include "chrome/common/extensions/api/commands/commands_handler.h"
36 #include "chrome/common/extensions/api/extension_action/action_info.h"
37 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h"
38 #include "chrome/common/extensions/sync_helper.h"
39 #include "chrome/common/url_constants.h"
40 #include "content/public/browser/notification_details.h"
41 #include "content/public/browser/notification_registrar.h"
42 #include "content/public/browser/notification_source.h"
43 #include "extensions/common/extension.h"
44 #include "grit/chromium_strings.h"
45 #include "grit/generated_resources.h"
46 #import "skia/ext/skia_utils_mac.h"
47 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
48 #include "ui/base/l10n/l10n_util.h"
50 using content::BrowserThread;
51 using extensions::BundleInstaller;
52 using extensions::Extension;
53 using extensions::UnloadedExtensionInfo;
55 // C++ class that receives EXTENSION_LOADED notifications and proxies them back
56 // to |controller|.
57 class ExtensionLoadedNotificationObserver
58     : public content::NotificationObserver {
59  public:
60   ExtensionLoadedNotificationObserver(
61       ExtensionInstalledBubbleController* controller, Profile* profile)
62           : controller_(controller) {
63     registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
64         content::Source<Profile>(profile));
65     registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
66         content::Source<Profile>(profile));
67   }
69  private:
70   // NotificationObserver implementation. Tells the controller to start showing
71   // its window on the main thread when the extension has finished loading.
72   virtual void Observe(
73       int type,
74       const content::NotificationSource& source,
75       const content::NotificationDetails& details) OVERRIDE {
76     if (type == chrome::NOTIFICATION_EXTENSION_LOADED) {
77       const Extension* extension =
78           content::Details<const Extension>(details).ptr();
79       if (extension == [controller_ extension]) {
80         [controller_ performSelectorOnMainThread:@selector(showWindow:)
81                                       withObject:controller_
82                                    waitUntilDone:NO];
83       }
84     } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
85       const Extension* extension =
86           content::Details<const UnloadedExtensionInfo>(details)->extension;
87       if (extension == [controller_ extension]) {
88         [controller_ performSelectorOnMainThread:@selector(extensionUnloaded:)
89                                       withObject:controller_
90                                    waitUntilDone:NO];
91       }
92     } else {
93       NOTREACHED() << "Received unexpected notification.";
94     }
95   }
97   content::NotificationRegistrar registrar_;
98   ExtensionInstalledBubbleController* controller_;  // weak, owns us
101 @implementation ExtensionInstalledBubbleController
103 @synthesize extension = extension_;
104 @synthesize bundle = bundle_;
105 // Exposed for unit test.
106 @synthesize pageActionPreviewShowing = pageActionPreviewShowing_;
108 - (id)initWithParentWindow:(NSWindow*)parentWindow
109                  extension:(const Extension*)extension
110                     bundle:(const BundleInstaller*)bundle
111                    browser:(Browser*)browser
112                       icon:(SkBitmap)icon {
113   NSString* nibName = bundle ? @"ExtensionInstalledBubbleBundle" :
114                                @"ExtensionInstalledBubble";
115   if ((self = [super initWithWindowNibPath:nibName
116                               parentWindow:parentWindow
117                                 anchoredAt:NSZeroPoint])) {
118     extension_ = extension;
119     bundle_ = bundle;
120     DCHECK(browser);
121     browser_ = browser;
122     icon_.reset([gfx::SkBitmapToNSImage(icon) retain]);
123     pageActionPreviewShowing_ = NO;
125     if (bundle_) {
126       type_ = extension_installed_bubble::kBundle;
127     } else if (extension->is_app()) {
128       type_ = extension_installed_bubble::kApp;
129     } else if (!extensions::OmniboxInfo::GetKeyword(extension).empty()) {
130       type_ = extension_installed_bubble::kOmniboxKeyword;
131     } else if (extensions::ActionInfo::GetBrowserActionInfo(extension)) {
132       type_ = extension_installed_bubble::kBrowserAction;
133     } else if (extensions::ActionInfo::GetPageActionInfo(extension) &&
134                extensions::ActionInfo::IsVerboseInstallMessage(extension)) {
135       type_ = extension_installed_bubble::kPageAction;
136     } else {
137       type_ = extension_installed_bubble::kGeneric;
138     }
140     if (type_ == extension_installed_bubble::kBundle) {
141       [self showWindow:self];
142     } else {
143       // Start showing window only after extension has fully loaded.
144       extensionObserver_.reset(new ExtensionLoadedNotificationObserver(
145           self, browser->profile()));
146     }
147   }
148   return self;
151 // Sets |promo_| based on |promoPlaceholder_|, sets |promoPlaceholder_| to nil.
152 - (void)initializeLabel {
153  // Replace the promo placeholder NSTextField with the real label NSTextView.
154  // The former doesn't show links in a nice way, but the latter can't be added
155  // in IB without a containing scroll view, so create the NSTextView
156  // programmatically.
157  promo_.reset([[HyperlinkTextView alloc]
158      initWithFrame:[promoPlaceholder_ frame]]);
159  [promo_.get() setAutoresizingMask:[promoPlaceholder_ autoresizingMask]];
160  [[promoPlaceholder_ superview]
161      replaceSubview:promoPlaceholder_ with:promo_.get()];
162  promoPlaceholder_ = nil;  // Now released.
163  [promo_.get() setDelegate:self];
166 // Returns YES if the sync promo should be shown in the bubble.
167 - (BOOL)showSyncPromo {
168   return extensions::sync_helper::IsSyncableExtension(extension_) &&
169          SyncPromoUI::ShouldShowSyncPromo(browser_->profile());
172 - (void)windowWillClose:(NSNotification*)notification {
173   // Turn off page action icon preview when the window closes, unless we
174   // already removed it when the window resigned key status.
175   [self removePageActionPreviewIfNecessary];
176   extension_ = NULL;
177   browser_ = NULL;
179   [super windowWillClose:notification];
182 // The controller is the delegate of the window, so it receives "did resign
183 // key" notifications.  When key is resigned, close the window.
184 - (void)windowDidResignKey:(NSNotification*)notification {
185   // If the browser window is closing, we need to remove the page action
186   // immediately, otherwise the closing animation may overlap with
187   // browser destruction.
188   [self removePageActionPreviewIfNecessary];
189   [super windowDidResignKey:notification];
192 - (IBAction)closeWindow:(id)sender {
193   DCHECK([[self window] isVisible]);
194   [self close];
197 - (BOOL)textView:(NSTextView*)aTextView
198    clickedOnLink:(id)link
199          atIndex:(NSUInteger)charIndex {
200   DCHECK_EQ(promo_.get(), aTextView);
201   GURL promo_url =
202       signin::GetPromoURL(signin::SOURCE_EXTENSION_INSTALL_BUBBLE, false);
203   chrome::NavigateParams params(
204       chrome::GetSingletonTabNavigateParams(browser_, promo_url));
205   chrome::Navigate(&params);
206   return YES;
209 // Extracted to a function here so that it can be overridden for unit testing.
210 - (void)removePageActionPreviewIfNecessary {
211   if (!extension_ || !pageActionPreviewShowing_)
212     return;
213   ExtensionAction* page_action =
214       extensions::ExtensionActionManager::Get(browser_->profile())->
215       GetPageAction(*extension_);
216   if (!page_action)
217     return;
218   pageActionPreviewShowing_ = NO;
220   BrowserWindowCocoa* window =
221       static_cast<BrowserWindowCocoa*>(browser_->window());
222   LocationBarViewMac* locationBarView =
223       [window->cocoa_controller() locationBarBridge];
224   locationBarView->SetPreviewEnabledPageAction(page_action,
225                                                false);  // disables preview.
228 // The extension installed bubble points at the browser action icon or the
229 // page action icon (shown as a preview), depending on the extension type.
230 // We need to calculate the location of these icons and the size of the
231 // message itself (which varies with the title of the extension) in order
232 // to figure out the origin point for the extension installed bubble.
233 // TODO(mirandac): add framework to easily test extension UI components!
234 - (NSPoint)calculateArrowPoint {
235   BrowserWindowCocoa* window =
236       static_cast<BrowserWindowCocoa*>(browser_->window());
237   NSPoint arrowPoint = NSZeroPoint;
239   switch(type_) {
240     case extension_installed_bubble::kApp: {
241       TabStripView* view = [window->cocoa_controller() tabStripView];
242       NewTabButton* button = [view getNewTabButton];
243       NSRect bounds = [button bounds];
244       NSPoint anchor = NSMakePoint(
245           NSMidX(bounds),
246           NSMaxY(bounds) - extension_installed_bubble::kAppsBubbleArrowOffset);
247       arrowPoint = [button convertPoint:anchor toView:nil];
248       break;
249     }
250     case extension_installed_bubble::kOmniboxKeyword: {
251       LocationBarViewMac* locationBarView =
252           [window->cocoa_controller() locationBarBridge];
253       arrowPoint = locationBarView->GetPageInfoBubblePoint();
254       break;
255     }
256     case extension_installed_bubble::kBrowserAction: {
257       BrowserActionsController* controller =
258           [[window->cocoa_controller() toolbarController]
259               browserActionsController];
260       arrowPoint = [controller popupPointForBrowserAction:extension_];
261       break;
262     }
263     case extension_installed_bubble::kPageAction: {
264       LocationBarViewMac* locationBarView =
265           [window->cocoa_controller() locationBarBridge];
267       ExtensionAction* page_action =
268           extensions::ExtensionActionManager::Get(browser_->profile())->
269           GetPageAction(*extension_);
271       // Tell the location bar to show a preview of the page action icon, which
272       // would ordinarily only be displayed on a page of the appropriate type.
273       // We remove this preview when the extension installed bubble closes.
274       locationBarView->SetPreviewEnabledPageAction(page_action, true);
275       pageActionPreviewShowing_ = YES;
277       // Find the center of the bottom of the page action icon.
278       arrowPoint =
279           locationBarView->GetPageActionBubblePoint(page_action);
280       break;
281     }
282     case extension_installed_bubble::kBundle:
283     case extension_installed_bubble::kGeneric: {
284       // Point at the bottom of the wrench menu.
285       NSView* wrenchButton =
286           [[window->cocoa_controller() toolbarController] wrenchButton];
287       const NSRect bounds = [wrenchButton bounds];
288       NSPoint anchor = NSMakePoint(NSMidX(bounds), NSMaxY(bounds));
289       arrowPoint = [wrenchButton convertPoint:anchor toView:nil];
290       break;
291     }
292     default: {
293       NOTREACHED();
294     }
295   }
296   return arrowPoint;
299 // Override -[BaseBubbleController showWindow:] to tweak bubble location and
300 // set up UI elements.
301 - (void)showWindow:(id)sender {
302   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
304   // Load nib and calculate height based on messages to be shown.
305   NSWindow* window = [self initializeWindow];
306   int newWindowHeight = [self calculateWindowHeight];
307   [self.bubble setFrameSize:NSMakeSize(
308       NSWidth([[window contentView] bounds]), newWindowHeight)];
309   NSSize windowDelta = NSMakeSize(
310       0, newWindowHeight - NSHeight([[window contentView] bounds]));
311   windowDelta = [[window contentView] convertSize:windowDelta toView:nil];
312   NSRect newFrame = [window frame];
313   newFrame.size.height += windowDelta.height;
314   [window setFrame:newFrame display:NO];
316   // Now that we have resized the window, adjust y pos of the messages.
317   [self setMessageFrames:newWindowHeight];
319   // Find window origin, taking into account bubble size and arrow location.
320   self.anchorPoint =
321       [self.parentWindow convertBaseToScreen:[self calculateArrowPoint]];
322   [super showWindow:sender];
325 // Finish nib loading, set arrow location and load icon into window.  This
326 // function is exposed for unit testing.
327 - (NSWindow*)initializeWindow {
328   NSWindow* window = [self window];  // completes nib load
330   if (type_ == extension_installed_bubble::kOmniboxKeyword) {
331     [self.bubble setArrowLocation:info_bubble::kTopLeft];
332   } else {
333     [self.bubble setArrowLocation:info_bubble::kTopRight];
334   }
336   if (type_ == extension_installed_bubble::kBundle)
337     return window;
339   // Set appropriate icon, resizing if necessary.
340   if ([icon_ size].width > extension_installed_bubble::kIconSize) {
341     [icon_ setSize:NSMakeSize(extension_installed_bubble::kIconSize,
342                               extension_installed_bubble::kIconSize)];
343   }
344   [iconImage_ setImage:icon_];
345   [iconImage_ setNeedsDisplay:YES];
346   return window;
349 - (bool)hasActivePageAction:(extensions::Command*)command {
350   extensions::CommandService* command_service =
351       extensions::CommandService::Get(browser_->profile());
352   if (type_ == extension_installed_bubble::kPageAction) {
353     if (extensions::CommandsInfo::GetPageActionCommand(extension_) &&
354         command_service->GetPageActionCommand(
355             extension_->id(),
356             extensions::CommandService::ACTIVE_ONLY,
357             command,
358             NULL)) {
359       return true;
360     }
361   }
363   return false;
366 - (bool)hasActiveBrowserAction:(extensions::Command*)command {
367   extensions::CommandService* command_service =
368       extensions::CommandService::Get(browser_->profile());
369   if (type_ == extension_installed_bubble::kBrowserAction) {
370     if (extensions::CommandsInfo::GetBrowserActionCommand(extension_) &&
371         command_service->GetBrowserActionCommand(
372             extension_->id(),
373             extensions::CommandService::ACTIVE_ONLY,
374             command,
375             NULL)) {
376       return true;
377     }
378   }
380   return false;
383 - (NSString*)installMessageForCurrentExtensionAction {
384   if (type_ == extension_installed_bubble::kPageAction) {
385     extensions::Command page_action_command;
386     if ([self hasActivePageAction:&page_action_command]) {
387       return l10n_util::GetNSStringF(
388           IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO_WITH_SHORTCUT,
389           page_action_command.accelerator().GetShortcutText());
390     } else {
391       return l10n_util::GetNSString(
392           IDS_EXTENSION_INSTALLED_PAGE_ACTION_INFO);
393     }
394   } else {
395     CHECK_EQ(extension_installed_bubble::kBrowserAction, type_);
396     extensions::Command browser_action_command;
397     if ([self hasActiveBrowserAction:&browser_action_command]) {
398       return l10n_util::GetNSStringF(
399           IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO_WITH_SHORTCUT,
400           browser_action_command.accelerator().GetShortcutText());
401     } else {
402       return l10n_util::GetNSString(
403           IDS_EXTENSION_INSTALLED_BROWSER_ACTION_INFO);
404     }
405   }
408 // Calculate the height of each install message, resizing messages in their
409 // frames to fit window width.  Return the new window height, based on the
410 // total of all message heights.
411 - (int)calculateWindowHeight {
412   // Adjust the window height to reflect the sum height of all messages
413   // and vertical padding.
414   int newWindowHeight = 2 * extension_installed_bubble::kOuterVerticalMargin;
416   // If type is bundle, list the extensions that were installed and those that
417   // failed.
418   if (type_ == extension_installed_bubble::kBundle) {
419     NSInteger installedListHeight =
420         [self addExtensionList:installedHeadingMsg_
421                       itemsMsg:installedItemsMsg_
422                          state:BundleInstaller::Item::STATE_INSTALLED];
424     NSInteger failedListHeight =
425         [self addExtensionList:failedHeadingMsg_
426                       itemsMsg:failedItemsMsg_
427                          state:BundleInstaller::Item::STATE_FAILED];
429     newWindowHeight += installedListHeight + failedListHeight;
431     // Put some space between the lists if both are present.
432     if (installedListHeight > 0 && failedListHeight > 0)
433       newWindowHeight += extension_installed_bubble::kInnerVerticalMargin;
435     return newWindowHeight;
436   }
438   int sync_promo_height = 0;
439   if ([self showSyncPromo]) {
440     // First calculate the height of the sign-in promo.
441     NSFont* font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
443     NSString* link(l10n_util::GetNSStringFWithFixup(
444         IDS_EXTENSION_INSTALLED_SIGNIN_PROMO_LINK,
445         l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)));
446     NSString* message(l10n_util::GetNSStringWithFixup(
447         IDS_EXTENSION_INSTALLED_SIGNIN_PROMO));
449     HyperlinkTextView* view = promo_.get();
450     [view setMessageAndLink:message
451                    withLink:link
452                    atOffset:0
453                        font:font
454                messageColor:[NSColor blackColor]
455                   linkColor:gfx::SkColorToCalibratedNSColor(
456                                 chrome_style::GetLinkColor())];
458     // HACK! The TextView does not report correct height even after you stuff
459     // it with text (it tells you it is single-line even if it is multiline), so
460     // here the hidden howToUse_ TextField is temporarily repurposed to
461     // calculate the correct height for the TextView.
462     [[howToUse_ cell] setAttributedStringValue:[promo_ attributedString]];
463     [GTMUILocalizerAndLayoutTweaker
464           sizeToFitFixedWidthTextField:howToUse_];
465     sync_promo_height = NSHeight([howToUse_ frame]);
466   }
468   // First part of extension installed message, the heading.
469   base::string16 extension_name = base::UTF8ToUTF16(extension_->name().c_str());
470   base::i18n::AdjustStringForLocaleDirection(&extension_name);
471   [heading_ setStringValue:l10n_util::GetNSStringF(
472       IDS_EXTENSION_INSTALLED_HEADING, extension_name)];
473   [GTMUILocalizerAndLayoutTweaker
474       sizeToFitFixedWidthTextField:heading_];
475   newWindowHeight += NSHeight([heading_ frame]) +
476       extension_installed_bubble::kInnerVerticalMargin;
478   // If type is browser/page action, include a special message about them.
479   if (type_ == extension_installed_bubble::kBrowserAction ||
480       type_ == extension_installed_bubble::kPageAction) {
481     [howToUse_ setStringValue:[self
482         installMessageForCurrentExtensionAction]];
483     [howToUse_ setHidden:NO];
484     [[howToUse_ cell]
485         setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
486     [GTMUILocalizerAndLayoutTweaker
487         sizeToFitFixedWidthTextField:howToUse_];
488     newWindowHeight += NSHeight([howToUse_ frame]) +
489         extension_installed_bubble::kInnerVerticalMargin;
490   }
492   // If type is omnibox keyword, include a special message about the keyword.
493   if (type_ == extension_installed_bubble::kOmniboxKeyword) {
494     [howToUse_ setStringValue:l10n_util::GetNSStringF(
495         IDS_EXTENSION_INSTALLED_OMNIBOX_KEYWORD_INFO,
496         base::UTF8ToUTF16(extensions::OmniboxInfo::GetKeyword(extension_)))];
497     [howToUse_ setHidden:NO];
498     [[howToUse_ cell]
499         setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
500     [GTMUILocalizerAndLayoutTweaker
501         sizeToFitFixedWidthTextField:howToUse_];
502     newWindowHeight += NSHeight([howToUse_ frame]) +
503         extension_installed_bubble::kInnerVerticalMargin;
504   }
506   // If type is app, hide howToManage_, and include a "show me" link in the
507   // bubble.
508   if (type_ == extension_installed_bubble::kApp) {
509     [howToManage_ setHidden:YES];
510     [appShortcutLink_ setHidden:NO];
511     newWindowHeight += 2 * extension_installed_bubble::kInnerVerticalMargin;
512     newWindowHeight += NSHeight([appShortcutLink_ frame]);
513   } else {
514     // Second part of extension installed message.
515     [[howToManage_ cell]
516         setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
517     [GTMUILocalizerAndLayoutTweaker
518         sizeToFitFixedWidthTextField:howToManage_];
519     newWindowHeight += NSHeight([howToManage_ frame]);
520   }
522   // Sync sign-in promo, if any.
523   if (sync_promo_height > 0) {
524     NSRect promo_frame = [promo_.get() frame];
525     promo_frame.size.height = sync_promo_height;
526     [promo_.get() setFrame:promo_frame];
527     newWindowHeight += extension_installed_bubble::kInnerVerticalMargin;
528     newWindowHeight += sync_promo_height;
529   }
531   extensions::Command command;
532   if ([self hasActivePageAction:&command] ||
533       [self hasActiveBrowserAction:&command]) {
534     [manageShortcutLink_ setHidden:NO];
535     [[manageShortcutLink_ cell]
536         setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
537     [[manageShortcutLink_ cell]
538         setTextColor:gfx::SkColorToCalibratedNSColor(
539             chrome_style::GetLinkColor())];
540     [GTMUILocalizerAndLayoutTweaker sizeToFitView:manageShortcutLink_];
541     newWindowHeight += extension_installed_bubble::kInnerVerticalMargin;
542     newWindowHeight += NSHeight([manageShortcutLink_ frame]);
543   }
545   return newWindowHeight;
548 - (NSInteger)addExtensionList:(NSTextField*)headingMsg
549                      itemsMsg:(NSTextField*)itemsMsg
550                         state:(BundleInstaller::Item::State)state {
551   base::string16 heading = bundle_->GetHeadingTextFor(state);
552   bool hidden = heading.empty();
553   [headingMsg setHidden:hidden];
554   [itemsMsg setHidden:hidden];
555   if (hidden)
556     return 0;
558   [headingMsg setStringValue:base::SysUTF16ToNSString(heading)];
559   [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:headingMsg];
561   NSMutableString* joinedItems = [NSMutableString string];
562   BundleInstaller::ItemList items = bundle_->GetItemsWithState(state);
563   for (size_t i = 0; i < items.size(); ++i) {
564     if (i > 0)
565       [joinedItems appendString:@"\n"];
566     [joinedItems appendString:base::SysUTF16ToNSString(
567         items[i].GetNameForDisplay())];
568   }
570   [itemsMsg setStringValue:joinedItems];
571   [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:itemsMsg];
573   return NSHeight([headingMsg frame]) +
574       extension_installed_bubble::kInnerVerticalMargin +
575       NSHeight([itemsMsg frame]);
578 // Adjust y-position of messages to sit properly in new window height.
579 - (void)setMessageFrames:(int)newWindowHeight {
580   if (type_ == extension_installed_bubble::kBundle) {
581     // Layout the messages from the bottom up.
582     NSTextField* msgs[] = { failedItemsMsg_, failedHeadingMsg_,
583                             installedItemsMsg_, installedHeadingMsg_ };
584     NSInteger offsetFromBottom = 0;
585     BOOL isFirstVisible = YES;
586     for (size_t i = 0; i < arraysize(msgs); ++i) {
587       if ([msgs[i] isHidden])
588         continue;
590       NSRect frame = [msgs[i] frame];
591       NSInteger margin = isFirstVisible ?
592           extension_installed_bubble::kOuterVerticalMargin :
593           extension_installed_bubble::kInnerVerticalMargin;
595       frame.origin.y = offsetFromBottom + margin;
596       [msgs[i] setFrame:frame];
597       offsetFromBottom += NSHeight(frame) + margin;
599       isFirstVisible = NO;
600     }
602     // Move the close button a bit to vertically align it with the heading.
603     NSInteger closeButtonFudge = 1;
604     NSRect frame = [closeButton_ frame];
605     frame.origin.y = newWindowHeight - (NSHeight(frame) + closeButtonFudge +
606          extension_installed_bubble::kOuterVerticalMargin);
607     [closeButton_ setFrame:frame];
609     return;
610   }
612   NSRect headingFrame = [heading_ frame];
613   headingFrame.origin.y = newWindowHeight - (
614       NSHeight(headingFrame) +
615       extension_installed_bubble::kOuterVerticalMargin);
616   [heading_ setFrame:headingFrame];
618   NSRect howToManageFrame = [howToManage_ frame];
619   if (!extensions::OmniboxInfo::GetKeyword(extension_).empty() ||
620       extensions::ActionInfo::GetBrowserActionInfo(extension_) ||
621       extensions::ActionInfo::IsVerboseInstallMessage(extension_)) {
622     // For browser actions, page actions and omnibox keyword show the
623     // 'how to use' message before the 'how to manage' message.
624     NSRect howToUseFrame = [howToUse_ frame];
625     howToUseFrame.origin.y = headingFrame.origin.y - (
626         NSHeight(howToUseFrame) +
627         extension_installed_bubble::kInnerVerticalMargin);
628     [howToUse_ setFrame:howToUseFrame];
630     howToManageFrame.origin.y = howToUseFrame.origin.y - (
631         NSHeight(howToManageFrame) +
632         extension_installed_bubble::kInnerVerticalMargin);
633   } else {
634     howToManageFrame.origin.y = NSMinY(headingFrame) - (
635         NSHeight(howToManageFrame) +
636         extension_installed_bubble::kInnerVerticalMargin);
637   }
638   [howToManage_ setFrame:howToManageFrame];
640   NSRect frame = howToManageFrame;
641   if ([self showSyncPromo]) {
642     frame = [promo_.get() frame];
643     frame.origin.y = NSMinY(howToManageFrame) -
644         (NSHeight(frame) + extension_installed_bubble::kInnerVerticalMargin);
645     [promo_.get() setFrame:frame];
646   }
648   extensions::Command command;
649   if (![manageShortcutLink_ isHidden]) {
650     NSRect manageShortcutFrame = [manageShortcutLink_ frame];
651     manageShortcutFrame.origin.y = NSMinY(frame) - (
652         NSHeight(manageShortcutFrame) +
653         extension_installed_bubble::kInnerVerticalMargin);
654     // Right-align the link.
655     manageShortcutFrame.origin.x = NSMaxX(frame) -
656                                    NSWidth(manageShortcutFrame);
657     [manageShortcutLink_ setFrame:manageShortcutFrame];
658   }
661 // Exposed for unit testing.
662 - (NSRect)headingFrame {
663   return [heading_ frame];
666 - (NSRect)frameOfHowToUse {
667   return [howToUse_ frame];
670 - (NSRect)frameOfHowToManage {
671   return [howToManage_ frame];
674 - (NSRect)frameOfSigninPromo {
675   return [promo_ frame];
678 - (NSButton*)appInstalledShortcutLink {
679   return appShortcutLink_;
682 - (void)extensionUnloaded:(id)sender {
683   extension_ = NULL;
686 - (IBAction)onManageShortcutClicked:(id)sender {
687   [self close];
688   std::string configure_url = chrome::kChromeUIExtensionsURL;
689   configure_url += chrome::kExtensionConfigureCommandsSubPage;
690   chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams(
691       browser_, GURL(configure_url)));
692   chrome::Navigate(&params);
695 - (IBAction)onAppShortcutClicked:(id)sender {
696   ExtensionInstallUI::OpenAppInstalledUI(browser_->profile(), extension_->id());
699 - (void)awakeFromNib {
700   if (bundle_)
701     return;
702   [self initializeLabel];
705 @end