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/extensions/app_host_installer_impl_win.h" | |
6 | |
7 #include <windows.h> | |
8 #include "base/basictypes.h" | |
9 #include "base/bind.h" | |
10 #include "base/callback.h" | |
11 #include "base/logging.h" | |
12 #include "base/process_util.h" | |
13 #include "base/string16.h" | |
14 #include "base/win/object_watcher.h" | |
15 #include "base/win/registry.h" | |
16 #include "chrome/browser/extensions/app_host_installer.h" | |
17 #include "chrome/common/extensions/extension.h" | |
18 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 | |
21 namespace { | |
22 | |
23 // TODO(huangs) Refactor the constants: http://crbug.com/148538 | |
24 const wchar_t kGoogleRegClientsKey[] = L"Software\\Google\\Update\\Clients"; | |
25 | |
26 // Copied from chrome_appid.cc. | |
27 const wchar_t kBinariesAppGuid[] = L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; | |
28 | |
29 // Copied from google_update_constants.cc | |
30 const wchar_t kRegCommandLineField[] = L"CommandLine"; | |
31 const wchar_t kRegCommandsKey[] = L"Commands"; | |
32 | |
33 // Copied from util_constants.cc. | |
34 const wchar_t kCmdQuickEnableApplicationHost[] = | |
35 L"quick-enable-application-host"; | |
36 | |
37 class QuickEnableWatcher : public base::win::ObjectWatcher::Delegate { | |
erikwright (departed)
2012/10/05 21:18:23
Add class comments.
huangs
2012/10/07 01:42:25
Done.
| |
38 public: | |
39 QuickEnableWatcher(const base::Callback<void(bool)>& callback) | |
40 : callback_(callback) {} | |
41 | |
42 // base::win::ObjectWatcher::Delegate implementation. | |
43 virtual void OnObjectSignaled(HANDLE object) OVERRIDE { | |
44 int exit_code = 0; | |
45 base::TerminationStatus status( | |
46 base::GetTerminationStatus(object, &exit_code)); | |
47 if (status == base::TERMINATION_STATUS_NORMAL_TERMINATION) { | |
48 callback_.Run(true); | |
49 } else { | |
50 LOG(ERROR) << "App Host install failed, status = " << status | |
51 << ", exit code = " << exit_code; | |
52 callback_.Run(false); | |
53 } | |
54 callback_.Reset(); | |
55 } | |
56 | |
57 private: | |
58 base::Callback<void(bool)> callback_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(QuickEnableWatcher); | |
61 }; | |
62 | |
63 // Reads the path to app_host.exe from the value "UninstallString" within the | |
64 // App Host's "ClientState" registry key. Returns an empty string if the path | |
65 // does not exist or cannot be read. | |
66 string16 GetQuickEnableAppHostCommand(bool system_level) { | |
67 HKEY root_key = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
68 string16 subkey(kGoogleRegClientsKey); | |
69 subkey.append(1, L'\\').append(kBinariesAppGuid) | |
70 .append(1, L'\\').append(kRegCommandsKey) | |
71 .append(1, L'\\').append(kCmdQuickEnableApplicationHost); | |
72 base::win::RegKey reg_key; | |
73 string16 cmd; | |
74 if (reg_key.Open(root_key, subkey.c_str(), | |
75 KEY_QUERY_VALUE) == ERROR_SUCCESS) { | |
76 // If read is unsuccessful, |cmd| remains empty. | |
77 reg_key.ReadValue(kRegCommandLineField, &cmd); | |
78 } | |
79 return cmd; | |
80 } | |
81 | |
82 // Launches the Google Update command to quick-enable App Host. | |
83 // Returns true if the command is launched. | |
84 bool LaunchQuickEnableAppHost(base::win::ScopedHandle* process) { | |
85 DCHECK(!process->IsValid()); | |
86 bool success = false; | |
87 | |
88 string16 cmd_str(GetQuickEnableAppHostCommand(true)); | |
89 if (cmd_str.empty()) { // Try user-level if absent from system-level. | |
90 cmd_str = GetQuickEnableAppHostCommand(false); | |
91 } | |
92 if (!cmd_str.empty()) { | |
93 VLOG(1) << "Quick-enabling application host: " << cmd_str; | |
94 if (!base::LaunchProcess(cmd_str, base::LaunchOptions(), | |
95 process->Receive())) { | |
96 LOG(ERROR) << "Failed to quick-enable application host."; | |
97 } | |
98 success = process->IsValid(); | |
99 } | |
100 return success; | |
101 } | |
102 | |
103 } // namespace | |
104 | |
105 namespace extensions { | |
106 | |
107 using content::BrowserThread; | |
108 | |
109 AppHostInstallerImpl::AppHostInstallerImpl() {} | |
110 | |
111 // (1) is checked here. If true, proceeds to (2), else signals success. | |
112 void AppHostInstallerImpl::InstallAppHostIfNecessary( | |
113 const Extension& extension, | |
114 const base::Callback<void(bool)>& completion_callback) { | |
115 if (!BrowserThread::GetCurrentThreadIdentifier(&caller_thread_id_)) { | |
erikwright (departed)
2012/10/05 21:18:23
DCHECK completion_callback_.IsNull()
huangs
2012/10/07 01:42:25
I don't understand this (callback.h has no IsNull(
| |
116 NOTREACHED(); | |
117 return; | |
118 } | |
119 completion_callback_ = completion_callback; | |
120 | |
121 if (extension.is_platform_app()) | |
122 InstallAppHostIfNecessaryInternal(); | |
123 else | |
124 Finish(true); | |
125 } | |
126 | |
127 // (2) is checked here. If false, signals success, else installs App Host. | |
128 void AppHostInstallerImpl::InstallAppHostIfNecessaryInternal() { | |
129 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
130 if (chrome_launcher_support::IsAppHostPresent()) | |
131 Finish(true); | |
132 else | |
133 InstallAppHost(); | |
134 } else { // Redo on FILE thread. | |
135 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | |
136 &AppHostInstallerImpl::InstallAppHostIfNecessaryInternal, | |
137 base::Unretained(this))); | |
erikwright (departed)
2012/10/05 21:18:23
Can't the impl potentially be deleted before this
huangs
2012/10/07 01:42:25
I'm letting AppHostInstallerImpl manage its own li
| |
138 } | |
139 } | |
140 | |
141 void AppHostInstallerImpl::InstallAppHost() { | |
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
143 DCHECK(!process_.IsValid()); | |
144 if (!LaunchQuickEnableAppHost(&process_)) { | |
145 Finish(false); | |
146 } else { | |
147 DCHECK(process_.IsValid()); | |
148 DCHECK(!delegate_.get()); | |
149 watcher_.StopWatching(); | |
150 delegate_.reset(new QuickEnableWatcher( | |
151 base::Bind(&AppHostInstallerImpl::Finish, base::Unretained(this)))); | |
erikwright (departed)
2012/10/05 21:18:23
Here I think the Unretained is probably safe, but
huangs
2012/10/07 01:42:25
If AppHostInstallerImpl deletes itself, then this
| |
152 watcher_.StartWatching(process_, delegate_.get()); | |
153 } | |
154 } | |
155 | |
156 void AppHostInstallerImpl::Finish(bool success) { | |
157 if (BrowserThread::CurrentlyOn(caller_thread_id_)) { | |
158 completion_callback_.Run(success); | |
159 } else { // Redo on caller thread. | |
160 BrowserThread::PostTask(caller_thread_id_, FROM_HERE, base::Bind( | |
161 &AppHostInstallerImpl::Finish, base::Unretained(this), success)); | |
erikwright (departed)
2012/10/05 21:18:23
Same concern here about Unretained.
huangs
2012/10/07 01:42:25
Ditto.
| |
162 } | |
163 } | |
164 | |
165 } // namespace extensions | |
OLD | NEW |