Home | History | Annotate | Download | only in extensions
      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| if |contents| is not NULL.
     41   // All existing icons from WebApplicationInfo will also be used.
     42   BookmarkAppHelper(ExtensionService* service,
     43                     WebApplicationInfo web_app_info,
     44                     content::WebContents* contents);
     45   virtual ~BookmarkAppHelper();
     46 
     47   // This finds the closest not-smaller bitmap in |bitmaps| for each size in
     48   // |sizes| and resizes it to that size. This returns a map of sizes to bitmaps
     49   // which contains only bitmaps of a size in |sizes| and at most one bitmap of
     50   // each size.
     51   static std::map<int, SkBitmap> ConstrainBitmapsToSizes(
     52       const std::vector<SkBitmap>& bitmaps,
     53       const std::set<int>& sizes);
     54 
     55   // Adds a square container icon of |output_size| pixels to |bitmaps| by
     56   // drawing the given |letter| into a rounded background of |color|.
     57   // Does nothing if an icon of |output_size| already exists in |bitmaps|.
     58   static void GenerateIcon(std::map<int, SkBitmap>* bitmaps,
     59                            int output_size,
     60                            SkColor color,
     61                            char letter);
     62 
     63   // Begins the asynchronous bookmark app creation.
     64   void Create(const CreateBookmarkAppCallback& callback);
     65 
     66  private:
     67   friend class TestBookmarkAppHelper;
     68 
     69   // Performs post icon download tasks including installing the bookmark app.
     70   void OnIconsDownloaded(bool success,
     71                          const std::map<GURL, std::vector<SkBitmap> >& bitmaps);
     72 
     73   // Overridden from content::NotificationObserver:
     74   virtual void Observe(int type,
     75                        const content::NotificationSource& source,
     76                        const content::NotificationDetails& details) OVERRIDE;
     77 
     78   // The WebApplicationInfo that the bookmark app is being created for.
     79   WebApplicationInfo web_app_info_;
     80 
     81   // Called on app creation or failure.
     82   CreateBookmarkAppCallback callback_;
     83 
     84   // Downloads icons from the given WebApplicationInfo using the given
     85   // WebContents.
     86   scoped_ptr<FaviconDownloader> favicon_downloader_;
     87 
     88   // Used to install the created bookmark app.
     89   scoped_refptr<extensions::CrxInstaller> crx_installer_;
     90 
     91   content::NotificationRegistrar registrar_;
     92 };
     93 
     94 // Creates or updates a bookmark app from the given |web_app_info|. Icons will
     95 // not be downloaded so only supplied icon data will be used.
     96 void CreateOrUpdateBookmarkApp(ExtensionService* service,
     97                                WebApplicationInfo& web_app_info);
     98 
     99 // Retrieves the WebApplicationInfo that represents a given bookmark app.
    100 // |callback| will be called with a WebApplicationInfo which is populated with
    101 // the extension's details and icons on success and an unpopulated
    102 // WebApplicationInfo on failure.
    103 void GetWebApplicationInfoFromApp(
    104     content::BrowserContext* browser_context,
    105     const extensions::Extension* extension,
    106     const base::Callback<void(const WebApplicationInfo&)> callback);
    107 
    108 // Returns whether the given |url| is a valid bookmark app url.
    109 bool IsValidBookmarkAppUrl(const GURL& url);
    110 
    111 }  // namespace extensions
    112 
    113 #endif  // CHROME_BROWSER_EXTENSIONS_BOOKMARK_APP_HELPER_H_
    114