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