OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/create_application_shortcut_view.h" | 5 #include "chrome/browser/ui/views/create_application_shortcut_view.h" |
6 | 6 |
7 #include <stddef.h> | |
8 #include <algorithm> | |
9 #include <cmath> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | 7 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "build/build_config.h" | 9 #include "build/build_config.h" |
16 #include "chrome/browser/extensions/tab_helper.h" | |
17 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/browser.h" | |
19 #include "chrome/browser/ui/browser_commands.h" | |
20 #include "chrome/browser/ui/browser_finder.h" | |
21 #include "chrome/browser/ui/views/harmony/chrome_typography.h" | |
22 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | |
23 #include "chrome/browser/web_applications/web_app.h" | |
24 #include "chrome/common/chrome_constants.h" | |
25 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
26 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
27 #include "chrome/grit/locale_settings.h" | |
28 #include "components/constrained_window/constrained_window_views.h" | 13 #include "components/constrained_window/constrained_window_views.h" |
29 #include "components/favicon_base/select_favicon_frames.h" | |
30 #include "components/prefs/pref_service.h" | 14 #include "components/prefs/pref_service.h" |
31 #include "content/public/browser/render_view_host.h" | |
32 #include "content/public/browser/render_widget_host_view.h" | |
33 #include "content/public/browser/web_contents.h" | |
34 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
35 #include "net/base/load_flags.h" | |
36 #include "net/url_request/url_request.h" | |
37 #include "skia/ext/image_operations.h" | |
38 #include "third_party/skia/include/core/SkBitmap.h" | |
39 #include "third_party/skia/include/core/SkRect.h" | |
40 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
41 #include "ui/base/layout.h" | |
42 #include "ui/base/resource/resource_bundle.h" | |
43 #include "ui/gfx/canvas.h" | |
44 #include "ui/gfx/codec/png_codec.h" | |
45 #include "ui/gfx/image/image_family.h" | |
46 #include "ui/gfx/image/image_skia.h" | |
47 #include "ui/views/controls/button/checkbox.h" | 17 #include "ui/views/controls/button/checkbox.h" |
48 #include "ui/views/controls/image_view.h" | |
49 #include "ui/views/controls/label.h" | 18 #include "ui/views/controls/label.h" |
50 #include "ui/views/layout/grid_layout.h" | 19 #include "ui/views/layout/grid_layout.h" |
51 #include "ui/views/layout/layout_constants.h" | 20 #include "ui/views/layout/layout_constants.h" |
52 #include "ui/views/widget/widget.h" | |
53 #include "ui/views/window/dialog_client_view.h" | 21 #include "ui/views/window/dialog_client_view.h" |
54 #include "url/gurl.h" | |
55 | 22 |
56 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
57 #include "base/win/shortcut.h" | 24 #include "base/win/shortcut.h" |
58 #include "base/win/windows_version.h" | 25 #include "base/win/windows_version.h" |
59 #endif // defined(OS_WIN) | 26 #endif // defined(OS_WIN) |
60 | 27 |
61 namespace { | |
62 | |
63 const int kIconPreviewSizePixels = 32; | |
64 | |
65 // AppInfoView shows the application icon and title. | |
66 class AppInfoView : public views::View { | |
67 public: | |
68 AppInfoView(const base::string16& title, | |
69 const base::string16& description, | |
70 const gfx::ImageFamily& icon); | |
71 | |
72 // Updates the icon of the web app. | |
73 void UpdateIcon(const gfx::ImageFamily& image); | |
74 | |
75 // Overridden from views::View: | |
76 void OnPaint(gfx::Canvas* canvas) override; | |
77 | |
78 private: | |
79 // Initializes the controls | |
80 void Init(const base::string16& title, | |
81 const base::string16& description, const gfx::ImageFamily& icon); | |
82 | |
83 // Creates or updates description label. | |
84 void PrepareDescriptionLabel(const base::string16& description); | |
85 | |
86 // Sets up layout manager. | |
87 void SetupLayout(); | |
88 | |
89 views::ImageView* icon_; | |
90 views::Label* title_; | |
91 views::Label* description_; | |
92 }; | |
93 | |
94 AppInfoView::AppInfoView(const base::string16& title, | |
95 const base::string16& description, | |
96 const gfx::ImageFamily& icon) | |
97 : icon_(nullptr), | |
98 title_(nullptr), | |
99 description_(nullptr) { | |
100 Init(title, description, icon); | |
101 } | |
102 | |
103 void AppInfoView::Init(const base::string16& title_text, | |
104 const base::string16& description_text, | |
105 const gfx::ImageFamily& icon) { | |
106 icon_ = new views::ImageView(); | |
107 UpdateIcon(icon); | |
108 icon_->SetImageSize(gfx::Size(kIconPreviewSizePixels, | |
109 kIconPreviewSizePixels)); | |
110 | |
111 title_ = | |
112 new views::Label(title_text, CONTEXT_BODY_TEXT_SMALL, STYLE_EMPHASIZED); | |
113 title_->SetMultiLine(true); | |
114 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
115 | |
116 PrepareDescriptionLabel(description_text); | |
117 | |
118 SetupLayout(); | |
119 } | |
120 | |
121 void AppInfoView::PrepareDescriptionLabel(const base::string16& description) { | |
122 // Do not make space for the description if it is empty. | |
123 if (description.empty()) | |
124 return; | |
125 | |
126 const size_t kMaxLength = 200; | |
127 const base::string16 kEllipsis(base::ASCIIToUTF16(" ... ")); | |
128 | |
129 base::string16 text = description; | |
130 if (text.length() > kMaxLength) { | |
131 text = text.substr(0, kMaxLength); | |
132 text += kEllipsis; | |
133 } | |
134 | |
135 if (description_) { | |
136 description_->SetText(text); | |
137 } else { | |
138 description_ = new views::Label(text); | |
139 description_->SetMultiLine(true); | |
140 description_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
141 } | |
142 } | |
143 | |
144 void AppInfoView::SetupLayout() { | |
145 views::GridLayout* layout = views::GridLayout::CreatePanel(this); | |
146 | |
147 static const int kColumnSetId = 0; | |
148 views::ColumnSet* column_set = layout->AddColumnSet(kColumnSetId); | |
149 column_set->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING, | |
150 20.0f, views::GridLayout::FIXED, | |
151 kIconPreviewSizePixels, kIconPreviewSizePixels); | |
152 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, | |
153 80.0f, views::GridLayout::USE_PREF, 0, 0); | |
154 | |
155 layout->StartRow(0, kColumnSetId); | |
156 layout->AddView(icon_, 1, description_ ? 2 : 1); | |
157 layout->AddView(title_); | |
158 | |
159 if (description_) { | |
160 layout->StartRow(0, kColumnSetId); | |
161 layout->SkipColumns(1); | |
162 layout->AddView(description_); | |
163 } | |
164 } | |
165 | |
166 void AppInfoView::UpdateIcon(const gfx::ImageFamily& image) { | |
167 // Get an icon at the desired preview size (scaling from a larger image if | |
168 // none is available at that exact size). | |
169 gfx::Image icon = | |
170 image.CreateExact(kIconPreviewSizePixels, kIconPreviewSizePixels); | |
171 icon_->SetImage(icon.ToImageSkia()); | |
172 } | |
173 | |
174 void AppInfoView::OnPaint(gfx::Canvas* canvas) { | |
175 gfx::Rect bounds = GetLocalBounds(); | |
176 | |
177 SkRect border_rect = { | |
178 SkIntToScalar(bounds.x()), | |
179 SkIntToScalar(bounds.y()), | |
180 SkIntToScalar(bounds.right()), | |
181 SkIntToScalar(bounds.bottom()) | |
182 }; | |
183 | |
184 cc::PaintFlags border_flags; | |
185 border_flags.setAntiAlias(true); | |
186 border_flags.setARGB(0xFF, 0xC8, 0xC8, 0xC8); | |
187 | |
188 canvas->sk_canvas()->drawRoundRect(border_rect, SkIntToScalar(2), | |
189 SkIntToScalar(2), border_flags); | |
190 | |
191 SkRect inner_rect = { | |
192 border_rect.fLeft + SkDoubleToScalar(0.5), | |
193 border_rect.fTop + SkDoubleToScalar(0.5), | |
194 border_rect.fRight - SkDoubleToScalar(0.5), | |
195 border_rect.fBottom - SkDoubleToScalar(0.5), | |
196 }; | |
197 | |
198 cc::PaintFlags inner_flags; | |
199 inner_flags.setAntiAlias(true); | |
200 inner_flags.setARGB(0xFF, 0xF8, 0xF8, 0xF8); | |
201 canvas->sk_canvas()->drawRoundRect(inner_rect, SkDoubleToScalar(1.5), | |
202 SkDoubleToScalar(1.5), inner_flags); | |
203 } | |
204 | |
205 } // namespace | |
206 | |
207 namespace chrome { | 28 namespace chrome { |
208 | 29 |
209 void ShowCreateWebAppShortcutsDialog(gfx::NativeWindow parent_window, | |
210 content::WebContents* web_contents) { | |
211 constrained_window::CreateBrowserModalDialogViews( | |
212 new CreateUrlApplicationShortcutView(web_contents), | |
213 parent_window)->Show(); | |
214 } | |
215 | |
216 void ShowCreateChromeAppShortcutsDialog( | 30 void ShowCreateChromeAppShortcutsDialog( |
217 gfx::NativeWindow parent_window, | 31 gfx::NativeWindow parent_window, |
218 Profile* profile, | 32 Profile* profile, |
219 const extensions::Extension* app, | 33 const extensions::Extension* app, |
220 const base::Callback<void(bool)>& close_callback) { | 34 const base::Callback<void(bool)>& close_callback) { |
221 constrained_window::CreateBrowserModalDialogViews( | 35 constrained_window::CreateBrowserModalDialogViews( |
222 new CreateChromeApplicationShortcutView(profile, app, close_callback), | 36 new CreateChromeApplicationShortcutView(profile, app, close_callback), |
223 parent_window)->Show(); | 37 parent_window)->Show(); |
224 } | 38 } |
225 | 39 |
226 } // namespace chrome | 40 } // namespace chrome |
227 | 41 |
228 CreateApplicationShortcutView::CreateApplicationShortcutView(Profile* profile) | 42 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView( |
| 43 Profile* profile, |
| 44 const extensions::Extension* app, |
| 45 const base::Callback<void(bool)>& close_callback) |
229 : profile_(profile), | 46 : profile_(profile), |
230 app_info_(nullptr), | 47 close_callback_(close_callback), |
231 create_shortcuts_label_(nullptr), | 48 create_shortcuts_label_(nullptr), |
232 desktop_check_box_(nullptr), | 49 desktop_check_box_(nullptr), |
233 menu_check_box_(nullptr), | 50 menu_check_box_(nullptr), |
234 quick_launch_check_box_(nullptr) { | 51 quick_launch_check_box_(nullptr), |
| 52 weak_ptr_factory_(this) { |
| 53 InitControls(); |
| 54 |
| 55 // Get shortcut and icon information; needed for creating the shortcut. |
| 56 web_app::GetShortcutInfoForApp( |
| 57 app, profile, |
| 58 base::Bind(&CreateChromeApplicationShortcutView::OnAppInfoLoaded, |
| 59 weak_ptr_factory_.GetWeakPtr())); |
235 } | 60 } |
236 | 61 |
237 CreateApplicationShortcutView::~CreateApplicationShortcutView() {} | 62 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {} |
238 | 63 |
239 void CreateApplicationShortcutView::InitControls(DialogLayout dialog_layout) { | 64 void CreateChromeApplicationShortcutView::InitControls() { |
240 if (dialog_layout == DIALOG_LAYOUT_URL_SHORTCUT && shortcut_info_) { | |
241 app_info_ = | |
242 new AppInfoView(shortcut_info_->title, shortcut_info_->description, | |
243 shortcut_info_->favicon); | |
244 } | |
245 create_shortcuts_label_ = new views::Label( | 65 create_shortcuts_label_ = new views::Label( |
246 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL)); | 66 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_LABEL)); |
247 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 67 create_shortcuts_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
248 create_shortcuts_label_->SetMultiLine(true); | 68 create_shortcuts_label_->SetMultiLine(true); |
249 | 69 |
250 desktop_check_box_ = AddCheckbox( | 70 desktop_check_box_ = AddCheckbox( |
251 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX), | 71 l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX), |
252 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop)); | 72 profile_->GetPrefs()->GetBoolean(prefs::kWebAppCreateOnDesktop)); |
253 | 73 |
254 menu_check_box_ = nullptr; | 74 menu_check_box_ = nullptr; |
(...skipping 30 matching lines...) Expand all Loading... |
285 views::ColumnSet* column_set = layout->AddColumnSet(kHeaderColumnSetId); | 105 views::ColumnSet* column_set = layout->AddColumnSet(kHeaderColumnSetId); |
286 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, | 106 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, |
287 100.0f, views::GridLayout::FIXED, 0, 0); | 107 100.0f, views::GridLayout::FIXED, 0, 0); |
288 | 108 |
289 static const int kTableColumnSetId = 1; | 109 static const int kTableColumnSetId = 1; |
290 column_set = layout->AddColumnSet(kTableColumnSetId); | 110 column_set = layout->AddColumnSet(kTableColumnSetId); |
291 column_set->AddPaddingColumn(0, views::kCheckboxIndent); | 111 column_set->AddPaddingColumn(0, views::kCheckboxIndent); |
292 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | 112 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, |
293 100.0f, views::GridLayout::USE_PREF, 0, 0); | 113 100.0f, views::GridLayout::USE_PREF, 0, 0); |
294 | 114 |
295 if (app_info_) { | |
296 layout->StartRow(0, kHeaderColumnSetId); | |
297 layout->AddView(app_info_); | |
298 layout->AddPaddingRow(0, views::kPanelSubVerticalSpacing); | |
299 } | |
300 | |
301 layout->StartRow(0, kHeaderColumnSetId); | 115 layout->StartRow(0, kHeaderColumnSetId); |
302 layout->AddView(create_shortcuts_label_); | 116 layout->AddView(create_shortcuts_label_); |
303 | 117 |
304 layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing); | 118 layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing); |
305 layout->StartRow(0, kTableColumnSetId); | 119 layout->StartRow(0, kTableColumnSetId); |
306 layout->AddView(desktop_check_box_); | 120 layout->AddView(desktop_check_box_); |
307 | 121 |
308 if (menu_check_box_ != nullptr) { | 122 if (menu_check_box_ != nullptr) { |
309 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing); | 123 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing); |
310 layout->StartRow(0, kTableColumnSetId); | 124 layout->StartRow(0, kTableColumnSetId); |
311 layout->AddView(menu_check_box_); | 125 layout->AddView(menu_check_box_); |
312 } | 126 } |
313 | 127 |
314 if (quick_launch_check_box_ != nullptr) { | 128 if (quick_launch_check_box_ != nullptr) { |
315 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing); | 129 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing); |
316 layout->StartRow(0, kTableColumnSetId); | 130 layout->StartRow(0, kTableColumnSetId); |
317 layout->AddView(quick_launch_check_box_); | 131 layout->AddView(quick_launch_check_box_); |
318 } | 132 } |
319 } | 133 } |
320 | 134 |
321 gfx::Size CreateApplicationShortcutView::GetPreferredSize() const { | 135 gfx::Size CreateChromeApplicationShortcutView::GetPreferredSize() const { |
322 // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS? | 136 // TODO(evanm): should this use IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS? |
323 static const int kDialogWidth = 360; | 137 static const int kDialogWidth = 360; |
324 int height = GetLayoutManager()->GetPreferredHeightForWidth(this, | 138 int height = GetLayoutManager()->GetPreferredHeightForWidth(this, |
325 kDialogWidth); | 139 kDialogWidth); |
326 return gfx::Size(kDialogWidth, height); | 140 return gfx::Size(kDialogWidth, height); |
327 } | 141 } |
328 | 142 |
329 base::string16 CreateApplicationShortcutView::GetDialogButtonLabel( | 143 base::string16 CreateChromeApplicationShortcutView::GetDialogButtonLabel( |
330 ui::DialogButton button) const { | 144 ui::DialogButton button) const { |
331 if (button == ui::DIALOG_BUTTON_OK) | 145 if (button == ui::DIALOG_BUTTON_OK) |
332 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT); | 146 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_COMMIT); |
333 return views::DialogDelegateView::GetDialogButtonLabel(button); | 147 return views::DialogDelegateView::GetDialogButtonLabel(button); |
334 } | 148 } |
335 | 149 |
336 bool CreateApplicationShortcutView::IsDialogButtonEnabled( | 150 bool CreateChromeApplicationShortcutView::IsDialogButtonEnabled( |
337 ui::DialogButton button) const { | 151 ui::DialogButton button) const { |
338 if (button == ui::DIALOG_BUTTON_OK) | 152 if (button == ui::DIALOG_BUTTON_OK) |
339 return desktop_check_box_->checked() || | 153 return desktop_check_box_->checked() || |
340 ((menu_check_box_ != nullptr) && | 154 ((menu_check_box_ != nullptr) && |
341 menu_check_box_->checked()) || | 155 menu_check_box_->checked()) || |
342 ((quick_launch_check_box_ != nullptr) && | 156 ((quick_launch_check_box_ != nullptr) && |
343 quick_launch_check_box_->checked()); | 157 quick_launch_check_box_->checked()); |
344 | 158 |
345 return true; | 159 return true; |
346 } | 160 } |
347 | 161 |
348 ui::ModalType CreateApplicationShortcutView::GetModalType() const { | 162 ui::ModalType CreateChromeApplicationShortcutView::GetModalType() const { |
349 return ui::MODAL_TYPE_WINDOW; | 163 return ui::MODAL_TYPE_WINDOW; |
350 } | 164 } |
351 | 165 |
352 base::string16 CreateApplicationShortcutView::GetWindowTitle() const { | 166 base::string16 CreateChromeApplicationShortcutView::GetWindowTitle() const { |
353 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE); | 167 return l10n_util::GetStringUTF16(IDS_CREATE_SHORTCUTS_TITLE); |
354 } | 168 } |
355 | 169 |
356 bool CreateApplicationShortcutView::Accept() { | 170 bool CreateChromeApplicationShortcutView::Accept() { |
357 // NOTE: This procedure will reset |shortcut_info_| to null. | 171 // NOTE: This procedure will reset |shortcut_info_| to null. |
| 172 if (!close_callback_.is_null()) |
| 173 close_callback_.Run(true); |
358 | 174 |
359 // Can happen if the shortcut data is not yet loaded. | 175 // Can happen if the shortcut data is not yet loaded. |
360 if (!shortcut_info_) | 176 if (!shortcut_info_) |
361 return false; | 177 return false; |
362 | 178 |
363 if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)) | 179 if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)) |
364 return false; | 180 return false; |
365 | 181 |
366 web_app::ShortcutLocations creation_locations; | 182 web_app::ShortcutLocations creation_locations; |
367 creation_locations.on_desktop = desktop_check_box_->checked(); | 183 creation_locations.on_desktop = desktop_check_box_->checked(); |
368 if (menu_check_box_ != nullptr && menu_check_box_->checked()) { | 184 if (menu_check_box_ != nullptr && menu_check_box_->checked()) { |
369 creation_locations.applications_menu_location = | 185 creation_locations.applications_menu_location = |
370 create_in_chrome_apps_subdir_ ? | 186 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; |
371 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS : | |
372 web_app::APP_MENU_LOCATION_ROOT; | |
373 } | 187 } |
374 | 188 |
375 #if defined(OS_WIN) | 189 #if defined(OS_WIN) |
376 creation_locations.in_quick_launch_bar = quick_launch_check_box_ == nullptr ? | 190 creation_locations.in_quick_launch_bar = quick_launch_check_box_ == nullptr ? |
377 false : quick_launch_check_box_->checked(); | 191 false : quick_launch_check_box_->checked(); |
378 #elif defined(OS_POSIX) | 192 #elif defined(OS_POSIX) |
379 // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher | 193 // Create shortcut in Mac dock or as Linux (gnome/kde) application launcher |
380 // are not implemented yet. | 194 // are not implemented yet. |
381 creation_locations.in_quick_launch_bar = false; | 195 creation_locations.in_quick_launch_bar = false; |
382 #endif | 196 #endif |
383 | 197 |
384 web_app::CreateShortcutsWithInfo(web_app::SHORTCUT_CREATION_BY_USER, | 198 web_app::CreateShortcutsWithInfo(web_app::SHORTCUT_CREATION_BY_USER, |
385 creation_locations, | 199 creation_locations, |
386 std::move(shortcut_info_)); | 200 std::move(shortcut_info_)); |
387 return true; | 201 return true; |
388 } | 202 } |
389 | 203 |
390 views::Checkbox* CreateApplicationShortcutView::AddCheckbox( | 204 bool CreateChromeApplicationShortcutView::Cancel() { |
391 const base::string16& text, bool checked) { | 205 if (!close_callback_.is_null()) |
392 views::Checkbox* checkbox = new views::Checkbox(text); | 206 close_callback_.Run(false); |
393 checkbox->SetChecked(checked); | 207 return true; |
394 checkbox->set_listener(this); | |
395 return checkbox; | |
396 } | 208 } |
397 | 209 |
398 void CreateApplicationShortcutView::ButtonPressed(views::Button* sender, | 210 void CreateChromeApplicationShortcutView::ButtonPressed( |
399 const ui::Event& event) { | 211 views::Button* sender, |
| 212 const ui::Event& event) { |
400 if (sender == desktop_check_box_) { | 213 if (sender == desktop_check_box_) { |
401 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop, | 214 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateOnDesktop, |
402 desktop_check_box_->checked()); | 215 desktop_check_box_->checked()); |
403 } else if (sender == menu_check_box_) { | 216 } else if (sender == menu_check_box_) { |
404 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu, | 217 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInAppsMenu, |
405 menu_check_box_->checked()); | 218 menu_check_box_->checked()); |
406 } else if (sender == quick_launch_check_box_) { | 219 } else if (sender == quick_launch_check_box_) { |
407 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar, | 220 profile_->GetPrefs()->SetBoolean(prefs::kWebAppCreateInQuickLaunchBar, |
408 quick_launch_check_box_->checked()); | 221 quick_launch_check_box_->checked()); |
409 } | 222 } |
410 | 223 |
411 // When no checkbox is checked we should not have the action button enabled. | 224 // When no checkbox is checked we should not have the action button enabled. |
412 GetDialogClientView()->UpdateDialogButtons(); | 225 GetDialogClientView()->UpdateDialogButtons(); |
413 } | 226 } |
414 | 227 |
415 CreateUrlApplicationShortcutView::CreateUrlApplicationShortcutView( | 228 views::Checkbox* CreateChromeApplicationShortcutView::AddCheckbox( |
416 content::WebContents* web_contents) | 229 const base::string16& text, |
417 : CreateApplicationShortcutView( | 230 bool checked) { |
418 Profile::FromBrowserContext(web_contents->GetBrowserContext())), | 231 views::Checkbox* checkbox = new views::Checkbox(text); |
419 web_contents_(web_contents), | 232 checkbox->SetChecked(checked); |
420 pending_download_id_(-1), | 233 checkbox->set_listener(this); |
421 weak_ptr_factory_(this) { | 234 return checkbox; |
422 shortcut_info_ = web_app::GetShortcutInfoForTab(web_contents_); | |
423 const WebApplicationInfo& app_info = | |
424 extensions::TabHelper::FromWebContents(web_contents_)->web_app_info(); | |
425 if (!app_info.icons.empty()) { | |
426 web_app::GetIconsInfo(app_info, &unprocessed_icons_); | |
427 FetchIcon(); | |
428 } | |
429 | |
430 // Create URL app shortcuts in the top-level menu. | |
431 create_in_chrome_apps_subdir_ = false; | |
432 | |
433 InitControls(DIALOG_LAYOUT_URL_SHORTCUT); | |
434 } | |
435 | |
436 CreateUrlApplicationShortcutView::~CreateUrlApplicationShortcutView() { | |
437 } | |
438 | |
439 bool CreateUrlApplicationShortcutView::Accept() { | |
440 // Get the smallest icon in the icon family (should have only 1). This must be | |
441 // done before the call to Accept(), which will reset |shortcut_info_|. | |
442 DCHECK(shortcut_info_); | |
443 const gfx::Image* icon = shortcut_info_->favicon.GetBest(0, 0); | |
444 SkBitmap bitmap = icon ? icon->AsBitmap() : SkBitmap(); | |
445 | |
446 if (!CreateApplicationShortcutView::Accept()) | |
447 return false; | |
448 | |
449 extensions::TabHelper::FromWebContents(web_contents_)->SetAppIcon(bitmap); | |
450 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | |
451 if (browser) | |
452 chrome::ConvertTabToAppWindow(browser, web_contents_); | |
453 return true; | |
454 } | |
455 | |
456 void CreateUrlApplicationShortcutView::FetchIcon() { | |
457 // There should only be fetch job at a time. | |
458 DCHECK_EQ(-1, pending_download_id_); | |
459 | |
460 if (unprocessed_icons_.empty()) // No icons to fetch. | |
461 return; | |
462 | |
463 int preferred_size = std::max(unprocessed_icons_.back().width, | |
464 unprocessed_icons_.back().height); | |
465 pending_download_id_ = web_contents_->DownloadImage( | |
466 unprocessed_icons_.back().url, | |
467 true, // is a favicon | |
468 0, // no maximum size | |
469 false, // normal cache policy | |
470 base::Bind(&CreateUrlApplicationShortcutView::DidDownloadFavicon, | |
471 weak_ptr_factory_.GetWeakPtr(), | |
472 preferred_size)); | |
473 | |
474 unprocessed_icons_.pop_back(); | |
475 } | |
476 | |
477 void CreateUrlApplicationShortcutView::DidDownloadFavicon( | |
478 int requested_size, | |
479 int id, | |
480 int http_status_code, | |
481 const GURL& image_url, | |
482 const std::vector<SkBitmap>& bitmaps, | |
483 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
484 if (id != pending_download_id_) | |
485 return; | |
486 pending_download_id_ = -1; | |
487 | |
488 // Can happen if the dialog has already been accepted. | |
489 if (!shortcut_info_) | |
490 return; | |
491 | |
492 gfx::ImageSkia image_skia = CreateFaviconImageSkia( | |
493 bitmaps, | |
494 original_bitmap_sizes, | |
495 requested_size, | |
496 nullptr); | |
497 if (!image_skia.isNull()) { | |
498 // As |shortcut_info_| will be passed to the FILE thread upon accepting the | |
499 // dialog, this image must be made read-only and thread-safe. | |
500 image_skia.MakeThreadSafe(); | |
501 shortcut_info_->favicon.Add(image_skia); | |
502 static_cast<AppInfoView*>(app_info_)->UpdateIcon(shortcut_info_->favicon); | |
503 } else { | |
504 FetchIcon(); | |
505 } | |
506 } | |
507 | |
508 CreateChromeApplicationShortcutView::CreateChromeApplicationShortcutView( | |
509 Profile* profile, | |
510 const extensions::Extension* app, | |
511 const base::Callback<void(bool)>& close_callback) | |
512 : CreateApplicationShortcutView(profile), | |
513 close_callback_(close_callback), | |
514 weak_ptr_factory_(this) { | |
515 // Place Chrome app shortcuts in the "Chrome Apps" submenu. | |
516 create_in_chrome_apps_subdir_ = true; | |
517 | |
518 InitControls(DIALOG_LAYOUT_APP_SHORTCUT); | |
519 | |
520 // Get shortcut and icon information; needed for creating the shortcut. | |
521 web_app::GetShortcutInfoForApp( | |
522 app, profile, | |
523 base::Bind(&CreateChromeApplicationShortcutView::OnAppInfoLoaded, | |
524 weak_ptr_factory_.GetWeakPtr())); | |
525 } | |
526 | |
527 CreateChromeApplicationShortcutView::~CreateChromeApplicationShortcutView() {} | |
528 | |
529 bool CreateChromeApplicationShortcutView::Accept() { | |
530 if (!close_callback_.is_null()) | |
531 close_callback_.Run(true); | |
532 return CreateApplicationShortcutView::Accept(); | |
533 } | |
534 | |
535 bool CreateChromeApplicationShortcutView::Cancel() { | |
536 if (!close_callback_.is_null()) | |
537 close_callback_.Run(false); | |
538 return CreateApplicationShortcutView::Cancel(); | |
539 } | 235 } |
540 | 236 |
541 void CreateChromeApplicationShortcutView::OnAppInfoLoaded( | 237 void CreateChromeApplicationShortcutView::OnAppInfoLoaded( |
542 std::unique_ptr<web_app::ShortcutInfo> shortcut_info) { | 238 std::unique_ptr<web_app::ShortcutInfo> shortcut_info) { |
543 shortcut_info_ = std::move(shortcut_info); | 239 shortcut_info_ = std::move(shortcut_info); |
544 } | 240 } |
OLD | NEW |