Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / bookmark_app_helper.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_
6 #define CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_
7
8 #include <map>
9 #include <set>
10 #include <vector>
11
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
19 class ExtensionService;
20 class FaviconDownloader;
21 class SkBitmap;
22
23 namespace content {
24 class BrowserContext;
25 class WebContents;
26 }
27
28 namespace extensions {
29 class CrxInstaller;
30 class Extension;
31
32 // A helper class for creating bookmark apps from a WebContents.
33 class BookmarkAppHelper : public content::NotificationObserver {
34  public:
35   typedef base::Callback<void(const Extension*, const WebApplicationInfo&)>
36       CreateBookmarkAppCallback;
37
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();
45
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
49   // each size.
50   static std::map<int, SkBitmap> ConstrainBitmapsToSizes(
51       const std::vector<SkBitmap>& bitmaps,
52       const std::set<int>& sizes);
53
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,
59                                     int output_size);
60
61   // Begins the asynchronous bookmark app creation.
62   void Create(const CreateBookmarkAppCallback& callback);
63
64  private:
65   friend class TestBookmarkAppHelper;
66
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);
70
71   // Overridden from content::NotificationObserver:
72   virtual void Observe(int type,
73                        const content::NotificationSource& source,
74                        const content::NotificationDetails& details) OVERRIDE;
75
76   // The WebApplicationInfo that the bookmark app is being created for.
77   WebApplicationInfo web_app_info_;
78
79   // Called on app creation or failure.
80   CreateBookmarkAppCallback callback_;
81
82   // Downloads icons from the given WebApplicationInfo using the given
83   // WebContents.
84   scoped_ptr<FaviconDownloader> favicon_downloader_;
85
86   // Used to install the created bookmark app.
87   scoped_refptr<extensions::CrxInstaller> crx_installer_;
88
89   content::NotificationRegistrar registrar_;
90 };
91
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);
96
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);
105
106 // Returns whether the given |url| is a valid bookmark app url.
107 bool IsValidBookmarkAppUrl(const GURL& url);
108
109 }  // namespace extensions
110
111 #endif  // CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_