Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(970)

Side by Side Diff: chrome/browser/ui/cocoa/web_intent_picker_cocoa.mm

Issue 9148032: [Web Intents] Refactor picker to use WebIntentPickerModel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix copyright year in w_i_b_c_unittest.mm Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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()]];
86 } 104 }
87 105
88 [controller_ setServiceURLs:urlArray]; 106 [controller_ setServiceURLs:urlArray];
89 } 107 }
90 108
91 void WebIntentPickerCocoa::SetServiceIcon(size_t index, const SkBitmap& icon) { 109 void WebIntentPickerCocoa::OnFaviconChanged(WebIntentPickerModel* model,
110 size_t index) {
92 DCHECK(controller_); 111 DCHECK(controller_);
93 if (icon.empty())
94 return;
95 112
96 NSImage* image = gfx::SkBitmapToNSImage(icon); 113 const WebIntentPickerModel::Item& item = model->GetItemAt(index);
114 NSImage* image = item.favicon.ToNSImage();
115
97 [controller_ replaceImageAtIndex:index withImage:image]; 116 [controller_ replaceImageAtIndex:index withImage:image];
98 } 117 }
99 118
100 void WebIntentPickerCocoa::SetDefaultServiceIcon(size_t index) { 119 void WebIntentPickerCocoa::OnInlineDisposition(WebIntentPickerModel* model) {
101 } 120 const WebIntentPickerModel::Item& item = model->GetItemAt(
121 model->inline_disposition_index());
122 const GURL& url = item.url;
sail 2012/01/27 18:43:59 I don't think you need this temp variable
binji 2012/01/27 19:04:20 Done.
102 123
103 void WebIntentPickerCocoa::Close() { 124 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); 125 browser_->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL);
109 inline_disposition_tab_contents_.reset(new TabContentsWrapper(web_contents)); 126 inline_disposition_tab_contents_.reset(new TabContentsWrapper(web_contents));
110 inline_disposition_delegate_.reset(new InlineHtmlContentDelegate); 127 inline_disposition_delegate_.reset(new InlineHtmlContentDelegate);
111 web_contents->SetDelegate(inline_disposition_delegate_.get()); 128 web_contents->SetDelegate(inline_disposition_delegate_.get());
112 129
113 inline_disposition_tab_contents_->web_contents()->GetController().LoadURL( 130 inline_disposition_tab_contents_->web_contents()->GetController().LoadURL(
114 url, 131 url,
115 content::Referrer(), 132 content::Referrer(),
116 content::PAGE_TRANSITION_START_PAGE, 133 content::PAGE_TRANSITION_START_PAGE,
117 std::string()); 134 std::string());
118 135
119 [controller_ setInlineDispositionTabContents: 136 [controller_ setInlineDispositionTabContents:
120 inline_disposition_tab_contents_.get()]; 137 inline_disposition_tab_contents_.get()];
121 return inline_disposition_tab_contents_->web_contents();
122 }
123 138
124 WebIntentPickerCocoa::~WebIntentPickerCocoa() { 139 delegate_->OnInlineDispositionWebContentsCreated(web_contents);
125 } 140 }
126 141
127 void WebIntentPickerCocoa::OnCancelled() { 142 void WebIntentPickerCocoa::OnCancelled() {
128 DCHECK(delegate_); 143 DCHECK(delegate_);
129 delegate_->OnCancelled(); 144 delegate_->OnCancelled();
130 controller_ = NULL; // Controller will be unusable soon, abandon. 145 controller_ = NULL; // Controller will be unusable soon, abandon.
131 } 146 }
132 147
133 void WebIntentPickerCocoa::OnServiceChosen(size_t index) { 148 void WebIntentPickerCocoa::OnServiceChosen(size_t index) {
134 DCHECK(delegate_); 149 DCHECK(delegate_);
135 delegate_->OnServiceChosen(index); 150 const WebIntentPickerModel::Item& item = model_->GetItemAt(index);
151 delegate_->OnServiceChosen(index, item.disposition);
136 } 152 }
137 153
138 void WebIntentPickerCocoa::set_controller( 154 void WebIntentPickerCocoa::set_controller(
139 WebIntentBubbleController* controller) { 155 WebIntentBubbleController* controller) {
140 controller_ = controller; 156 controller_ = controller;
141 } 157 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698