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_
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
;
30 namespace extensions
{
34 // A helper class for creating bookmark apps from a WebContents.
35 class BookmarkAppHelper
: public content::NotificationObserver
{
37 struct BitmapAndSource
{
39 BitmapAndSource(const GURL
& source_url_p
, const SkBitmap
& bitmap_p
);
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
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| pixels to |bitmaps| by
73 // drawing the given |letter| into a rounded background of |color|.
74 // Does nothing if an icon of |output_size| already exists in |bitmaps|.
75 static void GenerateIcon(std::map
<int, BitmapAndSource
>* bitmaps
,
80 // Returns true if a bookmark or hosted app from a given URL is already
81 // installed and enabled.
82 static bool BookmarkOrHostedAppInstalled(
83 content::BrowserContext
* browser_context
, const GURL
& url
);
85 // Resize icons to the accepted sizes, and generate any that are missing. Does
86 // not update |web_app_info| except to update |generated_icon_color|.
87 static std::map
<int, BitmapAndSource
> ResizeIconsAndGenerateMissing(
88 std::vector
<BitmapAndSource
> icons
,
89 std::set
<int> sizes_to_generate
,
90 WebApplicationInfo
* web_app_info
);
92 // It is important that the linked app information in any extension that
93 // gets created from sync matches the linked app information that came from
94 // sync. If there are any changes, they will be synced back to other devices
95 // and could potentially create a never ending sync cycle.
96 // This function updates |web_app_info| with the image data of any icon from
97 // |bitmap_map| that has a URL and size matching that in |web_app_info|, as
98 // well as adding any new images from |bitmap_map| that have no URL.
99 static void UpdateWebAppIconsWithoutChangingLinks(
100 std::map
<int, BookmarkAppHelper::BitmapAndSource
> bitmap_map
,
101 WebApplicationInfo
* web_app_info
);
103 // Begins the asynchronous bookmark app creation.
104 void Create(const CreateBookmarkAppCallback
& callback
);
106 // Begins the asynchronous bookmark app creation from an app banner.
107 void CreateFromAppBanner(const CreateBookmarkAppCallback
& callback
,
108 const content::Manifest
& manifest
);
111 friend class TestBookmarkAppHelper
;
113 // Called by the WebContents when the manifest has been downloaded. If there
114 // is no manifest, or the WebContents is destroyed before the manifest could
115 // be downloaded, this is called with an empty manifest.
116 void OnDidGetManifest(const content::Manifest
& manifest
);
118 // Performs post icon download tasks including installing the bookmark app.
119 void OnIconsDownloaded(bool success
,
120 const std::map
<GURL
, std::vector
<SkBitmap
> >& bitmaps
);
122 // Called after the bubble has been shown, and the user has either accepted or
123 // the dialog was dismissed.
124 void OnBubbleCompleted(bool user_accepted
,
125 const WebApplicationInfo
& web_app_info
);
127 // Called when the installation of the app is complete to perform the final
128 // installation steps.
129 void FinishInstallation(const Extension
* extension
);
131 // Overridden from content::NotificationObserver:
132 void Observe(int type
,
133 const content::NotificationSource
& source
,
134 const content::NotificationDetails
& details
) override
;
136 // The profile that the bookmark app is being added to.
139 // The web contents that the bookmark app is being created for.
140 content::WebContents
* contents_
;
142 // The WebApplicationInfo that the bookmark app is being created for.
143 WebApplicationInfo web_app_info_
;
145 // Called on app creation or failure.
146 CreateBookmarkAppCallback callback_
;
148 // Downloads icons from the given WebApplicationInfo using the given
150 scoped_ptr
<FaviconDownloader
> favicon_downloader_
;
152 // Used to install the created bookmark app.
153 scoped_refptr
<extensions::CrxInstaller
> crx_installer_
;
155 content::NotificationRegistrar registrar_
;
158 // Creates or updates a bookmark app from the given |web_app_info|. Icons will
159 // not be downloaded so only supplied icon data will be used.
160 void CreateOrUpdateBookmarkApp(ExtensionService
* service
,
161 WebApplicationInfo
* web_app_info
);
163 // Retrieves the WebApplicationInfo that represents a given bookmark app.
164 // |callback| will be called with a WebApplicationInfo which is populated with
165 // the extension's details and icons on success and an unpopulated
166 // WebApplicationInfo on failure.
167 void GetWebApplicationInfoFromApp(
168 content::BrowserContext
* browser_context
,
169 const extensions::Extension
* extension
,
170 const base::Callback
<void(const WebApplicationInfo
&)> callback
);
172 // Returns whether the given |url| is a valid bookmark app url.
173 bool IsValidBookmarkAppUrl(const GURL
& url
);
175 } // namespace extensions
177 #endif // CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_