0c5d7aa064837dee7a3370ce7c1ac8a8cc4ba429
[platform/framework/web/crosswalk.git] / src / chrome / browser / drive / drive_app_registry.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_DRIVE_DRIVE_APP_REGISTRY_H_
6 #define CHROME_BROWSER_DRIVE_DRIVE_APP_REGISTRY_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/callback_forward.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "google_apis/drive/gdata_errorcode.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 DriveAppRegistryObserver;
27 class DriveServiceInterface;
28
29 // Data structure that defines Drive app. See
30 // https://chrome.google.com/webstore/category/collection/drive_apps for
31 // Drive apps available on the webstore.
32 struct DriveAppInfo {
33   typedef std::vector<std::pair<int, GURL> > IconList;
34
35   DriveAppInfo();
36   DriveAppInfo(const std::string& app_id,
37                const std::string& product_id,
38                const IconList& app_icons,
39                const IconList& document_icons,
40                const std::string& app_name,
41                const GURL& create_url,
42                bool is_removable);
43   ~DriveAppInfo();
44
45   // Drive app id.
46   std::string app_id;
47   // Drive app's product id. This is different from app id that is used inside
48   // Drive. Product id is an id for the app in webstore; hence, it can be used
49   // for identifying the same app install as Chrome extension and as Drive app
50   // at the same time.
51   std::string product_id;
52   // Drive application icon URLs for this app, paired with their size (length of
53   // a side in pixels).
54   IconList app_icons;
55   // Drive document icon URLs for this app, paired with their size (length of
56   // a side in pixels).
57   IconList document_icons;
58   // App name.
59   std::string app_name;
60   // URL for opening a new file in the app. Empty if the app does not support
61   // new file creation.
62   GURL create_url;
63   // Returns if UninstallApp() is allowed for the app. Built-in apps have this
64   // field set false.
65   bool is_removable;
66 };
67
68 // Callback type for UninstallApp().
69 typedef base::Callback<void(google_apis::GDataErrorCode)> UninstallCallback;
70
71 // Keeps the track of installed drive applications in-memory.
72 class DriveAppRegistry {
73  public:
74   explicit DriveAppRegistry(DriveServiceInterface* scheduler);
75   ~DriveAppRegistry();
76
77   // Returns a list of Drive app information for the |file_extension| with
78   // |mime_type|.
79   void GetAppsForFile(const base::FilePath::StringType& file_extension,
80                       const std::string& mime_type,
81                       std::vector<DriveAppInfo>* apps) const;
82
83   // Returns the list of all Drive apps installed.
84   void GetAppList(std::vector<DriveAppInfo>* apps) const;
85
86   // Uninstalls the app specified by |app_id|. This method sends requests to the
87   // remote server, and returns the result to |callback| asynchronously. When
88   // succeeded, the callback receives HTTP_NO_CONTENT, and error code otherwise.
89   // |callback| must not be null.
90   void UninstallApp(const std::string& app_id,
91                     const UninstallCallback& callback);
92
93   // Checks whether UinstallApp is supported. The feature is available only for
94   // clients with whitelisted API keys (like Official Google Chrome build).
95   static bool IsAppUninstallSupported();
96
97   // Updates this registry by fetching the data from the server.
98   void Update();
99
100   // Updates this registry from the |app_list|.
101   void UpdateFromAppList(const google_apis::AppList& app_list);
102
103   void AddObserver(DriveAppRegistryObserver* observer);
104   void RemoveObserver(DriveAppRegistryObserver* observer);
105
106  private:
107   // Part of Update(). Runs upon the completion of fetching the Drive apps
108   // data from the server.
109   void UpdateAfterGetAppList(google_apis::GDataErrorCode gdata_error,
110                              scoped_ptr<google_apis::AppList> app_list);
111
112   // Part of UninstallApp(). Receives the response from the server.
113   void OnAppUninstalled(const std::string& app_id,
114                         const UninstallCallback& callback,
115                         google_apis::GDataErrorCode error);
116
117   // Map of application id to each app's info.
118   std::map<std::string, DriveAppInfo> all_apps_;
119
120   // Defines mapping between file content type selectors (extensions, MIME
121   // types) and corresponding app.
122   typedef std::multimap<std::string, std::string> DriveAppFileSelectorMap;
123   DriveAppFileSelectorMap extension_map_;
124   DriveAppFileSelectorMap mimetype_map_;
125
126   DriveServiceInterface* drive_service_;
127
128   bool is_updating_;
129
130   ObserverList<DriveAppRegistryObserver> observers_;
131
132   // Note: This should remain the last member so it'll be destroyed and
133   // invalidate the weak pointers before any other members are destroyed.
134   base::WeakPtrFactory<DriveAppRegistry> weak_ptr_factory_;
135
136   DISALLOW_COPY_AND_ASSIGN(DriveAppRegistry);
137 };
138
139 namespace util {
140
141 // The preferred icon size, which should usually be used for FindPreferredIcon;
142 const int kPreferredIconSize = 16;
143
144 // Finds an icon in the list of icons. If unable to find an icon of the exact
145 // size requested, returns one with the next larger size. If all icons are
146 // smaller than the preferred size, we'll return the largest one available.
147 // Icons do not have to be sorted by the icon size. If there are no icons in
148 // the list, returns an empty URL.
149 GURL FindPreferredIcon(const DriveAppInfo::IconList& icons,
150                        int preferred_size);
151
152 }  // namespace util
153
154 }  // namespace drive
155
156 #endif  // CHROME_BROWSER_DRIVE_DRIVE_APP_REGISTRY_H_