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, void* user_data) { | |
Finnur
2012/11/29 22:00:16
nit: 80 cols
Aaron Boodman
2012/11/29 22:29:11
Done.
| |
53 g_bubble->OnItemLinkClicked(sender); | |
54 } | |
55 | |
56 ScriptBubbleGtk::ScriptBubbleGtk(GtkWidget* anchor, WebContents* web_contents) | |
57 : anchor_(anchor), | |
58 web_contents_(web_contents), | |
59 profile_(Profile::FromBrowserContext(web_contents_->GetBrowserContext())), | |
60 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
61 BuildBubble(); | |
62 } | |
63 | |
64 ScriptBubbleGtk::~ScriptBubbleGtk() { | |
65 } | |
66 | |
67 void ScriptBubbleGtk::Close() { | |
68 if (bubble_) | |
69 bubble_->Close(); | |
70 } | |
71 | |
72 void ScriptBubbleGtk::BubbleClosing(BubbleGtk* bubble, | |
73 bool closed_by_escape) { | |
74 g_bubble = NULL; | |
75 delete this; | |
76 } | |
77 | |
78 void ScriptBubbleGtk::BuildBubble() { | |
79 GtkThemeService* theme_provider = GtkThemeService::GetFrom(profile_); | |
80 | |
81 GtkWidget* bubble_content = gtk_vbox_new(FALSE, ui::kControlSpacing); | |
82 gtk_container_set_border_width(GTK_CONTAINER(bubble_content), | |
83 ui::kContentAreaBorder); | |
84 | |
85 extensions::TabHelper* tab_helper = | |
86 extensions::TabHelper::FromWebContents(web_contents_); | |
87 const std::set<std::string>& extensions_running_scripts = | |
88 tab_helper->script_bubble_controller()->extensions_running_scripts(); | |
89 const ExtensionSet* loaded_extensions = | |
90 ExtensionSystem::Get(profile_)->extension_service()->extensions(); | |
91 | |
92 std::string heading_string = | |
93 l10n_util::GetStringUTF8(IDS_SCRIPT_BUBBLE_HEADLINE); | |
94 GtkWidget* heading = gtk_label_new(heading_string.c_str()); | |
95 gtk_misc_set_alignment(GTK_MISC(heading), 0, 0); | |
96 gtk_box_pack_start(GTK_BOX(bubble_content), heading, FALSE, FALSE, 0); | |
97 | |
98 for (std::set<std::string>::const_iterator iter = | |
99 extensions_running_scripts.begin(); | |
100 iter != extensions_running_scripts.end(); ++iter) { | |
101 const Extension* extension = loaded_extensions->GetByID(*iter); | |
102 if (!extension) | |
103 continue; | |
104 | |
105 GtkWidget* item = gtk_hbox_new(FALSE, ui::kControlSpacing); | |
106 gtk_box_pack_start(GTK_BOX(bubble_content), item, FALSE, FALSE, 0); | |
107 | |
108 GtkWidget* image = gtk_image_new(); | |
109 gtk_widget_set_usize(image, 16, 16); | |
110 gtk_box_pack_start(GTK_BOX(item), image, FALSE, FALSE, 0); | |
111 | |
112 // Load the image asynchronously. | |
113 icon_controls_[extension->id()] = GTK_IMAGE(image); | |
114 ImageLoader::Get(profile_)->LoadImageAsync( | |
115 extension, | |
116 extension->GetIconResource(extension_misc::EXTENSION_ICON_BITTY, | |
117 ExtensionIconSet::MATCH_EXACTLY), | |
118 gfx::Size(extension_misc::EXTENSION_ICON_BITTY, | |
119 extension_misc::EXTENSION_ICON_BITTY), | |
120 base::Bind(&ScriptBubbleGtk::OnIconLoaded, | |
121 weak_ptr_factory_.GetWeakPtr(), | |
122 extension->id())); | |
123 | |
124 GtkWidget* link = theme_provider->BuildChromeLinkButton( | |
125 extension->name().c_str()); | |
126 gtk_box_pack_start(GTK_BOX(item), link, FALSE, FALSE, 0); | |
127 link_controls_[GTK_WIDGET(link)] = extension->id(); | |
128 g_signal_connect(link, "button-press-event", | |
129 G_CALLBACK(&OnItemLinkClickedThunk), this); | |
130 } | |
131 | |
132 BubbleGtk::ArrowLocationGtk arrow_location = | |
133 !base::i18n::IsRTL() ? | |
134 BubbleGtk::ARROW_LOCATION_TOP_RIGHT : | |
135 BubbleGtk::ARROW_LOCATION_TOP_LEFT; | |
136 bubble_ = BubbleGtk::Show(anchor_, | |
137 NULL, | |
138 bubble_content, | |
139 arrow_location, | |
140 BubbleGtk::MATCH_SYSTEM_THEME | | |
141 BubbleGtk::POPUP_WINDOW | | |
142 BubbleGtk::GRAB_INPUT, | |
143 theme_provider, | |
144 this); | |
145 } | |
146 | |
147 void ScriptBubbleGtk::OnIconLoaded(const std::string& extension_id, | |
148 const gfx::Image& icon) { | |
149 gtk_image_set_from_pixbuf( | |
150 icon_controls_[extension_id], | |
151 (!icon.IsEmpty() ? icon : | |
152 GtkThemeService::GetFrom(profile_)->GetImageNamed( | |
153 IDR_EXTENSIONS_FAVICON)).ToGdkPixbuf()); | |
154 } | |
155 | |
156 void ScriptBubbleGtk::OnItemLinkClicked(GtkWidget* button) { | |
157 std::string link(chrome::kChromeUIExtensionsURL); | |
158 link += "?id=" + link_controls_[button]; | |
159 web_contents_->OpenURL(OpenURLParams(GURL(link), | |
160 Referrer(), | |
161 NEW_FOREGROUND_TAB, | |
162 content::PAGE_TRANSITION_LINK, | |
163 false)); | |
164 Close(); | |
165 } | |
OLD | NEW |