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 <string>
     10 #include <vector>
     11 
     12 #include "base/files/file_path.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/scoped_vector.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "google_apis/drive/gdata_errorcode.h"
     17 #include "google_apis/drive/gdata_wapi_parser.h"
     18 #include "url/gurl.h"
     19 
     20 namespace google_apis {
     21 class AppList;
     22 }  // namespace google_apis
     23 
     24 namespace drive {
     25 
     26 class JobScheduler;
     27 
     28 // Data structure that defines Drive app. See
     29 // https://chrome.google.com/webstore/category/collection/drive_apps for
     30 // Drive apps available on the webstore.
     31 struct DriveAppInfo {
     32   DriveAppInfo();
     33   DriveAppInfo(const std::string& app_id,
     34                const google_apis::InstalledApp::IconList& app_icons,
     35                const google_apis::InstalledApp::IconList& document_icons,
     36                const std::string& web_store_id,
     37                const std::string& app_name,
     38                const std::string& object_type,
     39                bool is_primary_selector);
     40   ~DriveAppInfo();
     41 
     42   // Drive app id.
     43   std::string app_id;
     44   // Drive application icon URLs for this app, paired with their size (length of
     45   // a side in pixels).
     46   google_apis::InstalledApp::IconList app_icons;
     47   // Drive document icon URLs for this app, paired with their size (length of
     48   // a side in pixels).
     49   google_apis::InstalledApp::IconList document_icons;
     50   // Web store id/extension id;
     51   std::string web_store_id;
     52   // App name.
     53   std::string app_name;
     54   // Object (file) type description handled by this app.
     55   std::string object_type;
     56   // Is app the primary selector for file (default open action).
     57   bool is_primary_selector;
     58 };
     59 
     60 // Keeps the track of installed drive applications in-memory.
     61 class DriveAppRegistry {
     62  public:
     63   explicit DriveAppRegistry(JobScheduler* scheduler);
     64   ~DriveAppRegistry();
     65 
     66   // Returns a list of Drive app information for the |file_extension| with
     67   // |mime_type|.
     68   void GetAppsForFile(const base::FilePath::StringType& file_extension,
     69                       const std::string& mime_type,
     70                       ScopedVector<DriveAppInfo>* apps) const;
     71 
     72   // Updates this registry by fetching the data from the server.
     73   void Update();
     74 
     75   // Updates this registry from the |app_list|.
     76   void UpdateFromAppList(const google_apis::AppList& app_list);
     77 
     78  private:
     79 
     80   // Defines mapping between file content type selectors (extensions, MIME
     81   // types) and corresponding app.
     82   typedef std::multimap<std::string, DriveAppInfo*> DriveAppFileSelectorMap;
     83 
     84   // Part of Update(). Runs upon the completion of fetching the Drive apps
     85   // data from the server.
     86   void UpdateAfterGetAppList(google_apis::GDataErrorCode gdata_error,
     87                              scoped_ptr<google_apis::AppList> app_list);
     88 
     89   // Helper function for loading Drive application file |selectors| into
     90   // corresponding |map|.
     91   static void AddAppSelectorList(
     92       const std::string& web_store_id,
     93       const std::string& app_name,
     94       const google_apis::InstalledApp::IconList& app_icons,
     95       const google_apis::InstalledApp::IconList& document_icons,
     96       const std::string& object_type,
     97       const std::string& app_id,
     98       bool is_primary_selector,
     99       const ScopedVector<std::string>& selectors,
    100       DriveAppFileSelectorMap* map);
    101 
    102   // Finds matching |apps| from |map| based on provided file |selector|.
    103   void FindAppsForSelector(const std::string& selector,
    104                            const DriveAppFileSelectorMap& map,
    105                            std::vector<DriveAppInfo*>* matched_apps) const;
    106 
    107   JobScheduler* scheduler_;
    108 
    109   // Map of filename extension to application info.
    110   DriveAppFileSelectorMap app_extension_map_;
    111 
    112   // Map of MIME type to application info.
    113   DriveAppFileSelectorMap app_mimetypes_map_;
    114 
    115   bool is_updating_;
    116 
    117   // Note: This should remain the last member so it'll be destroyed and
    118   // invalidate the weak pointers before any other members are destroyed.
    119   base::WeakPtrFactory<DriveAppRegistry> weak_ptr_factory_;
    120   DISALLOW_COPY_AND_ASSIGN(DriveAppRegistry);
    121 };
    122 
    123 namespace util {
    124 
    125 // The preferred icon size, which should usually be used for FindPreferredIcon;
    126 const int kPreferredIconSize = 16;
    127 
    128 // Finds an icon in the list of icons. If unable to find an icon of the exact
    129 // size requested, returns one with the next larger size. If all icons are
    130 // smaller than the preferred size, we'll return the largest one available.
    131 // Icons do not have to be sorted by the icon size. If there are no icons in
    132 // the list, returns an empty URL.
    133 GURL FindPreferredIcon(const google_apis::InstalledApp::IconList& icons,
    134                        int preferred_size);
    135 
    136 }  // namespace util
    137 
    138 }  // namespace drive
    139 
    140 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_APP_REGISTRY_H_
    141