Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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/chromeos/extensions/default_app_order.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/file_path.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/json/json_file_value_serializer.h" | |
| 12 #include "base/path_service.h" | |
| 13 #include "base/time.h" | |
| 14 #include "chrome/common/chrome_paths.h" | |
| 15 #include "chrome/common/extensions/extension_constants.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 | |
| 18 namespace chromeos { | |
| 19 namespace default_app_order { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Timeout in seconds to wait for the ordinals file to be loaded. | |
| 24 const int kTimeoutSecs = 3; | |
| 25 | |
| 26 // The single ExternalLoader instance. | |
| 27 ExternalLoader* loader_instance = NULL; | |
| 28 | |
| 29 // Reads external ordinal json file and returned the parsed value. Returns NULL | |
| 30 // if the file does not exist or could not be parsed properly. Caller takes | |
| 31 // ownership of the returned value. | |
| 32 base::ListValue* ReadExternalOrdinalFile(const FilePath& path) { | |
| 33 if (!file_util::PathExists(path)) | |
| 34 return NULL; | |
| 35 | |
| 36 JSONFileValueSerializer serializer(path); | |
| 37 std::string error_msg; | |
| 38 base::Value* value = serializer.Deserialize(NULL, &error_msg); | |
| 39 if (!value) { | |
| 40 LOG(WARNING) << "Unable to deserialize default app ordinals json data:" | |
| 41 << error_msg << ", file=" << path.value(); | |
| 42 return NULL; | |
| 43 } | |
| 44 | |
| 45 base::ListValue* ordinal_list_value = NULL; | |
| 46 if (value->GetAsList(&ordinal_list_value)) | |
| 47 return ordinal_list_value; | |
| 48 | |
| 49 LOG(WARNING) << "Expect a JSON list in file " << path.value(); | |
| 50 return NULL; | |
| 51 } | |
| 52 | |
| 53 // Gets built-in default app order. | |
| 54 void GetDefault(std::vector<std::string>* app_ids) { | |
| 55 DCHECK(app_ids && app_ids->empty()); | |
| 56 | |
| 57 const char* kDefaultAppOrder[] = { | |
| 58 extension_misc::kChromeAppId, | |
| 59 extension_misc::kWebStoreAppId, | |
| 60 "coobgpohoikkiipiblmjeljniedjpjpf", // Search | |
| 61 "blpcfgokakmgnkcojhhkbfbldkacnbeo", // Youtube | |
| 62 "pjkljhegncpnkpknbcohdijeoejaedia", // Gmail | |
| 63 "ejjicmeblgpmajnghnpcppodonldlgfn", // Calendar | |
| 64 "kjebfhglflhjjjiceimfkgicifkhjlnm", // Scratchpad | |
| 65 "lneaknkopdijkpnocmklfnjbeapigfbh", // Google Maps | |
| 66 "apdfllckaahabafndbhieahigkjlhalf", // Drive | |
| 67 "aohghmighlieiainnegkcijnfilokake", // Docs | |
| 68 "felcaaldnbdncclmgdcncolpebgiejap", // Sheets | |
| 69 "aapocclcgogkmnckokdopfmhonfmgoek", // Slides | |
| 70 "dlppkpafhbajpcmmoheippocdidnckmm", // Google+ | |
| 71 "kbpgddbgniojgndnhlkjbkpknjhppkbk", // Google+ Hangouts | |
| 72 "hhaomjibdihmijegdhdafkllkbggdgoj", // Files | |
| 73 "hkhhlkdconhgemhegnplaldnmnmkaemd", // Tips & Tricks | |
| 74 "icppfcnhkcmnfdhfhphakoifcfokfdhg", // Play Music | |
| 75 "mmimngoggfoobjdlefbcabngfnmieonb", // Play Books | |
| 76 "fppdphmgcddhjeddoeghpjefkdlccljb", // Play Movies | |
| 77 "fobcpibfeplaikcclojfdhfdmbbeofai", // Games | |
| 78 "joodangkbfjnajiiifokapkpmhfnpleo", // Calculator | |
| 79 "hfhhnacclhffhdffklopdkcgdhifgngh", // Camera | |
| 80 "gbchcmhmhahfdphkhkmpfmihenigjmpp", // Chrome Remote Desktop | |
| 81 }; | |
| 82 | |
| 83 for (size_t i = 0; i < arraysize(kDefaultAppOrder); ++i) | |
| 84 app_ids->push_back(std::string(kDefaultAppOrder[i])); | |
| 85 } | |
| 86 | |
| 87 } // namespace | |
| 88 | |
| 89 ExternalLoader::ExternalLoader() : loaded_(true, false) { | |
|
tbarzic
2012/10/26 21:56:28
nit: a comment whet true and false mean.
xiyuan
2012/10/26 22:39:25
Done.
| |
| 90 DCHECK(!loader_instance); | |
| 91 loader_instance = this; | |
| 92 | |
| 93 content::BrowserThread::PostBlockingPoolTask(FROM_HERE, | |
| 94 base::Bind(&ExternalLoader::LoadOnBlockingPool, | |
| 95 base::Unretained(this))); | |
|
tbarzic
2012/10/26 21:56:28
can blocking pool still be running while this is d
xiyuan
2012/10/26 22:39:25
Right now, the only ExternalLoader instance is own
| |
| 96 } | |
| 97 | |
| 98 ExternalLoader::~ExternalLoader() { | |
| 99 DCHECK_EQ(loader_instance, this); | |
| 100 loader_instance = NULL; | |
| 101 } | |
| 102 | |
| 103 bool ExternalLoader::IsLoaded() { | |
| 104 return loaded_.IsSignaled(); | |
| 105 } | |
| 106 | |
| 107 void ExternalLoader::WaitForLoad() { | |
| 108 CHECK(loaded_.TimedWait(base::TimeDelta::FromSeconds(kTimeoutSecs))); | |
| 109 } | |
| 110 | |
| 111 void ExternalLoader::LoadOnBlockingPool() { | |
| 112 FilePath ordinals_file; | |
| 113 CHECK(PathService::Get(chrome::FILE_DEFAULT_APP_ORDER, &ordinals_file)); | |
| 114 | |
| 115 scoped_ptr<base::ListValue> ordinals_value( | |
| 116 ReadExternalOrdinalFile(ordinals_file)); | |
| 117 if (ordinals_value) { | |
| 118 for (size_t i = 0; i < ordinals_value->GetSize(); ++i) { | |
| 119 std::string app_id; | |
| 120 CHECK(ordinals_value->GetString(i, &app_id)); | |
| 121 app_ids_.push_back(app_id); | |
| 122 } | |
| 123 } else { | |
| 124 GetDefault(&app_ids_); | |
| 125 } | |
| 126 | |
| 127 loaded_.Signal(); | |
| 128 } | |
| 129 | |
| 130 void Get(std::vector<std::string>* app_ids) { | |
| 131 // |loader_instance| could be NULL for test. | |
| 132 if (!loader_instance) { | |
| 133 GetDefault(app_ids); | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 CHECK(loader_instance->IsLoaded()); | |
| 138 *app_ids = loader_instance->app_ids(); | |
| 139 } | |
| 140 | |
| 141 } // namespace default_app_order | |
| 142 } // namespace chromeos | |
| OLD | NEW |