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

Side by Side Diff: chrome/browser/ui/extensions/extension_action_view_controller.cc

Issue 1152613003: Implement sidebar support for extension action popups (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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
OLDNEW
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/extensions/extension_action_view_controller.h" 5 #include "chrome/browser/ui/extensions/extension_action_view_controller.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/api/commands/command_service.h" 10 #include "chrome/browser/extensions/api/commands/command_service.h"
10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" 11 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
11 #include "chrome/browser/extensions/extension_action.h" 12 #include "chrome/browser/extensions/extension_action.h"
12 #include "chrome/browser/extensions/extension_view.h" 13 #include "chrome/browser/extensions/extension_view.h"
13 #include "chrome/browser/extensions/extension_view_host.h" 14 #include "chrome/browser/extensions/extension_view_host.h"
14 #include "chrome/browser/extensions/extension_view_host_factory.h" 15 #include "chrome/browser/extensions/extension_view_host_factory.h"
15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/sessions/session_tab_helper.h" 17 #include "chrome/browser/sessions/session_tab_helper.h"
18 #include "chrome/browser/sidebar/sidebar_container.h"
19 #include "chrome/browser/sidebar/sidebar_manager.h"
17 #include "chrome/browser/ui/browser.h" 20 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/extensions/accelerator_priority.h" 21 #include "chrome/browser/ui/extensions/accelerator_priority.h"
19 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" 22 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
20 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" 25 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h"
21 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" 26 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
22 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
23 #include "chrome/common/extensions/api/extension_action/action_info.h" 27 #include "chrome/common/extensions/api/extension_action/action_info.h"
28 #include "chrome/common/pref_names.h"
24 #include "extensions/browser/extension_host.h" 29 #include "extensions/browser/extension_host.h"
25 #include "extensions/browser/extension_registry.h" 30 #include "extensions/browser/extension_registry.h"
26 #include "extensions/common/extension.h" 31 #include "extensions/common/extension.h"
27 #include "extensions/common/feature_switch.h" 32 #include "extensions/common/feature_switch.h"
28 #include "extensions/common/manifest_constants.h" 33 #include "extensions/common/manifest_constants.h"
29 #include "ui/gfx/image/image_skia.h" 34 #include "ui/gfx/image/image_skia.h"
30 #include "ui/gfx/image/image_skia_operations.h" 35 #include "ui/gfx/image/image_skia_operations.h"
31 36
32 using extensions::ActionInfo; 37 using extensions::ActionInfo;
33 using extensions::CommandService; 38 using extensions::CommandService;
(...skipping 17 matching lines...) Expand all
51 popup_host_observer_(this), 56 popup_host_observer_(this),
52 weak_factory_(this) { 57 weak_factory_(this) {
53 DCHECK(extension_action); 58 DCHECK(extension_action);
54 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || 59 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE ||
55 extension_action->action_type() == ActionInfo::TYPE_BROWSER); 60 extension_action->action_type() == ActionInfo::TYPE_BROWSER);
56 DCHECK(extension); 61 DCHECK(extension);
57 } 62 }
58 63
59 ExtensionActionViewController::~ExtensionActionViewController() { 64 ExtensionActionViewController::~ExtensionActionViewController() {
60 DCHECK(!is_showing_popup()); 65 DCHECK(!is_showing_popup());
66
67 SidebarManager* sidebar_manager = SidebarManager::GetInstance();
68 sidebar_manager->RemoveObserver(this);
69 for (std::set<content::WebContents*>::iterator it =
70 active_in_webcontents_.begin();
71 it != active_in_webcontents_.end(); ++it)
72 sidebar_manager->HideSidebar(*it, GetId());
61 } 73 }
62 74
63 const std::string& ExtensionActionViewController::GetId() const { 75 const std::string& ExtensionActionViewController::GetId() const {
64 return extension_->id(); 76 return extension_->id();
65 } 77 }
66 78
67 void ExtensionActionViewController::SetDelegate( 79 void ExtensionActionViewController::SetDelegate(
68 ToolbarActionViewDelegate* delegate) { 80 ToolbarActionViewDelegate* delegate) {
69 DCHECK((delegate == nullptr) ^ (view_delegate_ == nullptr)); 81 DCHECK((delegate == nullptr) ^ (view_delegate_ == nullptr));
70 if (delegate) { 82 if (delegate) {
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 } 309 }
298 310
299 bool ExtensionActionViewController::TriggerPopupWithUrl( 311 bool ExtensionActionViewController::TriggerPopupWithUrl(
300 PopupShowAction show_action, 312 PopupShowAction show_action,
301 const GURL& popup_url, 313 const GURL& popup_url,
302 bool grant_tab_permissions) { 314 bool grant_tab_permissions) {
303 if (!ExtensionIsValid()) 315 if (!ExtensionIsValid())
304 return false; 316 return false;
305 317
306 bool already_showing = is_showing_popup(); 318 bool already_showing = is_showing_popup();
319 bool use_sidebar = extension_action_->open_in_sidebar();
307 320
308 // Always hide the current popup, even if it's not owned by this extension. 321 // Always hide the current popup, even if it's not owned by this extension.
309 // Only one popup should be visible at a time. 322 // Only one popup should be visible at a time.
310 HideActivePopup(); 323 if (!use_sidebar)
324 HideActivePopup();
311 325
312 // If we were showing a popup already, then we treat the action to open the 326 // If we were showing a popup already, then we treat the action to open the
313 // same one as a desire to close it (like clicking a menu button that was 327 // same one as a desire to close it (like clicking a menu button that was
314 // already open). 328 // already open).
315 if (already_showing) 329 if (already_showing)
316 return false; 330 return false;
317 331
332 if (use_sidebar) {
333 SidebarManager* sidebar_manager = SidebarManager::GetInstance();
334 content::WebContents* web_contents =
335 view_delegate_->GetCurrentWebContents();
336
337 if (active_in_webcontents_.find(web_contents) !=
338 active_in_webcontents_.end()) {
339 sidebar_manager->HideSidebar(web_contents, GetId());
340 return false;
341 }
342
343 active_in_webcontents_.insert(web_contents);
344 sidebar_manager->AddObserver(this);
345
346 sidebar_manager->ShowSidebar(web_contents, GetId(), popup_url, browser_);
347 return true;
348 }
349
318 scoped_ptr<extensions::ExtensionViewHost> host( 350 scoped_ptr<extensions::ExtensionViewHost> host(
319 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url, 351 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url,
320 browser_)); 352 browser_));
321 if (!host) 353 if (!host)
322 return false; 354 return false;
323 355
324 popup_host_ = host.get(); 356 popup_host_ = host.get();
325 popup_host_observer_.Add(popup_host_); 357 popup_host_observer_.Add(popup_host_);
326 if (toolbar_actions_bar_) 358 if (toolbar_actions_bar_)
327 toolbar_actions_bar_->SetPopupOwner(this); 359 toolbar_actions_bar_->SetPopupOwner(this);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 popup_host_observer_.Remove(popup_host_); 393 popup_host_observer_.Remove(popup_host_);
362 popup_host_ = nullptr; 394 popup_host_ = nullptr;
363 if (toolbar_actions_bar_) { 395 if (toolbar_actions_bar_) {
364 toolbar_actions_bar_->SetPopupOwner(nullptr); 396 toolbar_actions_bar_->SetPopupOwner(nullptr);
365 if (toolbar_actions_bar_->popped_out_action() == this && 397 if (toolbar_actions_bar_->popped_out_action() == this &&
366 !view_delegate_->IsMenuRunning()) 398 !view_delegate_->IsMenuRunning())
367 toolbar_actions_bar_->UndoPopOut(); 399 toolbar_actions_bar_->UndoPopOut();
368 } 400 }
369 view_delegate_->OnPopupClosed(); 401 view_delegate_->OnPopupClosed();
370 } 402 }
403
404 void ExtensionActionViewController::OnPopupShown(bool grant_tab_permissions) {
405 view_delegate_->OnPopupShown(grant_tab_permissions);
406 }
407
408 void ExtensionActionViewController::OnSidebarHidden(
409 content::WebContents* tab,
410 const std::string& content_id) {
411 if (view_delegate_->GetCurrentWebContents() == tab && content_id == GetId()) {
412 view_delegate_->OnPopupClosed();
413 active_in_webcontents_.erase(
414 active_in_webcontents_.find(view_delegate_->GetCurrentWebContents()));
415 if (active_in_webcontents_.size() == 0) {
416 SidebarManager::GetInstance()->RemoveObserver(this);
417 if (toolbar_actions_bar_) {
418 toolbar_actions_bar_->SetPopupOwner(nullptr);
419 if (toolbar_actions_bar_->popped_out_action() == this &&
420 !view_delegate_->IsMenuRunning())
421 toolbar_actions_bar_->UndoPopOut();
422 }
423 }
424 }
425 }
426
427 void ExtensionActionViewController::OnSidebarSwitched(
428 content::WebContents* old_tab,
429 const std::string& old_content_id,
430 content::WebContents* new_tab,
431 const std::string& new_content_id) {
432 if (browser_->tab_strip_model()->GetIndexOfWebContents(
433 new_tab ? new_tab : old_tab) == TabStripModel::kNoTab)
434 return;
435
436 if (old_content_id == GetId() && old_content_id != new_content_id) {
437 if (toolbar_actions_bar_) {
438 toolbar_actions_bar_->SetPopupOwner(nullptr);
439 if (toolbar_actions_bar_->popped_out_action() == this &&
440 !view_delegate_->IsMenuRunning())
441 toolbar_actions_bar_->UndoPopOut();
442 }
443 view_delegate_->OnPopupClosed();
444 }
445
446 if (view_delegate_->GetCurrentWebContents() == new_tab &&
447 new_content_id == GetId() && old_content_id != new_content_id) {
448 if (toolbar_actions_bar_)
449 toolbar_actions_bar_->SetPopupOwner(this);
450 if (toolbar_actions_bar_ && !toolbar_actions_bar_->IsActionVisible(this) &&
451 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) {
452 platform_delegate_->CloseOverflowMenu();
453 toolbar_actions_bar_->PopOutAction(
454 this, base::Bind(&ExtensionActionViewController::OnPopupShown,
455 weak_factory_.GetWeakPtr(), true));
456 } else {
457 // Without the popup corner arrow indicator, marking the browserAction
458 // icon
459 // is necessary for extension attribution
460 view_delegate_->OnPopupShown(true);
461 }
462 }
463
464 if (old_tab == new_tab && old_content_id == GetId())
465 OnSidebarHidden(old_tab, old_content_id);
466 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698