OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_UI_APP_LIST_ARC_APP_LIST_PREFS_H_ |
| 6 #define CHROME_BROWSER_UI_APP_LIST_ARC_APP_LIST_PREFS_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/observer_list.h" |
| 17 #include "components/arc/arc_bridge_service.h" |
| 18 #include "components/keyed_service/core/keyed_service.h" |
| 19 #include "ui/base/layout.h" |
| 20 |
| 21 class PrefService; |
| 22 |
| 23 namespace content { |
| 24 class BrowserContext; |
| 25 } |
| 26 |
| 27 namespace user_prefs { |
| 28 class PrefRegistrySyncable; |
| 29 } |
| 30 |
| 31 // Declares shareable ARC app specific preferences, that keep information |
| 32 // about app attributes (name, package, activity) and its state. This |
| 33 // information is used to pre-create non-ready app items while ARC bridge |
| 34 // service is not ready to provide information about available ARC apps. |
| 35 class ArcAppListPrefs : public KeyedService, |
| 36 public arc::ArcBridgeService::Observer { |
| 37 public: |
| 38 struct AppInfo { |
| 39 AppInfo(); |
| 40 |
| 41 std::string name; |
| 42 std::string package; |
| 43 std::string activity; |
| 44 bool ready; |
| 45 }; |
| 46 |
| 47 class Observer { |
| 48 public: |
| 49 // Notifies an observer that new app is registered. |
| 50 virtual void OnAppRegistered(const std::string& app_id, |
| 51 const AppInfo& app_info) = 0; |
| 52 // Notifies an observer that app ready state has been changed. |
| 53 virtual void OnAppReadyChanged(const std::string& id, bool ready) = 0; |
| 54 // Notifies an observer that app icon has been installed or updated. |
| 55 virtual void OnAppIconUpdated(const std::string& id, |
| 56 ui::ScaleFactor scale_factor) = 0; |
| 57 }; |
| 58 |
| 59 static ArcAppListPrefs* Create(const base::FilePath& base_path, |
| 60 PrefService* prefs); |
| 61 |
| 62 // Convenience function to get the ArcAppListPrefs for a BrowserContext. |
| 63 static ArcAppListPrefs* Get(content::BrowserContext* context); |
| 64 |
| 65 // Constructs unique id based on package and activity information. This id |
| 66 // is safe to use at file paths and as preference keys. |
| 67 static std::string GetAppId(const std::string& package, |
| 68 const std::string& activity); |
| 69 |
| 70 // It is called from chrome/browser/prefs/browser_prefs.cc. |
| 71 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
| 72 |
| 73 ~ArcAppListPrefs() override; |
| 74 |
| 75 // Returns a list of all app ids, including ready and non-ready apps. |
| 76 std::vector<std::string> GetAppIds() const; |
| 77 |
| 78 // Extracts attributes of an app based on its id. Returns NULL if the app is |
| 79 // not found. |
| 80 scoped_ptr<AppInfo> GetApp(const std::string& app_id) const; |
| 81 |
| 82 // Constructs path to app icon for specific scale factor. |
| 83 base::FilePath GetIconPath(const std::string& app_id, |
| 84 ui::ScaleFactor scale_factor) const; |
| 85 |
| 86 // Requests to load an app icon for specific scale factor. If the app or Arc |
| 87 // bridge service is not ready, then defer this request until the app gets |
| 88 // available. Once new icon is installed notifies an observer |
| 89 // OnAppIconUpdated. |
| 90 void RequestIcon(const std::string& app_id, ui::ScaleFactor scale_factor); |
| 91 |
| 92 // Returns true if app is registered. |
| 93 bool IsRegistered(const std::string& app_id); |
| 94 |
| 95 void AddObserver(Observer* observer); |
| 96 void RemoveObserver(Observer* observer); |
| 97 |
| 98 // arc::ArcBridgeService::Observer |
| 99 void OnStateChanged(arc::ArcBridgeService::State state) override; |
| 100 void OnAppsRefreshed(const std::vector<std::string>& name, |
| 101 const std::vector<std::string>& packages, |
| 102 const std::vector<std::string>& activities) override; |
| 103 void OnAppIcon(const std::string& package, |
| 104 const std::string& activity, |
| 105 int scale_factor, |
| 106 const std::vector<uint8_t>& icon_png_data) override; |
| 107 |
| 108 private: |
| 109 // See the Create methods. |
| 110 ArcAppListPrefs(const base::FilePath& base_path, PrefService* prefs); |
| 111 |
| 112 void OnAppReady(const std::string& name, |
| 113 const std::string& package, |
| 114 const std::string& activity); |
| 115 void DisableAllApps(); |
| 116 |
| 117 // Installs an icon to file system in the special folder of the profile |
| 118 // directory. |
| 119 void InstallIcon(const std::string& app_id, |
| 120 ui::ScaleFactor scale_factor, |
| 121 const std::vector<uint8>& contentPng); |
| 122 void OnIconInstalled(const std::string& app_id, |
| 123 ui::ScaleFactor scale_factor, |
| 124 bool install_succeed); |
| 125 |
| 126 // Owned by the BrowserContext. |
| 127 PrefService* prefs_; |
| 128 |
| 129 // List of observers. |
| 130 base::ObserverList<Observer> observer_list_; |
| 131 // Keeps root folder where ARC app icons for different scale factor are |
| 132 // stored. |
| 133 base::FilePath base_path_; |
| 134 // Contains set of ARC apps that are currently ready. |
| 135 std::set<std::string> ready_apps_; |
| 136 // Keeps deferred icon load requests. Each app may contain several requests |
| 137 // for different scale factor. Scale factor is defined by specific bit |
| 138 // position. |
| 139 std::map<std::string, uint32> request_icon_deferred_; |
| 140 |
| 141 base::WeakPtrFactory<ArcAppListPrefs> weak_ptr_factory_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(ArcAppListPrefs); |
| 144 }; |
| 145 |
| 146 #endif // CHROME_BROWSER_UI_APP_LIST_ARC_APP_LIST_PREFS_H_ |
OLD | NEW |