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 // Resize icons to the accepted sizes, and generate any that are missing. Does
81 // not update |web_app_info| except to update |generated_icon_color|.
82 static std::map
<int, BitmapAndSource
> ResizeIconsAndGenerateMissing(
83 std::vector
<BitmapAndSource
> icons
,
84 std::set
<int> sizes_to_generate
,
85 WebApplicationInfo
* web_app_info
);
87 // It is important that the linked app information in any extension that
88 // gets created from sync matches the linked app information that came from
89 // sync. If there are any changes, they will be synced back to other devices
90 // and could potentially create a never ending sync cycle.
91 // This function updates |web_app_info| with the image data of any icon from
92 // |bitmap_map| that has a URL and size matching that in |web_app_info|, as
93 // well as adding any new images from |bitmap_map| that have no URL.
94 static void UpdateWebAppIconsWithoutChangingLinks(
95 std::map
<int, BookmarkAppHelper::BitmapAndSource
> bitmap_map
,
96 WebApplicationInfo
* web_app_info
);
98 // Begins the asynchronous bookmark app creation.
99 void Create(const CreateBookmarkAppCallback
& callback
);
101 // Begins the asynchronous bookmark app creation from an app banner.
102 void CreateFromAppBanner(const CreateBookmarkAppCallback
& callback
,
103 const content::Manifest
& manifest
);
106 friend class TestBookmarkAppHelper
;
108 // Called by the WebContents when the manifest has been downloaded. If there
109 // is no manifest, or the WebContents is destroyed before the manifest could
110 // be downloaded, this is called with an empty manifest.
111 void OnDidGetManifest(const content::Manifest
& manifest
);
113 // Performs post icon download tasks including installing the bookmark app.
114 void OnIconsDownloaded(bool success
,
115 const std::map
<GURL
, std::vector
<SkBitmap
> >& bitmaps
);
117 // Called after the bubble has been shown, and the user has either accepted or
118 // the dialog was dismissed.
119 void OnBubbleCompleted(bool user_accepted
,
120 const WebApplicationInfo
& web_app_info
);
122 // Called when the installation of the app is complete to perform the final
123 // installation steps.
124 void FinishInstallation(const Extension
* extension
);
126 // Overridden from content::NotificationObserver:
127 void Observe(int type
,
128 const content::NotificationSource
& source
,
129 const content::NotificationDetails
& details
) override
;
131 // The profile that the bookmark app is being added to.
134 // The web contents that the bookmark app is being created for.
135 content::WebContents
* contents_
;
137 // The WebApplicationInfo that the bookmark app is being created for.
138 WebApplicationInfo web_app_info_
;
140 // Called on app creation or failure.
141 CreateBookmarkAppCallback callback_
;
143 // Downloads icons from the given WebApplicationInfo using the given
145 scoped_ptr
<FaviconDownloader
> favicon_downloader_
;
147 // Used to install the created bookmark app.
148 scoped_refptr
<extensions::CrxInstaller
> crx_installer_
;
150 content::NotificationRegistrar registrar_
;
153 // Creates or updates a bookmark app from the given |web_app_info|. Icons will
154 // not be downloaded so only supplied icon data will be used.
155 void CreateOrUpdateBookmarkApp(ExtensionService
* service
,
156 WebApplicationInfo
* web_app_info
);
158 // Retrieves the WebApplicationInfo that represents a given bookmark app.
159 // |callback| will be called with a WebApplicationInfo which is populated with
160 // the extension's details and icons on success and an unpopulated
161 // WebApplicationInfo on failure.
162 void GetWebApplicationInfoFromApp(
163 content::BrowserContext
* browser_context
,
164 const extensions::Extension
* extension
,
165 const base::Callback
<void(const WebApplicationInfo
&)> callback
);
167 // Returns whether the given |url| is a valid bookmark app url.
168 bool IsValidBookmarkAppUrl(const GURL
& url
);
170 } // namespace extensions
172 #endif // CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_