create_application_shortcut_view.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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#ifndef CHROME_BROWSER_UI_VIEWS_CREATE_APPLICATION_SHORTCUT_VIEW_H_
6#define CHROME_BROWSER_UI_VIEWS_CREATE_APPLICATION_SHORTCUT_VIEW_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "chrome/browser/web_applications/web_app.h"
14#include "ui/views/controls/button/button.h"
15#include "ui/views/window/dialog_delegate.h"
16
17class FaviconDownloadHelper;
18class GURL;
19class Profile;
20class SkBitmap;
21
22namespace content {
23class WebContents;
24}
25
26namespace extensions {
27class Extension;
28}
29
30namespace views {
31class Checkbox;
32class Label;
33}
34
35// CreateShortcutViewCommon implements a dialog that asks user where to create
36// the shortcut for given web app.  There are two variants of this dialog:
37// Shortcuts that load a URL in an app-like window, and shortcuts that load
38// a chrome app (the kind you see under "apps" on the new tabs page) in an app
39// window.  These are implemented as subclasses of CreateShortcutViewCommon.
40class CreateApplicationShortcutView : public views::DialogDelegateView,
41                                      public views::ButtonListener {
42 public:
43  enum DialogLayout {
44    // URL shortcuts have an info frame at the top with a thumbnail, title and
45    // description.
46    DIALOG_LAYOUT_URL_SHORTCUT,
47
48    // App shortcuts don't have an info frame, since they are launched from
49    // places where it's clear what app they are from.
50    DIALOG_LAYOUT_APP_SHORTCUT
51  };
52
53  explicit CreateApplicationShortcutView(Profile* profile);
54  virtual ~CreateApplicationShortcutView();
55
56  // Initialize the controls on the dialog.
57  void InitControls(DialogLayout dialog_layout);
58
59  // Overridden from views::View:
60  virtual gfx::Size GetPreferredSize() const OVERRIDE;
61
62  // Overridden from views::DialogDelegate:
63  virtual base::string16 GetDialogButtonLabel(
64      ui::DialogButton button) const OVERRIDE;
65  virtual bool IsDialogButtonEnabled(ui::DialogButton button) const OVERRIDE;
66  virtual ui::ModalType GetModalType() const OVERRIDE;
67  virtual base::string16 GetWindowTitle() const OVERRIDE;
68  virtual bool Accept() OVERRIDE;
69
70  // Overridden from views::ButtonListener:
71  virtual void ButtonPressed(views::Button* sender,
72                             const ui::Event& event) OVERRIDE;
73
74 protected:
75  // Adds a new check-box as a child to the view.
76  views::Checkbox* AddCheckbox(const base::string16& text, bool checked);
77
78  // Profile in which the shortcuts will be created.
79  Profile* profile_;
80
81  // UI elements on the dialog.
82  // May be NULL if we are not displaying the app's info.
83  views::View* app_info_;
84  views::Label* create_shortcuts_label_;
85  views::Checkbox* desktop_check_box_;
86  views::Checkbox* menu_check_box_;
87  views::Checkbox* quick_launch_check_box_;
88
89  // Target shortcut info.
90  web_app::ShortcutInfo shortcut_info_;
91  // If false, the shortcut will be created in the root level of the Start Menu.
92  bool create_in_chrome_apps_subdir_;
93
94  DISALLOW_COPY_AND_ASSIGN(CreateApplicationShortcutView);
95};
96
97// Create an application shortcut pointing to a URL.
98class CreateUrlApplicationShortcutView : public CreateApplicationShortcutView {
99 public:
100  explicit CreateUrlApplicationShortcutView(content::WebContents* web_contents);
101  virtual ~CreateUrlApplicationShortcutView();
102
103  virtual bool Accept() OVERRIDE;
104
105 private:
106  // Fetch the largest unprocessed icon.
107  // The first largest icon downloaded and decoded successfully will be used.
108  void FetchIcon();
109
110  // Favicon download callback.
111  void DidDownloadFavicon(
112      int requested_size,
113      int id,
114      int http_status_code,
115      const GURL& image_url,
116      const std::vector<SkBitmap>& bitmaps,
117      const std::vector<gfx::Size>& original_bitmap_sizes);
118
119  // The tab whose URL is being turned into an app.
120  content::WebContents* web_contents_;
121
122  // Pending app icon download tracked by us.
123  int pending_download_id_;
124
125  // Unprocessed icons from the WebApplicationInfo passed in.
126  web_app::IconInfoList unprocessed_icons_;
127
128  base::WeakPtrFactory<CreateUrlApplicationShortcutView> weak_ptr_factory_;
129
130  DISALLOW_COPY_AND_ASSIGN(CreateUrlApplicationShortcutView);
131};
132
133// Create an application shortcut pointing to a chrome application.
134class CreateChromeApplicationShortcutView
135    : public CreateApplicationShortcutView {
136 public:
137  CreateChromeApplicationShortcutView(
138      Profile* profile,
139      const extensions::Extension* app,
140      const base::Callback<void(bool)>& close_callback);
141  virtual ~CreateChromeApplicationShortcutView();
142  virtual bool Accept() OVERRIDE;
143  virtual bool Cancel() OVERRIDE;
144
145 private:
146  void OnShortcutInfoLoaded(const web_app::ShortcutInfo& shortcut_info);
147
148  base::Callback<void(bool)> close_callback_;
149
150  base::WeakPtrFactory<CreateChromeApplicationShortcutView> weak_ptr_factory_;
151
152  DISALLOW_COPY_AND_ASSIGN(CreateChromeApplicationShortcutView);
153};
154
155#endif  // CHROME_BROWSER_UI_VIEWS_CREATE_APPLICATION_SHORTCUT_VIEW_H_
156