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

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: Move SidebarManager to ExtensionSystem and remove notifications Created 5 years, 6 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"
16 #include "chrome/browser/extensions/sidebar_container.h"
17 #include "chrome/browser/extensions/sidebar_manager.h"
15 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/sessions/session_tab_helper.h" 19 #include "chrome/browser/sessions/session_tab_helper.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 extensions::SidebarManager* sidebar_manager =
68 extensions::SidebarManager::GetFromContext(browser_->profile());
69
70 sidebar_manager->RemoveObserver(this);
71 for (std::set<content::WebContents*>::iterator it =
72 active_in_webcontents_.begin();
73 it != active_in_webcontents_.end(); ++it)
74 sidebar_manager->HideSidebar(*it, GetId());
61 } 75 }
62 76
63 const std::string& ExtensionActionViewController::GetId() const { 77 const std::string& ExtensionActionViewController::GetId() const {
64 return extension_->id(); 78 return extension_->id();
65 } 79 }
66 80
67 void ExtensionActionViewController::SetDelegate( 81 void ExtensionActionViewController::SetDelegate(
68 ToolbarActionViewDelegate* delegate) { 82 ToolbarActionViewDelegate* delegate) {
69 DCHECK((delegate == nullptr) ^ (view_delegate_ == nullptr)); 83 DCHECK((delegate == nullptr) ^ (view_delegate_ == nullptr));
70 if (delegate) { 84 if (delegate) {
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 } 311 }
298 312
299 bool ExtensionActionViewController::TriggerPopupWithUrl( 313 bool ExtensionActionViewController::TriggerPopupWithUrl(
300 PopupShowAction show_action, 314 PopupShowAction show_action,
301 const GURL& popup_url, 315 const GURL& popup_url,
302 bool grant_tab_permissions) { 316 bool grant_tab_permissions) {
303 if (!ExtensionIsValid()) 317 if (!ExtensionIsValid())
304 return false; 318 return false;
305 319
306 bool already_showing = is_showing_popup(); 320 bool already_showing = is_showing_popup();
321 bool use_sidebar = extension_action_->open_in_sidebar();
307 322
308 // Always hide the current popup, even if it's not owned by this extension. 323 // Always hide the current popup, even if it's not owned by this extension.
309 // Only one popup should be visible at a time. 324 // Only one popup should be visible at a time.
310 HideActivePopup(); 325 if (!use_sidebar)
326 HideActivePopup();
311 327
312 // If we were showing a popup already, then we treat the action to open the 328 // 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 329 // same one as a desire to close it (like clicking a menu button that was
314 // already open). 330 // already open).
315 if (already_showing) 331 if (already_showing)
316 return false; 332 return false;
317 333
334 if (use_sidebar) {
335 extensions::SidebarManager* sidebar_manager =
336 extensions::SidebarManager::GetFromContext(browser_->profile());
337
338 content::WebContents* web_contents =
339 view_delegate_->GetCurrentWebContents();
340
341 if (active_in_webcontents_.find(web_contents) !=
342 active_in_webcontents_.end()) {
343 sidebar_manager->HideSidebar(web_contents, GetId());
344 return false;
345 }
346
347 active_in_webcontents_.insert(web_contents);
348 sidebar_manager->AddObserver(this);
349
350 sidebar_manager->ShowSidebar(web_contents, GetId(), popup_url, browser_);
351 return true;
352 }
353
318 scoped_ptr<extensions::ExtensionViewHost> host( 354 scoped_ptr<extensions::ExtensionViewHost> host(
319 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url, 355 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url,
320 browser_)); 356 browser_));
321 if (!host) 357 if (!host)
322 return false; 358 return false;
323 359
324 popup_host_ = host.get(); 360 popup_host_ = host.get();
325 popup_host_observer_.Add(popup_host_); 361 popup_host_observer_.Add(popup_host_);
326 if (toolbar_actions_bar_) 362 if (toolbar_actions_bar_)
327 toolbar_actions_bar_->SetPopupOwner(this); 363 toolbar_actions_bar_->SetPopupOwner(this);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 popup_host_observer_.Remove(popup_host_); 397 popup_host_observer_.Remove(popup_host_);
362 popup_host_ = nullptr; 398 popup_host_ = nullptr;
363 if (toolbar_actions_bar_) { 399 if (toolbar_actions_bar_) {
364 toolbar_actions_bar_->SetPopupOwner(nullptr); 400 toolbar_actions_bar_->SetPopupOwner(nullptr);
365 if (toolbar_actions_bar_->popped_out_action() == this && 401 if (toolbar_actions_bar_->popped_out_action() == this &&
366 !view_delegate_->IsMenuRunning()) 402 !view_delegate_->IsMenuRunning())
367 toolbar_actions_bar_->UndoPopOut(); 403 toolbar_actions_bar_->UndoPopOut();
368 } 404 }
369 view_delegate_->OnPopupClosed(); 405 view_delegate_->OnPopupClosed();
370 } 406 }
407
408 void ExtensionActionViewController::OnPopupShown(bool grant_tab_permissions) {
409 view_delegate_->OnPopupShown(grant_tab_permissions);
410 }
411
412 void ExtensionActionViewController::OnSidebarHidden(
413 content::WebContents* tab,
414 const std::string& content_id) {
415 if (view_delegate_->GetCurrentWebContents() == tab && content_id == GetId()) {
416 view_delegate_->OnPopupClosed();
417 active_in_webcontents_.erase(
418 active_in_webcontents_.find(view_delegate_->GetCurrentWebContents()));
419 if (active_in_webcontents_.size() == 0) {
420 extensions::SidebarManager::GetFromContext(browser_->profile())
421 ->RemoveObserver(this);
422
423 if (toolbar_actions_bar_) {
424 toolbar_actions_bar_->SetPopupOwner(nullptr);
425 if (toolbar_actions_bar_->popped_out_action() == this &&
426 !view_delegate_->IsMenuRunning())
427 toolbar_actions_bar_->UndoPopOut();
428 }
429 }
430 }
431 }
432
433 void ExtensionActionViewController::OnSidebarSwitched(
434 content::WebContents* old_tab,
435 const std::string& old_content_id,
436 content::WebContents* new_tab,
437 const std::string& new_content_id) {
438 if (browser_->tab_strip_model()->GetIndexOfWebContents(
439 new_tab ? new_tab : old_tab) == TabStripModel::kNoTab)
440 return;
441
442 if (old_content_id == GetId() && old_content_id != new_content_id) {
443 if (toolbar_actions_bar_) {
444 toolbar_actions_bar_->SetPopupOwner(nullptr);
445 if (toolbar_actions_bar_->popped_out_action() == this &&
446 !view_delegate_->IsMenuRunning())
447 toolbar_actions_bar_->UndoPopOut();
448 }
449 view_delegate_->OnPopupClosed();
450 }
451
452 if (view_delegate_->GetCurrentWebContents() == new_tab &&
453 new_content_id == GetId() && old_content_id != new_content_id) {
454 if (toolbar_actions_bar_)
455 toolbar_actions_bar_->SetPopupOwner(this);
456 if (toolbar_actions_bar_ && !toolbar_actions_bar_->IsActionVisible(this) &&
457 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) {
458 platform_delegate_->CloseOverflowMenu();
459 toolbar_actions_bar_->PopOutAction(
460 this, base::Bind(&ExtensionActionViewController::OnPopupShown,
461 weak_factory_.GetWeakPtr(), true));
462 } else {
463 // Without the popup corner arrow indicator, marking the browserAction
464 // icon
465 // is necessary for extension attribution
466 view_delegate_->OnPopupShown(true);
467 }
468 }
469
470 if (old_tab == new_tab && old_content_id == GetId())
471 OnSidebarHidden(old_tab, old_content_id);
472 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698