OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/views/extensions/extension_action_view_controller.h" | 5 #include "chrome/browser/ui/views/extensions/extension_action_view_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
10 #include "chrome/browser/extensions/extension_action.h" | 10 #include "chrome/browser/extensions/extension_action.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 void ExtensionActionViewController::ExecuteActionByUser() { | 65 void ExtensionActionViewController::ExecuteActionByUser() { |
66 ExecuteAction(ExtensionPopup::SHOW, true); | 66 ExecuteAction(ExtensionPopup::SHOW, true); |
67 } | 67 } |
68 | 68 |
69 bool ExtensionActionViewController::ExecuteAction( | 69 bool ExtensionActionViewController::ExecuteAction( |
70 ExtensionPopup::ShowAction show_action, bool grant_tab_permissions) { | 70 ExtensionPopup::ShowAction show_action, bool grant_tab_permissions) { |
71 if (extensions::ExtensionActionAPI::Get(browser_->profile())-> | 71 if (extensions::ExtensionActionAPI::Get(browser_->profile())-> |
72 ExecuteExtensionAction(extension_, browser_, grant_tab_permissions) == | 72 ExecuteExtensionAction(extension_, browser_, grant_tab_permissions) == |
73 ExtensionAction::ACTION_SHOW_POPUP) { | 73 ExtensionAction::ACTION_SHOW_POPUP) { |
74 GURL popup_url = extension_action_->GetPopupUrl(GetCurrentTabId()); | 74 GURL popup_url = extension_action_->GetPopupUrl(GetCurrentTabId()); |
75 if (delegate_->GetPreferredPopupViewController()->ShowPopupWithUrl( | 75 return delegate_->GetPreferredPopupViewController()->ShowPopupWithUrl( |
76 show_action, popup_url)) { | 76 show_action, popup_url, grant_tab_permissions); |
77 delegate_->OnPopupShown(grant_tab_permissions); | |
78 return true; | |
79 } | |
80 } | 77 } |
81 return false; | 78 return false; |
82 } | 79 } |
83 | 80 |
84 void ExtensionActionViewController::HidePopup() { | 81 void ExtensionActionViewController::HidePopup() { |
85 if (popup_) | 82 if (popup_) |
86 CleanupPopup(true); | 83 CleanupPopup(true); |
87 } | 84 } |
88 | 85 |
89 gfx::Image ExtensionActionViewController::GetIcon(int tab_id) { | 86 gfx::Image ExtensionActionViewController::GetIcon(int tab_id) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 ui::MenuSourceType source_type) { | 193 ui::MenuSourceType source_type) { |
197 if (!extension_->ShowConfigureContextMenus()) | 194 if (!extension_->ShowConfigureContextMenus()) |
198 return; | 195 return; |
199 | 196 |
200 DCHECK(!context_menu_owner); | 197 DCHECK(!context_menu_owner); |
201 context_menu_owner = this; | 198 context_menu_owner = this; |
202 | 199 |
203 // We shouldn't have both a popup and a context menu showing. | 200 // We shouldn't have both a popup and a context menu showing. |
204 delegate_->HideActivePopup(); | 201 delegate_->HideActivePopup(); |
205 | 202 |
206 delegate_->OnWillShowContextMenus(); | |
207 | |
208 // Reconstructs the menu every time because the menu's contents are dynamic. | 203 // Reconstructs the menu every time because the menu's contents are dynamic. |
209 scoped_refptr<ExtensionContextMenuModel> context_menu_model( | 204 scoped_refptr<ExtensionContextMenuModel> context_menu_model( |
210 new ExtensionContextMenuModel(extension_, browser_, this)); | 205 new ExtensionContextMenuModel(extension_, browser_, this)); |
211 | 206 |
212 gfx::Point screen_loc; | 207 gfx::Point screen_loc; |
213 views::View::ConvertPointToScreen(delegate_->GetAsView(), &screen_loc); | 208 views::View::ConvertPointToScreen(delegate_->GetAsView(), &screen_loc); |
214 | 209 |
215 int run_types = views::MenuRunner::HAS_MNEMONICS | | 210 int run_types = views::MenuRunner::HAS_MNEMONICS | |
216 views::MenuRunner::CONTEXT_MENU; | 211 views::MenuRunner::CONTEXT_MENU; |
217 if (delegate_->IsShownInMenu()) | 212 if (delegate_->IsShownInMenu()) |
218 run_types |= views::MenuRunner::IS_NESTED; | 213 run_types |= views::MenuRunner::IS_NESTED; |
219 | 214 |
220 views::Widget* parent = delegate_->GetParentForContextMenu(); | 215 views::Widget* parent = delegate_->GetParentForContextMenu(); |
221 | 216 |
222 menu_runner_.reset( | 217 menu_runner_.reset( |
223 new views::MenuRunner(context_menu_model.get(), run_types)); | 218 new views::MenuRunner(context_menu_model.get(), run_types)); |
224 | 219 |
225 if (menu_runner_->RunMenuAt( | 220 if (menu_runner_->RunMenuAt( |
226 parent, | 221 parent, |
227 NULL, | 222 delegate_->GetContextMenuButton(), |
228 gfx::Rect(screen_loc, delegate_->GetAsView()->size()), | 223 gfx::Rect(screen_loc, delegate_->GetAsView()->size()), |
229 views::MENU_ANCHOR_TOPLEFT, | 224 views::MENU_ANCHOR_TOPLEFT, |
230 source_type) == views::MenuRunner::MENU_DELETED) { | 225 source_type) == views::MenuRunner::MENU_DELETED) { |
231 return; | 226 return; |
232 } | 227 } |
233 | 228 |
234 context_menu_owner = NULL; | 229 context_menu_owner = NULL; |
235 menu_runner_.reset(); | 230 menu_runner_.reset(); |
236 delegate_->OnContextMenuDone(); | |
237 | 231 |
238 // If another extension action wants to show its context menu, allow it to. | 232 // If another extension action wants to show its context menu, allow it to. |
239 if (!followup_context_menu_task_.is_null()) { | 233 if (!followup_context_menu_task_.is_null()) { |
240 base::Closure task = followup_context_menu_task_; | 234 base::Closure task = followup_context_menu_task_; |
241 followup_context_menu_task_ = base::Closure(); | 235 followup_context_menu_task_ = base::Closure(); |
242 task.Run(); | 236 task.Run(); |
243 } | 237 } |
244 } | 238 } |
245 | 239 |
246 bool ExtensionActionViewController::ShowPopupWithUrl( | 240 bool ExtensionActionViewController::ShowPopupWithUrl( |
247 ExtensionPopup::ShowAction show_action, const GURL& popup_url) { | 241 ExtensionPopup::ShowAction show_action, |
| 242 const GURL& popup_url, |
| 243 bool grant_tab_permissions) { |
248 // If we're already showing the popup for this browser action, just hide it | 244 // If we're already showing the popup for this browser action, just hide it |
249 // and return. | 245 // and return. |
250 bool already_showing = popup_ != NULL; | 246 bool already_showing = popup_ != NULL; |
251 | 247 |
252 // Always hide the current popup, even if it's not the same. | 248 // Always hide the current popup, even if it's not the same. |
253 // Only one popup should be visible at a time. | 249 // Only one popup should be visible at a time. |
254 delegate_->HideActivePopup(); | 250 delegate_->HideActivePopup(); |
255 | 251 |
256 // Similarly, don't allow a context menu and a popup to be showing | 252 // Similarly, don't allow a context menu and a popup to be showing |
257 // simultaneously. | 253 // simultaneously. |
258 CloseActiveMenuIfNeeded(); | 254 CloseActiveMenuIfNeeded(); |
259 | 255 |
260 if (already_showing) | 256 if (already_showing) |
261 return false; | 257 return false; |
262 | 258 |
263 views::BubbleBorder::Arrow arrow = base::i18n::IsRTL() ? | 259 views::BubbleBorder::Arrow arrow = base::i18n::IsRTL() ? |
264 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | 260 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
265 | 261 |
266 views::View* reference_view = delegate_->GetReferenceViewForPopup(); | 262 views::View* reference_view = delegate_->GetReferenceViewForPopup(); |
267 | 263 |
268 popup_ = ExtensionPopup::ShowPopup( | 264 popup_ = ExtensionPopup::ShowPopup( |
269 popup_url, browser_, reference_view, arrow, show_action); | 265 popup_url, browser_, reference_view, arrow, show_action); |
270 popup_->GetWidget()->AddObserver(this); | 266 popup_->GetWidget()->AddObserver(this); |
271 | 267 |
| 268 delegate_->OnPopupShown(grant_tab_permissions); |
| 269 |
272 return true; | 270 return true; |
273 } | 271 } |
274 | 272 |
275 bool ExtensionActionViewController::GetExtensionCommand( | 273 bool ExtensionActionViewController::GetExtensionCommand( |
276 extensions::Command* command) { | 274 extensions::Command* command) { |
277 DCHECK(command); | 275 DCHECK(command); |
278 CommandService* command_service = CommandService::Get(browser_->profile()); | 276 CommandService* command_service = CommandService::Get(browser_->profile()); |
279 if (extension_action_->action_type() == ActionInfo::TYPE_PAGE) { | 277 if (extension_action_->action_type() == ActionInfo::TYPE_PAGE) { |
280 return command_service->GetPageActionCommand( | 278 return command_service->GetPageActionCommand( |
281 extension_->id(), CommandService::ACTIVE_ONLY, command, NULL); | 279 extension_->id(), CommandService::ACTIVE_ONLY, command, NULL); |
(...skipping 24 matching lines...) Expand all Loading... |
306 } | 304 } |
307 | 305 |
308 void ExtensionActionViewController::CleanupPopup(bool close_widget) { | 306 void ExtensionActionViewController::CleanupPopup(bool close_widget) { |
309 DCHECK(popup_); | 307 DCHECK(popup_); |
310 delegate_->CleanupPopup(); | 308 delegate_->CleanupPopup(); |
311 popup_->GetWidget()->RemoveObserver(this); | 309 popup_->GetWidget()->RemoveObserver(this); |
312 if (close_widget) | 310 if (close_widget) |
313 popup_->GetWidget()->Close(); | 311 popup_->GetWidget()->Close(); |
314 popup_ = NULL; | 312 popup_ = NULL; |
315 } | 313 } |
OLD | NEW |