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