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