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