Home | History | Annotate | Download | only in drive
      1 // Copyright (c) 2012 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_CHROMEOS_DRIVE_DRIVE_APP_REGISTRY_H_
      6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_APP_REGISTRY_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "chrome/browser/google_apis/gdata_errorcode.h"
     16 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
     17 #include "url/gurl.h"
     18 
     19 namespace base {
     20 class FilePath;
     21 }  // namespace base
     22 
     23 namespace google_apis {
     24 class AppList;
     25 }  // namespace google_apis
     26 
     27 namespace drive {
     28 
     29 class JobScheduler;
     30 
     31 // Data structure that defines Drive app. See
     32 // https://chrome.google.com/webstore/category/collection/drive_apps for
     33 // Drive apps available on the webstore.
     34 struct DriveAppInfo {
     35   DriveAppInfo(const std::string& app_id,
     36                const google_apis::InstalledApp::IconList& app_icons,
     37                const google_apis::InstalledApp::IconList& document_icons,
     38                const std::string& web_store_id,
     39                const string16& app_name,
     40                const string16& object_type,
     41                bool is_primary_selector);
     42   ~DriveAppInfo();
     43 
     44   // Drive app id.
     45   std::string app_id;
     46   // Drive application icon URLs for this app, paired with their size (length of
     47   // a side in pixels).
     48   google_apis::InstalledApp::IconList app_icons;
     49   // Drive document icon URLs for this app, paired with their size (length of
     50   // a side in pixels).
     51   google_apis::InstalledApp::IconList document_icons;
     52   // Web store id/extension id;
     53   std::string web_store_id;
     54   // App name.
     55   string16 app_name;
     56   // Object (file) type description handled by this app.
     57   string16 object_type;
     58   // Is app the primary selector for file (default open action).
     59   bool is_primary_selector;
     60 };
     61 
     62 // Keeps the track of installed drive applications in-memory.
     63 class DriveAppRegistry {
     64  public:
     65   explicit DriveAppRegistry(JobScheduler* scheduler);
     66   ~DriveAppRegistry();
     67 
     68   // Returns a list of web app information for the |file| with |mime_type|.
     69   void GetAppsForFile(const base::FilePath& file_path,
     70                       const std::string& mime_type,
     71                       ScopedVector<DriveAppInfo>* apps);
     72 
     73   // Updates this registry by fetching the data from the server.
     74   void Update();
     75 
     76  private:
     77   // Defines application details that are associated with a given
     78   // file extension or content mimetype.
     79   struct DriveAppFileSelector {
     80     DriveAppFileSelector(
     81         const GURL& product_link,
     82         const google_apis::InstalledApp::IconList& app_icons,
     83         const google_apis::InstalledApp::IconList& document_icons,
     84         const string16& object_type,
     85         const std::string& app_id,
     86         bool is_primary_selector);
     87     ~DriveAppFileSelector();
     88     // Product link to the webstore.
     89     GURL product_link;
     90     // Drive application icon URLs for this app, paired with their size (length
     91     // of a side in pixels).
     92     google_apis::InstalledApp::IconList app_icons;
     93     // Drive document icon URLs for this app, paired with their size (length of
     94     // a side in pixels).
     95     google_apis::InstalledApp::IconList document_icons;
     96     // Object (file) type description.
     97     string16 object_type;
     98     // Drive app id
     99     std::string app_id;
    100     // True if the selector is the default one. The default selector should
    101     // trigger on file double-click events. Non-default selectors only show up
    102     // in "Open with..." pop-up menu.
    103     bool is_primary_selector;
    104   };
    105 
    106   // Defines mapping between file content type selectors (extensions, MIME
    107   // types) and corresponding app.
    108   typedef std::multimap<std::string,
    109                         DriveAppFileSelector*> DriveAppFileSelectorMap;
    110 
    111   // Helper map used for deduplication of selector matching results.
    112   typedef std::map<const DriveAppFileSelector*,
    113                    DriveAppInfo*> SelectorAppList;
    114 
    115   // Part of Update(). Runs upon the completion of fetching the web apps
    116   // data from the server.
    117   void UpdateAfterGetAppList(google_apis::GDataErrorCode gdata_error,
    118                              scoped_ptr<google_apis::AppList> app_list);
    119 
    120   // Helper function for loading web application file |selectors| into
    121   // corresponding |map|.
    122   static void AddAppSelectorList(
    123       const GURL& product_link,
    124       const google_apis::InstalledApp::IconList& app_icons,
    125       const google_apis::InstalledApp::IconList& document_icons,
    126       const std::string& object_type,
    127       const std::string& app_id,
    128       bool is_primary_selector,
    129       const ScopedVector<std::string>& selectors,
    130       DriveAppFileSelectorMap* map);
    131 
    132   // Finds matching |apps| from |map| based on provided file |selector|.
    133   void FindAppsForSelector(const std::string& selector,
    134                            const DriveAppFileSelectorMap& map,
    135                            SelectorAppList* apps);
    136 
    137   JobScheduler* scheduler_;
    138 
    139   // Map of web store product URL to application name.
    140   std::map<GURL, std::string> url_to_name_map_;
    141 
    142   // Map of filename extension to application info.
    143   DriveAppFileSelectorMap app_extension_map_;
    144 
    145   // Map of MIME type to application info.
    146   DriveAppFileSelectorMap app_mimetypes_map_;
    147 
    148   bool is_updating_;
    149 
    150   // Note: This should remain the last member so it'll be destroyed and
    151   // invalidate the weak pointers before any other members are destroyed.
    152   base::WeakPtrFactory<DriveAppRegistry> weak_ptr_factory_;
    153   DISALLOW_COPY_AND_ASSIGN(DriveAppRegistry);
    154 };
    155 
    156 }  // namespace drive
    157 
    158 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_APP_REGISTRY_H_
    159