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