OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "chrome/browser/ui/app_list/arc/arc_default_app_list.h" |
| 6 |
| 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/json/json_file_value_serializer.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/task_runner_util.h" |
| 11 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kActivity[] = "activity"; |
| 18 const char kAppPath[] = "app_path"; |
| 19 const char kName[] = "name"; |
| 20 const char kOem[] = "oem"; |
| 21 const char kPackageName[] = "package_name"; |
| 22 |
| 23 // Sub-directory wher Arc apps forward declarations are stored. |
| 24 const base::FilePath::CharType kArcDirectory[] = FILE_PATH_LITERAL("arc"); |
| 25 const base::FilePath::CharType kArcTestDirectory[] = |
| 26 FILE_PATH_LITERAL("arc_default_apps"); |
| 27 |
| 28 bool use_test_apps_directory = false; |
| 29 |
| 30 std::unique_ptr<ArcDefaultAppList::AppInfoMap> |
| 31 ReadAppsFromFileThread() { |
| 32 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 33 |
| 34 std::unique_ptr<ArcDefaultAppList::AppInfoMap> apps( |
| 35 new ArcDefaultAppList::AppInfoMap); |
| 36 |
| 37 base::FilePath base_path; |
| 38 if (!use_test_apps_directory) { |
| 39 if (!base::PathService::Get(chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS, |
| 40 &base_path)) |
| 41 return apps; |
| 42 base_path = base_path.Append(kArcDirectory); |
| 43 } else { |
| 44 if (!base::PathService::Get(chrome::DIR_TEST_DATA, &base_path)) |
| 45 return apps; |
| 46 base_path = base_path.AppendASCII(kArcTestDirectory); |
| 47 } |
| 48 |
| 49 base::FilePath::StringType extension(".json"); |
| 50 base::FileEnumerator json_files( |
| 51 base_path, |
| 52 false, // Recursive. |
| 53 base::FileEnumerator::FILES); |
| 54 |
| 55 for (base::FilePath file = json_files.Next(); !file.empty(); |
| 56 file = json_files.Next()) { |
| 57 |
| 58 if (file.MatchesExtension(extension)) { |
| 59 JSONFileValueDeserializer deserializer(file); |
| 60 std::string error_msg; |
| 61 std::unique_ptr<base::Value> app_info = |
| 62 deserializer.Deserialize(nullptr, &error_msg); |
| 63 if (!app_info) { |
| 64 VLOG(2) << "Unable to deserialize json data: " << error_msg |
| 65 << " in file " << file.value() << "."; |
| 66 continue; |
| 67 } |
| 68 |
| 69 std::unique_ptr<base::DictionaryValue> app_info_dictionary = |
| 70 base::DictionaryValue::From(std::move(app_info)); |
| 71 CHECK(app_info_dictionary); |
| 72 |
| 73 std::string name; |
| 74 std::string package_name; |
| 75 std::string activity; |
| 76 std::string app_path; |
| 77 bool oem = false; |
| 78 |
| 79 app_info_dictionary->GetString(kName, &name); |
| 80 app_info_dictionary->GetString(kPackageName, &package_name); |
| 81 app_info_dictionary->GetString(kActivity, &activity); |
| 82 app_info_dictionary->GetString(kAppPath, &app_path); |
| 83 app_info_dictionary->GetBoolean(kOem, &oem); |
| 84 |
| 85 if (name.empty() || |
| 86 package_name.empty() || |
| 87 activity.empty() || |
| 88 app_path.empty()) { |
| 89 VLOG(2) << "Arc app declaration is incomplete in file " |
| 90 << file.value() << "."; |
| 91 continue; |
| 92 } |
| 93 |
| 94 const std::string app_id = ArcAppListPrefs::GetAppId( |
| 95 package_name, activity); |
| 96 std::unique_ptr<ArcDefaultAppList::AppInfo> app( |
| 97 new ArcDefaultAppList::AppInfo(name, |
| 98 package_name, |
| 99 activity, |
| 100 oem, |
| 101 base_path.Append(app_path))); |
| 102 apps.get()->insert( |
| 103 std::pair<std::string, |
| 104 std::unique_ptr<ArcDefaultAppList::AppInfo>>( |
| 105 app_id, std::move(app))); |
| 106 } else { |
| 107 DVLOG(1) << "Not considering: " << file.LossyDisplayName() |
| 108 << " (does not have a .json extension)"; |
| 109 } |
| 110 } |
| 111 |
| 112 return apps; |
| 113 } |
| 114 |
| 115 } // namespace |
| 116 |
| 117 // static |
| 118 void ArcDefaultAppList::UseTestAppsDirectory() { |
| 119 use_test_apps_directory = true; |
| 120 } |
| 121 |
| 122 ArcDefaultAppList::ArcDefaultAppList(Delegate* delegate) |
| 123 : delegate_(delegate), weak_ptr_factory_(this) { |
| 124 CHECK(delegate_); |
| 125 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 126 |
| 127 // Once ready OnAppsReady is called. |
| 128 base::PostTaskAndReplyWithResult( |
| 129 content::BrowserThread::GetBlockingPool(), FROM_HERE, |
| 130 base::Bind(&ReadAppsFromFileThread), |
| 131 base::Bind(&ArcDefaultAppList::OnAppsReady, |
| 132 weak_ptr_factory_.GetWeakPtr())); |
| 133 } |
| 134 |
| 135 ArcDefaultAppList::~ArcDefaultAppList() {} |
| 136 |
| 137 void ArcDefaultAppList::OnAppsReady(std::unique_ptr<AppInfoMap> apps) { |
| 138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 139 |
| 140 apps_.swap(*apps.get()); |
| 141 |
| 142 // Initially consider packages are installed. |
| 143 for (const auto& app : apps_) |
| 144 packages_[app.second->package_name] = false; |
| 145 |
| 146 delegate_->OnDefaultAppsReady(); |
| 147 } |
| 148 |
| 149 const ArcDefaultAppList::AppInfo* ArcDefaultAppList::GetApp( |
| 150 const std::string& app_id) const { |
| 151 const auto it = apps_.find(app_id); |
| 152 if (it == apps_.end()) |
| 153 return nullptr; |
| 154 // Check if its package was uninstalled. |
| 155 const auto it_package = packages_.find(it->second->package_name); |
| 156 DCHECK(it_package != packages_.end()); |
| 157 if (it_package->second) |
| 158 return nullptr; |
| 159 return it->second.get(); |
| 160 } |
| 161 |
| 162 bool ArcDefaultAppList::HasApp(const std::string& app_id) const { |
| 163 return GetApp(app_id) != nullptr; |
| 164 } |
| 165 |
| 166 bool ArcDefaultAppList::HasPackage(const std::string& package_name) const { |
| 167 return packages_.count(package_name); |
| 168 } |
| 169 |
| 170 void ArcDefaultAppList::MaybeMarkPackageUninstalled( |
| 171 const std::string& package_name, bool uninstalled) { |
| 172 auto it = packages_.find(package_name); |
| 173 if (it == packages_.end()) |
| 174 return; |
| 175 it->second = uninstalled; |
| 176 } |
| 177 |
| 178 ArcDefaultAppList::AppInfo::AppInfo(const std::string& name, |
| 179 const std::string& package_name, |
| 180 const std::string& activity, |
| 181 bool oem, |
| 182 const base::FilePath app_path) |
| 183 : name(name), |
| 184 package_name(package_name), |
| 185 activity(activity), |
| 186 oem(oem), |
| 187 app_path(app_path) {} |
| 188 |
| 189 ArcDefaultAppList::AppInfo::~AppInfo() {} |
OLD | NEW |