Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / component_updater / caps_installer_win.cc
blob4105617e2b0e0045da7daeb64db24ac0302ca819
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.
5 #include "chrome/browser/component_updater/caps_installer_win.h"
7 #include <stdint.h>
8 #include <string>
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/metrics/histogram.h"
15 #include "base/path_service.h"
16 #include "base/process/launch.h"
17 #include "base/threading/worker_pool.h"
18 #include "base/time/time.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "components/component_updater/component_updater_service.h"
21 #include "components/component_updater/default_component_installer.h"
23 namespace component_updater {
25 namespace {
27 // The values of the enum cannot be changed and should be mirrored with
28 // the values of CAPSUpdaterStep in metrics/histograms/histograms.xml. Only
29 // add new values at the end.
30 enum CAPSUmaValue {
31 CAPS_COMPONENT_READY = 0,
32 CAPS_COMPONENT_MISSING = 1,
33 CAPS_SERVICE_FAILED_TO_START = 2,
34 CAPS_SERVICE_STARTED = 3,
35 CAPS_UMA_MAX
38 void ReportUmaStep(CAPSUmaValue value) {
39 UMA_HISTOGRAM_ENUMERATION("CAPSUpdater.Step", value, CAPS_UMA_MAX);
42 // CRX hash. The extension id is: bcpgokokgekmnfkohklccmonnakdimfh.
43 const uint8_t kSha256Hash[] = {0x12, 0xf6, 0xea, 0xea, 0x64, 0xac, 0xd5, 0xae,
44 0x7a, 0xb2, 0x2c, 0xed, 0xd0, 0xa3, 0x8c, 0x57,
45 0x49, 0x05, 0x8f, 0x7d, 0x14, 0xa4, 0x22, 0x4d,
46 0x9b, 0xf6, 0x14, 0x99, 0xdf, 0xf8, 0xc9, 0xb3};
48 const base::FilePath::CharType kCapsBinary[] =
49 FILE_PATH_LITERAL("chrome_crash_svc.exe");
51 const base::FilePath::CharType kCapsDirectory[] =
52 FILE_PATH_LITERAL("Caps");
54 // This function is called from a worker thread to launch crash service.
55 void LaunchService(const base::FilePath& exe_path) {
56 base::CommandLine service_cmdline(exe_path);
57 service_cmdline.AppendSwitch("caps-update");
58 base::Process service =
59 base::LaunchProcess(service_cmdline, base::LaunchOptions());
60 CAPSUmaValue uma_step = service.IsValid() ?
61 CAPS_SERVICE_STARTED : CAPS_SERVICE_FAILED_TO_START;
62 ReportUmaStep(uma_step);
65 class CAPSInstallerTraits : public ComponentInstallerTraits {
66 public:
67 CAPSInstallerTraits() {}
68 ~CAPSInstallerTraits() override {}
70 bool VerifyInstallation(const base::DictionaryValue& manifest,
71 const base::FilePath& dir) const override {
72 bool has_binary = base::PathExists(dir.Append(kCapsBinary));
73 ReportUmaStep(has_binary ? CAPS_COMPONENT_READY : CAPS_COMPONENT_MISSING);
74 return has_binary;
77 bool CanAutoUpdate() const override { return true; }
79 bool OnCustomInstall(const base::DictionaryValue& manifest,
80 const base::FilePath& install_dir) override {
81 return true;
84 void ComponentReady(const base::Version& version,
85 const base::FilePath& install_dir,
86 scoped_ptr<base::DictionaryValue> manifest) override {
87 // Can't block here. This is usually the browser UI thread.
88 base::WorkerPool::PostTask(
89 FROM_HERE,
90 base::Bind(&LaunchService, install_dir.Append(kCapsBinary)),
91 false);
94 // Directory is usually "%appdata%\Local\Chrome\User Data\Caps".
95 base::FilePath GetBaseDirectory() const override {
96 base::FilePath user_data;
97 PathService::Get(chrome::DIR_USER_DATA, &user_data);
98 return user_data.Append(kCapsDirectory);
101 void GetHash(std::vector<uint8_t>* hash) const override {
102 hash->assign(kSha256Hash,
103 kSha256Hash + arraysize(kSha256Hash));
106 // This string is shown in chrome://components.
107 std::string GetName() const override { return "Chrome Crash Service"; }
110 } // namespace
112 void RegisterCAPSComponent(ComponentUpdateService* cus) {
113 // The component updater takes ownership of |installer|.
114 scoped_ptr<ComponentInstallerTraits> traits(
115 new CAPSInstallerTraits());
116 DefaultComponentInstaller* installer =
117 new DefaultComponentInstaller(traits.Pass());
118 installer->Register(cus, base::Closure());
121 } // namespace component_updater