OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #import "chrome/browser/ui/cocoa/intents/web_intent_picker_cocoa2.h" | |
6 | |
7 #include <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/mac/foundation_util.h" | |
11 #include "base/message_loop.h" | |
12 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" | |
13 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_wi
ndow.h" | |
14 #import "chrome/browser/ui/cocoa/intents/web_intent_picker_view_controller.h" | |
15 #include "chrome/browser/ui/intents/web_intent_picker_delegate.h" | |
16 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
17 #include "content/public/browser/native_web_keyboard_event.h" | |
18 | |
19 WebIntentPickerCocoa2::WebIntentPickerCocoa2(content::WebContents* web_contents, | |
20 WebIntentPickerDelegate* delegate, | |
21 WebIntentPickerModel* model) | |
22 : web_contents_(web_contents), | |
23 delegate_(delegate), | |
24 model_(model), | |
25 update_pending_(false), | |
26 weak_ptr_factory_(this) { | |
27 model_->set_observer(this); | |
28 | |
29 view_controller_.reset( | |
30 [[WebIntentPickerViewController alloc] initWithPicker:this]); | |
31 | |
32 scoped_nsobject<NSWindow> window([[ConstrainedWindowCustomWindow alloc] | |
33 initWithContentRect:[[view_controller_ view] bounds]]); | |
34 [[window contentView] addSubview:[view_controller_ view]]; | |
35 [view_controller_ update]; | |
36 | |
37 constrained_window_.reset(new ConstrainedWindowMac2( | |
38 this, web_contents, window)); | |
39 } | |
40 | |
41 WebIntentPickerCocoa2::~WebIntentPickerCocoa2() { | |
42 } | |
43 | |
44 void WebIntentPickerCocoa2::Close() { | |
45 constrained_window_->CloseConstrainedWindow(); | |
46 } | |
47 | |
48 void WebIntentPickerCocoa2::SetActionString(const string16& action) { | |
49 // Ignored. Action string is retrieved from the model. | |
50 } | |
51 | |
52 void WebIntentPickerCocoa2::OnExtensionInstallSuccess(const std::string& id) { | |
53 ScheduleUpdate(); | |
54 } | |
55 | |
56 void WebIntentPickerCocoa2::OnExtensionInstallFailure(const std::string& id) { | |
57 ScheduleUpdate(); | |
58 } | |
59 | |
60 void WebIntentPickerCocoa2::OnShowExtensionInstallDialog( | |
61 content::WebContents* parent_web_contents, | |
62 ExtensionInstallPrompt::Delegate* delegate, | |
63 const ExtensionInstallPrompt::Prompt& prompt) { | |
64 ScheduleUpdate(); | |
65 } | |
66 | |
67 void WebIntentPickerCocoa2::OnInlineDispositionAutoResize( | |
68 const gfx::Size& size) { | |
69 ScheduleUpdate(); | |
70 } | |
71 | |
72 void WebIntentPickerCocoa2::OnInlineDispositionHandleKeyboardEvent( | |
73 const content::NativeWebKeyboardEvent& event) { | |
74 if (event.skip_in_browser || | |
75 event.type == content::NativeWebKeyboardEvent::Char) { | |
76 return; | |
77 } | |
78 ChromeEventProcessingWindow* window = | |
79 base::mac::ObjCCastStrict<ChromeEventProcessingWindow>( | |
80 constrained_window_->GetNativeWindow()); | |
81 [window redispatchKeyEvent:event.os_event]; | |
82 } | |
83 | |
84 void WebIntentPickerCocoa2::OnPendingAsyncCompleted() { | |
85 ScheduleUpdate(); | |
86 } | |
87 | |
88 void WebIntentPickerCocoa2::InvalidateDelegate() { | |
89 delegate_ = NULL; | |
90 } | |
91 | |
92 void WebIntentPickerCocoa2::OnInlineDispositionWebContentsLoaded( | |
93 content::WebContents* web_contents) { | |
94 ScheduleUpdate(); | |
95 } | |
96 | |
97 gfx::Size WebIntentPickerCocoa2::GetMinInlineDispositionSize() { | |
98 return [view_controller_ minimumInlineWebViewSize]; | |
99 } | |
100 | |
101 void WebIntentPickerCocoa2::OnModelChanged(WebIntentPickerModel* model) { | |
102 ScheduleUpdate(); | |
103 } | |
104 | |
105 void WebIntentPickerCocoa2::OnFaviconChanged(WebIntentPickerModel* model, | |
106 size_t index) { | |
107 ScheduleUpdate(); | |
108 } | |
109 | |
110 void WebIntentPickerCocoa2::OnExtensionIconChanged( | |
111 WebIntentPickerModel* model, | |
112 const std::string& extension_id) { | |
113 ScheduleUpdate(); | |
114 } | |
115 | |
116 void WebIntentPickerCocoa2::OnInlineDisposition(const string16& title, | |
117 const GURL& url) { | |
118 ScheduleUpdate(); | |
119 } | |
120 | |
121 void WebIntentPickerCocoa2::OnConstrainedWindowClosed( | |
122 ConstrainedWindowMac2* window) { | |
123 // After the OnClosing call the model may be deleted so unset this reference. | |
124 model_->set_observer(NULL); | |
125 model_ = NULL; | |
126 weak_ptr_factory_.InvalidateWeakPtrs(); | |
127 | |
128 if (delegate_) | |
129 delegate_->OnClosing(); | |
130 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
131 } | |
132 | |
133 void WebIntentPickerCocoa2::ScheduleUpdate() { | |
134 if (update_pending_) | |
135 return; | |
136 update_pending_ = true; | |
137 MessageLoop::current()->PostTask( | |
138 FROM_HERE, | |
139 base::Bind(&WebIntentPickerCocoa2::PerformUpdate, | |
140 weak_ptr_factory_.GetWeakPtr())); | |
141 } | |
142 | |
143 void WebIntentPickerCocoa2::PerformUpdate() { | |
144 update_pending_ = false; | |
145 [view_controller_ update]; | |
146 } | |
OLD | NEW |