Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / bookmark_app_helper.h
blob860641a55a680b9978ea94daf80648837b3c0e12
1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_
6 #define CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_
8 #include <map>
9 #include <set>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "chrome/common/web_application_info.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/common/manifest.h"
20 class ExtensionService;
21 class FaviconDownloader;
22 class Profile;
23 class SkBitmap;
25 namespace content {
26 class BrowserContext;
27 class WebContents;
30 namespace extensions {
31 class CrxInstaller;
32 class Extension;
34 // A helper class for creating bookmark apps from a WebContents.
35 class BookmarkAppHelper : public content::NotificationObserver {
36 public:
37 struct BitmapAndSource {
38 BitmapAndSource();
39 BitmapAndSource(const GURL& source_url_p, const SkBitmap& bitmap_p);
40 ~BitmapAndSource();
42 GURL source_url;
43 SkBitmap bitmap;
46 typedef base::Callback<void(const Extension*, const WebApplicationInfo&)>
47 CreateBookmarkAppCallback;
49 // This helper class will create a bookmark app out of |web_app_info| and
50 // install it to |service|. Icons will be downloaded from the URLs in
51 // |web_app_info.icons| using |contents| if |contents| is not NULL.
52 // All existing icons from WebApplicationInfo will also be used. The user
53 // will then be prompted to edit the creation information via a bubble and
54 // will have a chance to cancel the operation.
55 BookmarkAppHelper(Profile* profile,
56 WebApplicationInfo web_app_info,
57 content::WebContents* contents);
58 ~BookmarkAppHelper() override;
60 // Update the given WebApplicationInfo with information from the manifest.
61 static void UpdateWebAppInfoFromManifest(const content::Manifest& manifest,
62 WebApplicationInfo* web_app_info);
64 // This finds the closest not-smaller bitmap in |bitmaps| for each size in
65 // |sizes| and resizes it to that size. This returns a map of sizes to bitmaps
66 // which contains only bitmaps of a size in |sizes| and at most one bitmap of
67 // each size.
68 static std::map<int, BitmapAndSource> ConstrainBitmapsToSizes(
69 const std::vector<BitmapAndSource>& bitmaps,
70 const std::set<int>& sizes);
72 // Adds a square container icon of |output_size| and 2 * |output_size| pixels
73 // to |bitmaps| by drawing the given |letter| into a rounded background of
74 // |color|. For each size, if an icon of the requested size already exists in
75 // |bitmaps|, nothing will happen.
76 static void GenerateIcon(std::map<int, BitmapAndSource>* bitmaps,
77 int output_size,
78 SkColor color,
79 char letter);
81 // Returns true if a bookmark or hosted app from a given URL is already
82 // installed and enabled.
83 static bool BookmarkOrHostedAppInstalled(
84 content::BrowserContext* browser_context, const GURL& url);
86 // Resize icons to the accepted sizes, and generate any that are missing. Does
87 // not update |web_app_info| except to update |generated_icon_color|.
88 static std::map<int, BitmapAndSource> ResizeIconsAndGenerateMissing(
89 std::vector<BitmapAndSource> icons,
90 std::set<int> sizes_to_generate,
91 WebApplicationInfo* web_app_info);
93 // It is important that the linked app information in any extension that
94 // gets created from sync matches the linked app information that came from
95 // sync. If there are any changes, they will be synced back to other devices
96 // and could potentially create a never ending sync cycle.
97 // This function updates |web_app_info| with the image data of any icon from
98 // |bitmap_map| that has a URL and size matching that in |web_app_info|, as
99 // well as adding any new images from |bitmap_map| that have no URL.
100 static void UpdateWebAppIconsWithoutChangingLinks(
101 std::map<int, BookmarkAppHelper::BitmapAndSource> bitmap_map,
102 WebApplicationInfo* web_app_info);
104 // Begins the asynchronous bookmark app creation.
105 void Create(const CreateBookmarkAppCallback& callback);
107 // Begins the asynchronous bookmark app creation from an app banner.
108 void CreateFromAppBanner(const CreateBookmarkAppCallback& callback,
109 const content::Manifest& manifest);
111 private:
112 friend class TestBookmarkAppHelper;
114 // Called by the WebContents when the manifest has been downloaded. If there
115 // is no manifest, or the WebContents is destroyed before the manifest could
116 // be downloaded, this is called with an empty manifest.
117 void OnDidGetManifest(const content::Manifest& manifest);
119 // Performs post icon download tasks including installing the bookmark app.
120 void OnIconsDownloaded(bool success,
121 const std::map<GURL, std::vector<SkBitmap> >& bitmaps);
123 // Called after the bubble has been shown, and the user has either accepted or
124 // the dialog was dismissed.
125 void OnBubbleCompleted(bool user_accepted,
126 const WebApplicationInfo& web_app_info);
128 // Called when the installation of the app is complete to perform the final
129 // installation steps.
130 void FinishInstallation(const Extension* extension);
132 // Overridden from content::NotificationObserver:
133 void Observe(int type,
134 const content::NotificationSource& source,
135 const content::NotificationDetails& details) override;
137 // The profile that the bookmark app is being added to.
138 Profile* profile_;
140 // The web contents that the bookmark app is being created for.
141 content::WebContents* contents_;
143 // The WebApplicationInfo that the bookmark app is being created for.
144 WebApplicationInfo web_app_info_;
146 // Called on app creation or failure.
147 CreateBookmarkAppCallback callback_;
149 // Downloads icons from the given WebApplicationInfo using the given
150 // WebContents.
151 scoped_ptr<FaviconDownloader> favicon_downloader_;
153 // Used to install the created bookmark app.
154 scoped_refptr<extensions::CrxInstaller> crx_installer_;
156 content::NotificationRegistrar registrar_;
159 // Creates or updates a bookmark app from the given |web_app_info|. Icons will
160 // be downloaded from the icon URLs provided in |web_app_info|.
161 void CreateOrUpdateBookmarkApp(ExtensionService* service,
162 WebApplicationInfo* web_app_info);
164 // Retrieves the WebApplicationInfo that represents a given bookmark app.
165 // |callback| will be called with a WebApplicationInfo which is populated with
166 // the extension's details and icons on success and an unpopulated
167 // WebApplicationInfo on failure.
168 void GetWebApplicationInfoFromApp(
169 content::BrowserContext* browser_context,
170 const extensions::Extension* extension,
171 const base::Callback<void(const WebApplicationInfo&)> callback);
173 // Returns whether the given |url| is a valid bookmark app url.
174 bool IsValidBookmarkAppUrl(const GURL& url);
176 } // namespace extensions
178 #endif // CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_