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