Chromium Code Reviews| 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/web_intent_picker_cocoa.h" | 5 #include "chrome/browser/ui/cocoa/web_intent_picker_cocoa.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_window.h" | 11 #include "chrome/browser/ui/browser_window.h" |
| 12 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 12 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 13 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | 13 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
| 14 #import "chrome/browser/ui/cocoa/web_intent_bubble_controller.h" | 14 #import "chrome/browser/ui/cocoa/web_intent_bubble_controller.h" |
| 15 #include "chrome/browser/ui/intents/web_intent_picker.h" | 15 #include "chrome/browser/ui/intents/web_intent_picker.h" |
| 16 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" | 16 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" |
| 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 #include "grit/ui_resources.h" | |
| 19 #include "skia/ext/skia_utils_mac.h" | 20 #include "skia/ext/skia_utils_mac.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | |
| 22 #include "ui/gfx/image/image.h" | |
| 20 | 23 |
| 21 using content::WebContents; | 24 using content::WebContents; |
| 22 | 25 |
| 23 class InlineHtmlContentDelegate: public content::WebContentsDelegate { | 26 class InlineHtmlContentDelegate: public content::WebContentsDelegate { |
| 24 public: | 27 public: |
| 25 InlineHtmlContentDelegate() {} | 28 InlineHtmlContentDelegate() {} |
| 26 virtual ~InlineHtmlContentDelegate() {} | 29 virtual ~InlineHtmlContentDelegate() {} |
| 27 | 30 |
| 28 virtual bool IsPopupOrPanel( | 31 virtual bool IsPopupOrPanel( |
| 29 const content::WebContents* source) const OVERRIDE { | 32 const content::WebContents* source) const OVERRIDE { |
| 30 return true; | 33 return true; |
| 31 } | 34 } |
| 32 virtual bool ShouldAddNavigationToHistory( | 35 virtual bool ShouldAddNavigationToHistory( |
| 33 const history::HistoryAddPageArgs& add_page_args, | 36 const history::HistoryAddPageArgs& add_page_args, |
| 34 content::NavigationType navigation_type) OVERRIDE { | 37 content::NavigationType navigation_type) OVERRIDE { |
| 35 return false; | 38 return false; |
| 36 } | 39 } |
| 37 | 40 |
| 38 private: | 41 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(InlineHtmlContentDelegate); | 42 DISALLOW_COPY_AND_ASSIGN(InlineHtmlContentDelegate); |
| 40 }; | 43 }; |
| 41 | 44 |
| 42 // static | 45 // static |
| 43 WebIntentPicker* WebIntentPicker::Create(Browser* browser, | 46 WebIntentPicker* WebIntentPicker::Create(Browser* browser, |
| 44 TabContentsWrapper* wrapper, | 47 TabContentsWrapper* wrapper, |
| 45 WebIntentPickerDelegate* delegate) { | 48 WebIntentPickerDelegate* delegate, |
| 46 return new WebIntentPickerCocoa(browser, wrapper, delegate); | 49 WebIntentPickerModel* model) { |
| 50 return new WebIntentPickerCocoa(browser, wrapper, delegate, model); | |
| 47 } | 51 } |
| 48 | 52 |
| 49 WebIntentPickerCocoa::WebIntentPickerCocoa() | 53 WebIntentPickerCocoa::WebIntentPickerCocoa() |
| 50 : delegate_(NULL), browser_(NULL), controller_(NULL) { | 54 : delegate_(NULL), |
| 55 model_(NULL), | |
| 56 browser_(NULL), | |
| 57 controller_(NULL) { | |
| 51 } | 58 } |
| 52 | 59 |
| 53 | 60 |
| 54 WebIntentPickerCocoa::WebIntentPickerCocoa(Browser* browser, | 61 WebIntentPickerCocoa::WebIntentPickerCocoa(Browser* browser, |
| 55 TabContentsWrapper* wrapper, | 62 TabContentsWrapper* wrapper, |
| 56 WebIntentPickerDelegate* delegate) | 63 WebIntentPickerDelegate* delegate, |
| 57 : delegate_(delegate), | 64 WebIntentPickerModel* model) |
| 58 browser_(browser), | 65 : delegate_(delegate), |
| 59 controller_(NULL) { | 66 model_(model), |
| 67 browser_(browser), | |
| 68 controller_(NULL) { | |
| 69 model_->set_observer(this); | |
| 70 | |
| 60 DCHECK(browser); | 71 DCHECK(browser); |
| 61 DCHECK(delegate); | 72 DCHECK(delegate); |
| 62 NSWindow* parentWindow = browser->window()->GetNativeHandle(); | 73 NSWindow* parentWindow = browser->window()->GetNativeHandle(); |
| 63 | 74 |
| 64 // Compute the anchor point, relative to location bar. | 75 // Compute the anchor point, relative to location bar. |
| 65 BrowserWindowController* controller = [parentWindow windowController]; | 76 BrowserWindowController* controller = [parentWindow windowController]; |
| 66 LocationBarViewMac* locationBar = [controller locationBarBridge]; | 77 LocationBarViewMac* locationBar = [controller locationBarBridge]; |
| 67 NSPoint anchor = locationBar->GetPageInfoBubblePoint(); | 78 NSPoint anchor = locationBar->GetPageInfoBubblePoint(); |
| 68 anchor = [browser->window()->GetNativeHandle() convertBaseToScreen:anchor]; | 79 anchor = [browser->window()->GetNativeHandle() convertBaseToScreen:anchor]; |
| 69 | 80 |
| 70 // The controller is deallocated when the window is closed, so no need to | 81 // The controller is deallocated when the window is closed, so no need to |
| 71 // worry about it here. | 82 // worry about it here. |
| 72 [[WebIntentBubbleController alloc] initWithPicker:this | 83 [[WebIntentBubbleController alloc] initWithPicker:this |
| 73 parentWindow:parentWindow | 84 parentWindow:parentWindow |
| 74 anchoredAt:anchor]; | 85 anchoredAt:anchor]; |
| 75 } | 86 } |
| 76 | 87 |
| 77 void WebIntentPickerCocoa::SetServiceURLs(const std::vector<GURL>& urls) { | 88 WebIntentPickerCocoa::~WebIntentPickerCocoa() { |
| 89 if (model_ != NULL) | |
| 90 model_->set_observer(NULL); | |
| 91 } | |
| 92 | |
| 93 void WebIntentPickerCocoa::Close() { | |
| 94 } | |
| 95 | |
| 96 void WebIntentPickerCocoa::OnModelChanged(WebIntentPickerModel* model) { | |
| 78 DCHECK(controller_); | 97 DCHECK(controller_); |
| 79 scoped_nsobject<NSMutableArray> urlArray( | 98 scoped_nsobject<NSMutableArray> urlArray( |
| 80 [[NSMutableArray alloc] initWithCapacity:urls.size()]); | 99 [[NSMutableArray alloc] initWithCapacity:model->GetItemCount()]); |
| 81 | 100 |
| 82 for (std::vector<GURL>::const_iterator iter(urls.begin()); | 101 for (size_t i = 0; i < model->GetItemCount(); ++i) { |
| 83 iter != urls.end(); ++iter) { | 102 const WebIntentPickerModel::Item& item = model->GetItemAt(i); |
| 103 | |
| 84 [urlArray addObject: | 104 [urlArray addObject: |
| 85 [NSString stringWithUTF8String:iter->spec().c_str()]]; | 105 [NSString stringWithUTF8String:item.url.spec().c_str()]]; |
| 86 } | 106 } |
| 87 | 107 |
| 88 [controller_ setServiceURLs:urlArray]; | 108 [controller_ setServiceURLs:urlArray]; |
| 89 } | 109 } |
| 90 | 110 |
| 91 void WebIntentPickerCocoa::SetServiceIcon(size_t index, const SkBitmap& icon) { | 111 void WebIntentPickerCocoa::OnFaviconChanged(WebIntentPickerModel* model, |
| 112 size_t index) { | |
| 92 DCHECK(controller_); | 113 DCHECK(controller_); |
| 93 if (icon.empty()) | |
| 94 return; | |
| 95 | 114 |
| 96 NSImage* image = gfx::SkBitmapToNSImage(icon); | 115 const WebIntentPickerModel::Item& item = model->GetItemAt(index); |
| 116 NSImage* image; | |
| 117 if (item.favicon.get() != NULL) { | |
|
groby-ooo-7-16
2012/01/25 22:03:13
Can skip the "get" part - scoped_ptr has operator!
binji
2012/01/26 00:27:41
Done.
| |
| 118 image = item.favicon->ToNSImage(); | |
| 119 } else { | |
| 120 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
|
groby-ooo-7-16
2012/01/25 22:03:13
I'd suggest moving that to the model instead - tha
binji
2012/01/26 00:27:41
Done.
| |
| 121 image = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON); | |
| 122 } | |
| 123 | |
| 97 [controller_ replaceImageAtIndex:index withImage:image]; | 124 [controller_ replaceImageAtIndex:index withImage:image]; |
| 98 } | 125 } |
| 99 | 126 |
| 100 void WebIntentPickerCocoa::SetDefaultServiceIcon(size_t index) { | 127 void WebIntentPickerCocoa::OnInlineDisposition(WebIntentPickerModel* model) { |
| 101 } | 128 const WebIntentPickerModel::Item& item = model->GetItemAt( |
| 129 model->inline_disposition_index()); | |
| 130 const GURL& url = item.url; | |
| 102 | 131 |
| 103 void WebIntentPickerCocoa::Close() { | 132 content::WebContents* web_contents = content::WebContents::Create( |
| 104 } | |
| 105 | |
| 106 WebContents* WebIntentPickerCocoa::SetInlineDisposition(const GURL& url) { | |
| 107 WebContents* web_contents = WebContents::Create( | |
| 108 browser_->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL); | 133 browser_->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL); |
| 109 inline_disposition_tab_contents_.reset(new TabContentsWrapper(web_contents)); | 134 inline_disposition_tab_contents_.reset(new TabContentsWrapper(web_contents)); |
| 110 inline_disposition_delegate_.reset(new InlineHtmlContentDelegate); | 135 inline_disposition_delegate_.reset(new InlineHtmlContentDelegate); |
| 111 web_contents->SetDelegate(inline_disposition_delegate_.get()); | 136 web_contents->SetDelegate(inline_disposition_delegate_.get()); |
| 112 | 137 |
| 113 inline_disposition_tab_contents_->web_contents()->GetController().LoadURL( | 138 inline_disposition_tab_contents_->web_contents()->GetController().LoadURL( |
| 114 url, | 139 url, |
| 115 content::Referrer(), | 140 content::Referrer(), |
| 116 content::PAGE_TRANSITION_START_PAGE, | 141 content::PAGE_TRANSITION_START_PAGE, |
| 117 std::string()); | 142 std::string()); |
| 118 | 143 |
| 119 [controller_ setInlineDispositionTabContents: | 144 [controller_ setInlineDispositionTabContents: |
| 120 inline_disposition_tab_contents_.get()]; | 145 inline_disposition_tab_contents_.get()]; |
| 121 return inline_disposition_tab_contents_->web_contents(); | |
| 122 } | |
| 123 | 146 |
| 124 WebIntentPickerCocoa::~WebIntentPickerCocoa() { | 147 delegate_->OnInlineDispositionWebContentsCreated(web_contents); |
| 125 } | 148 } |
| 126 | 149 |
| 127 void WebIntentPickerCocoa::OnCancelled() { | 150 void WebIntentPickerCocoa::OnCancelled() { |
| 128 DCHECK(delegate_); | 151 DCHECK(delegate_); |
| 129 delegate_->OnCancelled(); | 152 delegate_->OnCancelled(); |
| 130 controller_ = NULL; // Controller will be unusable soon, abandon. | 153 controller_ = NULL; // Controller will be unusable soon, abandon. |
| 131 } | 154 } |
| 132 | 155 |
| 133 void WebIntentPickerCocoa::OnServiceChosen(size_t index) { | 156 void WebIntentPickerCocoa::OnServiceChosen(size_t index) { |
| 134 DCHECK(delegate_); | 157 DCHECK(delegate_); |
| 135 delegate_->OnServiceChosen(index); | 158 const WebIntentPickerModel::Item& item = model_->GetItemAt(index); |
| 159 | |
|
groby-ooo-7-16
2012/01/25 22:03:13
Nit: delete empty line
binji
2012/01/26 00:27:41
Done.
| |
| 160 delegate_->OnServiceChosen(index, item.disposition); | |
| 136 } | 161 } |
| 137 | 162 |
| 138 void WebIntentPickerCocoa::set_controller( | 163 void WebIntentPickerCocoa::set_controller( |
| 139 WebIntentBubbleController* controller) { | 164 WebIntentBubbleController* controller) { |
| 140 controller_ = controller; | 165 controller_ = controller; |
| 141 } | 166 } |
| OLD | NEW |