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