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
;
29 namespace extensions
{
33 // A helper class for creating bookmark apps from a WebContents.
34 class BookmarkAppHelper
: public content::NotificationObserver
{
36 typedef base::Callback
<void(const Extension
*, const WebApplicationInfo
&)>
37 CreateBookmarkAppCallback
;
39 // This helper class will create a bookmark app out of |web_app_info| and
40 // install it to |service|. Icons will be downloaded from the URLs in
41 // |web_app_info.icons| using |contents| if |contents| is not NULL.
42 // All existing icons from WebApplicationInfo will also be used.
43 BookmarkAppHelper(ExtensionService
* service
,
44 WebApplicationInfo web_app_info
,
45 content::WebContents
* contents
);
46 ~BookmarkAppHelper() override
;
48 // Update the given WebApplicationInfo with information from the manifest.
49 static void UpdateWebAppInfoFromManifest(const content::Manifest
& manifest
,
50 WebApplicationInfo
* web_app_info
);
52 // This finds the closest not-smaller bitmap in |bitmaps| for each size in
53 // |sizes| and resizes it to that size. This returns a map of sizes to bitmaps
54 // which contains only bitmaps of a size in |sizes| and at most one bitmap of
56 static std::map
<int, SkBitmap
> ConstrainBitmapsToSizes(
57 const std::vector
<SkBitmap
>& bitmaps
,
58 const std::set
<int>& sizes
);
60 // Adds a square container icon of |output_size| pixels to |bitmaps| by
61 // drawing the given |letter| into a rounded background of |color|.
62 // Does nothing if an icon of |output_size| already exists in |bitmaps|.
63 static void GenerateIcon(std::map
<int, SkBitmap
>* bitmaps
,
68 // Begins the asynchronous bookmark app creation.
69 void Create(const CreateBookmarkAppCallback
& callback
);
72 friend class TestBookmarkAppHelper
;
74 // Called by the WebContents when the manifest has been downloaded. If there
75 // is no manifest, or the WebContents is destroyed before the manifest could
76 // be downloaded, this is called with an empty manifest.
77 void OnDidGetManifest(const content::Manifest
& manifest
);
79 // Performs post icon download tasks including installing the bookmark app.
80 void OnIconsDownloaded(bool success
,
81 const std::map
<GURL
, std::vector
<SkBitmap
> >& bitmaps
);
83 // Overridden from content::NotificationObserver:
84 void Observe(int type
,
85 const content::NotificationSource
& source
,
86 const content::NotificationDetails
& details
) override
;
88 // The web contents that the bookmark app is being created for.
89 content::WebContents
* contents_
;
91 // The WebApplicationInfo that the bookmark app is being created for.
92 WebApplicationInfo web_app_info_
;
94 // Called on app creation or failure.
95 CreateBookmarkAppCallback callback_
;
97 // Downloads icons from the given WebApplicationInfo using the given
99 scoped_ptr
<FaviconDownloader
> favicon_downloader_
;
101 // Used to install the created bookmark app.
102 scoped_refptr
<extensions::CrxInstaller
> crx_installer_
;
104 content::NotificationRegistrar registrar_
;
107 // Creates or updates a bookmark app from the given |web_app_info|. Icons will
108 // not be downloaded so only supplied icon data will be used.
109 void CreateOrUpdateBookmarkApp(ExtensionService
* service
,
110 WebApplicationInfo
* web_app_info
);
112 // Retrieves the WebApplicationInfo that represents a given bookmark app.
113 // |callback| will be called with a WebApplicationInfo which is populated with
114 // the extension's details and icons on success and an unpopulated
115 // WebApplicationInfo on failure.
116 void GetWebApplicationInfoFromApp(
117 content::BrowserContext
* browser_context
,
118 const extensions::Extension
* extension
,
119 const base::Callback
<void(const WebApplicationInfo
&)> callback
);
121 // Returns whether the given |url| is a valid bookmark app url.
122 bool IsValidBookmarkAppUrl(const GURL
& url
);
124 } // namespace extensions
126 #endif // CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_