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 #include "chrome/browser/ui/gtk/script_bubble_gtk.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/i18n/rtl.h" |
| 11 #include "base/string_number_conversions.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_system.h" |
| 14 #include "chrome/browser/extensions/image_loader.h" |
| 15 #include "chrome/browser/extensions/script_bubble_controller.h" |
| 16 #include "chrome/browser/extensions/tab_helper.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
| 19 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/extensions/extension_set.h" |
| 21 #include "chrome/common/url_constants.h" |
| 22 #include "content/public/browser/web_contents.h" |
| 23 #include "grit/generated_resources.h" |
| 24 #include "grit/theme_resources.h" |
| 25 #include "grit/ui_resources.h" |
| 26 #include "ui/base/gtk/gtk_hig_constants.h" |
| 27 #include "ui/base/l10n/l10n_util.h" |
| 28 |
| 29 using content::WebContents; |
| 30 using content::OpenURLParams; |
| 31 using content::Referrer; |
| 32 using extensions::Extension; |
| 33 using extensions::ExtensionSystem; |
| 34 using extensions::TabHelper; |
| 35 using extensions::ImageLoader; |
| 36 |
| 37 namespace { |
| 38 |
| 39 // The currently open app-modal bubble singleton, or NULL if none is open. |
| 40 ScriptBubbleGtk* g_bubble = NULL; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 |
| 45 // static |
| 46 void ScriptBubbleGtk::Show(GtkWidget* anchor, WebContents* web_contents) { |
| 47 if (!g_bubble) |
| 48 g_bubble = new ScriptBubbleGtk(anchor, web_contents); |
| 49 } |
| 50 |
| 51 // static |
| 52 void ScriptBubbleGtk::OnItemLinkClickedThunk(GtkWidget* sender, |
| 53 void* user_data) { |
| 54 g_bubble->OnItemLinkClicked(sender); |
| 55 } |
| 56 |
| 57 ScriptBubbleGtk::ScriptBubbleGtk(GtkWidget* anchor, WebContents* web_contents) |
| 58 : anchor_(anchor), |
| 59 web_contents_(web_contents), |
| 60 profile_(Profile::FromBrowserContext(web_contents_->GetBrowserContext())), |
| 61 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 62 BuildBubble(); |
| 63 } |
| 64 |
| 65 ScriptBubbleGtk::~ScriptBubbleGtk() { |
| 66 } |
| 67 |
| 68 void ScriptBubbleGtk::Close() { |
| 69 if (bubble_) |
| 70 bubble_->Close(); |
| 71 } |
| 72 |
| 73 void ScriptBubbleGtk::BubbleClosing(BubbleGtk* bubble, |
| 74 bool closed_by_escape) { |
| 75 g_bubble = NULL; |
| 76 delete this; |
| 77 } |
| 78 |
| 79 void ScriptBubbleGtk::BuildBubble() { |
| 80 GtkThemeService* theme_provider = GtkThemeService::GetFrom(profile_); |
| 81 |
| 82 GtkWidget* bubble_content = gtk_vbox_new(FALSE, ui::kControlSpacing); |
| 83 gtk_container_set_border_width(GTK_CONTAINER(bubble_content), |
| 84 ui::kContentAreaBorder); |
| 85 |
| 86 extensions::TabHelper* tab_helper = |
| 87 extensions::TabHelper::FromWebContents(web_contents_); |
| 88 const std::set<std::string>& extensions_running_scripts = |
| 89 tab_helper->script_bubble_controller()->extensions_running_scripts(); |
| 90 const ExtensionSet* loaded_extensions = |
| 91 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 92 |
| 93 std::string heading_string = |
| 94 l10n_util::GetStringUTF8(IDS_SCRIPT_BUBBLE_HEADLINE); |
| 95 GtkWidget* heading = gtk_label_new(heading_string.c_str()); |
| 96 gtk_misc_set_alignment(GTK_MISC(heading), 0, 0); |
| 97 gtk_box_pack_start(GTK_BOX(bubble_content), heading, FALSE, FALSE, 0); |
| 98 |
| 99 for (std::set<std::string>::const_iterator iter = |
| 100 extensions_running_scripts.begin(); |
| 101 iter != extensions_running_scripts.end(); ++iter) { |
| 102 const Extension* extension = loaded_extensions->GetByID(*iter); |
| 103 if (!extension) |
| 104 continue; |
| 105 |
| 106 GtkWidget* item = gtk_hbox_new(FALSE, ui::kControlSpacing); |
| 107 gtk_box_pack_start(GTK_BOX(bubble_content), item, FALSE, FALSE, 0); |
| 108 |
| 109 GtkWidget* image = gtk_image_new(); |
| 110 gtk_widget_set_usize(image, 16, 16); |
| 111 gtk_box_pack_start(GTK_BOX(item), image, FALSE, FALSE, 0); |
| 112 |
| 113 // Load the image asynchronously. |
| 114 icon_controls_[extension->id()] = GTK_IMAGE(image); |
| 115 ImageLoader::Get(profile_)->LoadImageAsync( |
| 116 extension, |
| 117 extension->GetIconResource(extension_misc::EXTENSION_ICON_BITTY, |
| 118 ExtensionIconSet::MATCH_EXACTLY), |
| 119 gfx::Size(extension_misc::EXTENSION_ICON_BITTY, |
| 120 extension_misc::EXTENSION_ICON_BITTY), |
| 121 base::Bind(&ScriptBubbleGtk::OnIconLoaded, |
| 122 weak_ptr_factory_.GetWeakPtr(), |
| 123 extension->id())); |
| 124 |
| 125 GtkWidget* link = theme_provider->BuildChromeLinkButton( |
| 126 extension->name().c_str()); |
| 127 gtk_box_pack_start(GTK_BOX(item), link, FALSE, FALSE, 0); |
| 128 link_controls_[GTK_WIDGET(link)] = extension->id(); |
| 129 g_signal_connect(link, "button-press-event", |
| 130 G_CALLBACK(&OnItemLinkClickedThunk), this); |
| 131 } |
| 132 |
| 133 BubbleGtk::ArrowLocationGtk arrow_location = |
| 134 !base::i18n::IsRTL() ? |
| 135 BubbleGtk::ARROW_LOCATION_TOP_RIGHT : |
| 136 BubbleGtk::ARROW_LOCATION_TOP_LEFT; |
| 137 bubble_ = BubbleGtk::Show(anchor_, |
| 138 NULL, |
| 139 bubble_content, |
| 140 arrow_location, |
| 141 BubbleGtk::MATCH_SYSTEM_THEME | |
| 142 BubbleGtk::POPUP_WINDOW | |
| 143 BubbleGtk::GRAB_INPUT, |
| 144 theme_provider, |
| 145 this); |
| 146 } |
| 147 |
| 148 void ScriptBubbleGtk::OnIconLoaded(const std::string& extension_id, |
| 149 const gfx::Image& icon) { |
| 150 gtk_image_set_from_pixbuf( |
| 151 icon_controls_[extension_id], |
| 152 (!icon.IsEmpty() ? icon : |
| 153 GtkThemeService::GetFrom(profile_)->GetImageNamed( |
| 154 IDR_EXTENSIONS_FAVICON)).ToGdkPixbuf()); |
| 155 } |
| 156 |
| 157 void ScriptBubbleGtk::OnItemLinkClicked(GtkWidget* button) { |
| 158 std::string link(chrome::kChromeUIExtensionsURL); |
| 159 link += "?id=" + link_controls_[button]; |
| 160 web_contents_->OpenURL(OpenURLParams(GURL(link), |
| 161 Referrer(), |
| 162 NEW_FOREGROUND_TAB, |
| 163 content::PAGE_TRANSITION_LINK, |
| 164 false)); |
| 165 Close(); |
| 166 } |
OLD | NEW |