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"
19 class ExtensionService;
20 class FaviconDownloader;
28 namespace extensions {
32 // A helper class for creating bookmark apps from a WebContents.
33 class BookmarkAppHelper : public content::NotificationObserver {
35 typedef base::Callback<void(const Extension*, const WebApplicationInfo&)>
36 CreateBookmarkAppCallback;
38 // This helper class will create a bookmark app out of |web_app_info| and
39 // install it to |service|. Icons will be downloaded from the URLs in
40 // |web_app_info.icons| using |contents|.
41 BookmarkAppHelper(ExtensionService* service,
42 WebApplicationInfo web_app_info,
43 content::WebContents* contents);
44 virtual ~BookmarkAppHelper();
46 // This finds the closest not-smaller bitmap in |bitmaps| for each size in
47 // |sizes| and resizes it to that size. This returns a map of sizes to bitmaps
48 // which contains only bitmaps of a size in |sizes| and at most one bitmap of
50 static std::map<int, SkBitmap> ConstrainBitmapsToSizes(
51 const std::vector<SkBitmap>& bitmaps,
52 const std::set<int>& sizes);
54 // Adds a square container icon of |output_size| pixels to |bitmaps| by
55 // centering the biggest smaller icon in |bitmaps| and drawing a rounded
56 // rectangle with strip of the that icon's dominant color at the bottom.
57 // Does nothing if an icon of |output_size| already exists in |bitmaps|.
58 static void GenerateContainerIcon(std::map<int, SkBitmap>* bitmaps,
61 // Begins the asynchronous bookmark app creation.
62 void Create(const CreateBookmarkAppCallback& callback);
65 friend class TestBookmarkAppHelper;
67 // Performs post icon download tasks including installing the bookmark app.
68 void OnIconsDownloaded(bool success,
69 const std::map<GURL, std::vector<SkBitmap> >& bitmaps);
71 // Overridden from content::NotificationObserver:
72 virtual void Observe(int type,
73 const content::NotificationSource& source,
74 const content::NotificationDetails& details) OVERRIDE;
76 // The WebApplicationInfo that the bookmark app is being created for.
77 WebApplicationInfo web_app_info_;
79 // Called on app creation or failure.
80 CreateBookmarkAppCallback callback_;
82 // Downloads icons from the given WebApplicationInfo using the given
84 scoped_ptr<FaviconDownloader> favicon_downloader_;
86 // Used to install the created bookmark app.
87 scoped_refptr<extensions::CrxInstaller> crx_installer_;
89 content::NotificationRegistrar registrar_;
92 // Creates or updates a bookmark app from the given |web_app_info|. Icons will
93 // not be downloaded so only supplied icon data will be used.
94 void CreateOrUpdateBookmarkApp(ExtensionService* service,
95 WebApplicationInfo& web_app_info);
97 // Retrieves the WebApplicationInfo that represents a given bookmark app.
98 // |callback| will be called with a WebApplicationInfo which is populated with
99 // the extension's details and icons on success and an unpopulated
100 // WebApplicationInfo on failure.
101 void GetWebApplicationInfoFromApp(
102 content::BrowserContext* browser_context,
103 const extensions::Extension* extension,
104 const base::Callback<void(const WebApplicationInfo&)> callback);
106 // Returns whether the given |url| is a valid bookmark app url.
107 bool IsValidBookmarkAppUrl(const GURL& url);
109 } // namespace extensions
111 #endif // CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_