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