OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #import "chrome/browser/cocoa/extensions/extension_infobar_controller.h" | 5 #import "chrome/browser/cocoa/extensions/extension_infobar_controller.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "app/resource_bundle.h" | 9 #include "app/resource_bundle.h" |
10 #import "chrome/browser/cocoa/animatable_view.h" | 10 #import "chrome/browser/cocoa/animatable_view.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 - (CGFloat)clampedExtensionViewHeight; | 40 - (CGFloat)clampedExtensionViewHeight; |
41 // Adjusts the width of the extension's hosted view to match the window's width | 41 // Adjusts the width of the extension's hosted view to match the window's width |
42 // and sets the proper height for it as well. | 42 // and sets the proper height for it as well. |
43 - (void)adjustExtensionViewSize; | 43 - (void)adjustExtensionViewSize; |
44 // Sets the image to be used in the button on the left side of the infobar. | 44 // Sets the image to be used in the button on the left side of the infobar. |
45 - (void)setButtonImage:(NSImage*)image; | 45 - (void)setButtonImage:(NSImage*)image; |
46 @end | 46 @end |
47 | 47 |
48 // A helper class to bridge the asynchronous Skia bitmap loading mechanism to | 48 // A helper class to bridge the asynchronous Skia bitmap loading mechanism to |
49 // the extension's button. | 49 // the extension's button. |
50 class InfobarBridge : public ExtensionInfoBarDelegate::DelegateObserver, | 50 class InfobarBridge : public ImageLoadingTracker::Observer { |
51 public ImageLoadingTracker::Observer { | |
52 public: | 51 public: |
53 explicit InfobarBridge(ExtensionInfoBarController* owner) | 52 explicit InfobarBridge(ExtensionInfoBarController* owner) |
54 : owner_(owner), | 53 : owner_(owner), |
55 delegate_([owner delegate]->AsExtensionInfoBarDelegate()), | |
56 tracker_(this) { | 54 tracker_(this) { |
57 delegate_->set_observer(this); | |
58 LoadIcon(); | 55 LoadIcon(); |
59 } | 56 } |
60 | 57 |
61 virtual ~InfobarBridge() { | 58 virtual ~InfobarBridge() { |
62 if (delegate_) | |
63 delegate_->set_observer(NULL); | |
64 } | 59 } |
65 | 60 |
66 // Load the Extension's icon image. | 61 // Load the Extension's icon image. |
67 void LoadIcon() { | 62 void LoadIcon() { |
68 const Extension* extension = delegate_->extension_host()->extension(); | 63 const Extension* extension = [owner_ delegate]-> |
| 64 AsExtensionInfoBarDelegate()->extension_host()->extension(); |
69 ExtensionResource icon_resource = extension->GetIconResource( | 65 ExtensionResource icon_resource = extension->GetIconResource( |
70 Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY); | 66 Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY); |
71 if (!icon_resource.relative_path().empty()) { | 67 if (!icon_resource.relative_path().empty()) { |
72 tracker_.LoadImage(extension, icon_resource, | 68 tracker_.LoadImage(extension, icon_resource, |
73 gfx::Size(Extension::EXTENSION_ICON_BITTY, | 69 gfx::Size(Extension::EXTENSION_ICON_BITTY, |
74 Extension::EXTENSION_ICON_BITTY), | 70 Extension::EXTENSION_ICON_BITTY), |
75 ImageLoadingTracker::DONT_CACHE); | 71 ImageLoadingTracker::DONT_CACHE); |
76 } else { | 72 } else { |
77 OnImageLoaded(NULL, icon_resource, 0); | 73 OnImageLoaded(NULL, icon_resource, 0); |
78 } | 74 } |
79 } | 75 } |
80 | 76 |
81 // ImageLoadingTracker::Observer implementation. | 77 // ImageLoadingTracker::Observer implementation. |
82 // TODO(andybons): The infobar view implementations share a lot of the same | 78 // TODO(andybons): The infobar view implementations share a lot of the same |
83 // code. Come up with a strategy to share amongst them. | 79 // code. Come up with a strategy to share amongst them. |
84 virtual void OnImageLoaded( | 80 virtual void OnImageLoaded(SkBitmap* image, |
85 SkBitmap* image, ExtensionResource resource, int index) { | 81 ExtensionResource resource, |
86 if (!delegate_) | 82 int index) { |
87 return; // The delegate can go away while the image asynchronously loads. | |
88 | |
89 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 83 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
90 | 84 |
91 // Fall back on the default extension icon on failure. | 85 // Fall back on the default extension icon on failure. |
92 SkBitmap* icon; | 86 SkBitmap* icon; |
93 if (!image || image->empty()) | 87 if (!image || image->empty()) |
94 icon = rb.GetBitmapNamed(IDR_EXTENSIONS_SECTION); | 88 icon = rb.GetBitmapNamed(IDR_EXTENSIONS_SECTION); |
95 else | 89 else |
96 icon = image; | 90 icon = image; |
97 | 91 |
98 SkBitmap* drop_image = rb.GetBitmapNamed(IDR_APP_DROPARROW); | 92 SkBitmap* drop_image = rb.GetBitmapNamed(IDR_APP_DROPARROW); |
99 | 93 |
100 const int image_size = Extension::EXTENSION_ICON_BITTY; | 94 const int image_size = Extension::EXTENSION_ICON_BITTY; |
101 scoped_ptr<gfx::CanvasSkia> canvas( | 95 scoped_ptr<gfx::CanvasSkia> canvas( |
102 new gfx::CanvasSkia( | 96 new gfx::CanvasSkia( |
103 image_size + kDropArrowLeftMarginPx + drop_image->width(), | 97 image_size + kDropArrowLeftMarginPx + drop_image->width(), |
104 image_size, false)); | 98 image_size, false)); |
105 canvas->DrawBitmapInt(*icon, | 99 canvas->DrawBitmapInt(*icon, |
106 0, 0, icon->width(), icon->height(), | 100 0, 0, icon->width(), icon->height(), |
107 0, 0, image_size, image_size, | 101 0, 0, image_size, image_size, |
108 false); | 102 false); |
109 canvas->DrawBitmapInt(*drop_image, | 103 canvas->DrawBitmapInt(*drop_image, |
110 image_size + kDropArrowLeftMarginPx, | 104 image_size + kDropArrowLeftMarginPx, |
111 image_size / 2); | 105 image_size / 2); |
112 [owner_ setButtonImage:gfx::SkBitmapToNSImage(canvas->ExtractBitmap())]; | 106 [owner_ setButtonImage:gfx::SkBitmapToNSImage(canvas->ExtractBitmap())]; |
113 } | 107 } |
114 | 108 |
115 // Overridden from ExtensionInfoBarDelegate::DelegateObserver: | |
116 virtual void OnDelegateDeleted() { | |
117 delegate_ = NULL; | |
118 } | |
119 | |
120 private: | 109 private: |
121 // Weak. Owns us. | 110 // Weak. Owns us. |
122 ExtensionInfoBarController* owner_; | 111 ExtensionInfoBarController* owner_; |
123 | 112 |
124 // Weak. | |
125 ExtensionInfoBarDelegate* delegate_; | |
126 | |
127 // Loads the extensions's icon on the file thread. | 113 // Loads the extensions's icon on the file thread. |
128 ImageLoadingTracker tracker_; | 114 ImageLoadingTracker tracker_; |
129 | 115 |
130 DISALLOW_COPY_AND_ASSIGN(InfobarBridge); | 116 DISALLOW_COPY_AND_ASSIGN(InfobarBridge); |
131 }; | 117 }; |
132 | 118 |
133 | 119 |
134 @implementation ExtensionInfoBarController | 120 @implementation ExtensionInfoBarController |
135 | 121 |
136 - (id)initWithDelegate:(InfoBarDelegate*)delegate | 122 - (id)initWithDelegate:(InfoBarDelegate*)delegate |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 | 243 |
258 @end | 244 @end |
259 | 245 |
260 InfoBar* ExtensionInfoBarDelegate::CreateInfoBar() { | 246 InfoBar* ExtensionInfoBarDelegate::CreateInfoBar() { |
261 NSWindow* window = [(NSView*)tab_contents_->GetContentNativeView() window]; | 247 NSWindow* window = [(NSView*)tab_contents_->GetContentNativeView() window]; |
262 ExtensionInfoBarController* controller = | 248 ExtensionInfoBarController* controller = |
263 [[ExtensionInfoBarController alloc] initWithDelegate:this | 249 [[ExtensionInfoBarController alloc] initWithDelegate:this |
264 window:window]; | 250 window:window]; |
265 return new InfoBar(controller); | 251 return new InfoBar(controller); |
266 } | 252 } |
OLD | NEW |