Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: chrome/browser/profiles/profile_shortcut_manager_win.cc

Issue 10837352: Update profile desktop shortcuts (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/profiles/profile_shortcut_manager.h" 5 #include "chrome/browser/profiles/profile_shortcut_manager.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 profile_path.BaseName().value().c_str()); 108 profile_path.BaseName().value().c_str());
109 } 109 }
110 110
111 // Wrap a ShellUtil function that returns a bool so it can be posted in a 111 // Wrap a ShellUtil function that returns a bool so it can be posted in a
112 // task to the FILE thread. 112 // task to the FILE thread.
113 void CallShellUtilBoolFunction( 113 void CallShellUtilBoolFunction(
114 const base::Callback<bool(void)>& bool_function) { 114 const base::Callback<bool(void)>& bool_function) {
115 bool_function.Run(); 115 bool_function.Run();
116 } 116 }
117 117
118 // Renames an existing Chrome desktop profile shortcut. Must be called on the
119 // FILE thread.
120 void RenameChromeDesktopShortcutForProfile(
121 const string16& old_shortcut,
122 const string16& new_shortcut) {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
124 FilePath shortcut_path;
125 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system.
126 &shortcut_path)) {
127 FilePath old_profile = shortcut_path.Append(old_shortcut);
128 FilePath new_profile = shortcut_path.Append(new_shortcut);
129 file_util::Move(old_profile, new_profile);
130 }
131 }
132
133 // Updates the arguments to a Chrome desktop shortcut for a profile. Must be
134 // called on the FILE thread.
135 void UpdateChromeDesktopShortcutForProfile(
136 const string16& shortcut,
137 const string16& arguments,
138 const FilePath& profile_path,
139 const gfx::Image* avatar_image) {
sail 2012/08/21 21:59:26 looks like the avatar_image is leaked here?
Halli 2012/08/23 17:59:20 Done.
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
141 FilePath shortcut_path;
142 if (!ShellUtil::GetDesktopPath(false, &shortcut_path))
143 return;
144
145 shortcut_path = shortcut_path.Append(shortcut);
146 FilePath chrome_exe;
147 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
148 return;
149 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
150 string16 description;
151 if (!dist)
152 return;
153 else
154 description = WideToUTF16(dist->GetAppDescription());
155 FilePath icon_path = avatar_image ?
156 CreateChromeDesktopShortcutIconForProfile(profile_path, *avatar_image) :
157 FilePath();
158
159 ShellUtil::UpdateChromeShortcut(
160 dist,
161 chrome_exe.value(),
162 shortcut_path.value(),
163 arguments,
164 description,
165 icon_path.empty() ? chrome_exe.value() : icon_path.value(),
166 icon_path.empty() ? dist->GetIconIndex() : 0,
167 ShellUtil::SHORTCUT_NO_OPTIONS);
168 }
169
118 } // namespace 170 } // namespace
119 171
120 class ProfileShortcutManagerWin : public ProfileShortcutManager { 172 class ProfileShortcutManagerWin : public ProfileShortcutManager {
121 public: 173 public:
122 ProfileShortcutManagerWin(); 174 ProfileShortcutManagerWin();
123 virtual ~ProfileShortcutManagerWin(); 175 virtual ~ProfileShortcutManagerWin();
124 176
125 virtual void CreateChromeDesktopShortcut( 177 virtual void CreateChromeDesktopShortcut(
126 const FilePath& profile_path, const string16& profile_name, 178 const FilePath& profile_path, const string16& profile_name,
127 const gfx::Image& avatar_image) OVERRIDE; 179 const gfx::Image& avatar_image) OVERRIDE;
128 virtual void DeleteChromeDesktopShortcut(const FilePath& profile_path) 180 virtual void DeleteChromeDesktopShortcut(const FilePath& profile_path,
129 OVERRIDE; 181 const string16& profile_name) OVERRIDE;
130 182
131 private: 183 virtual void OnProfileAdded(const FilePath& profile_path) OVERRIDE;
132 struct ProfileShortcutInfo { 184 virtual void OnProfileWillBeRemoved(const FilePath& profile_path) OVERRIDE;
133 string16 flags; 185 virtual void OnProfileWasRemoved(const FilePath& profile_path,
134 string16 profile_name; 186 const string16& profile_name) OVERRIDE;
135 gfx::Image avatar_image; 187 virtual void OnProfileNameChanged(const FilePath& profile_path,
188 const string16& old_profile_name) OVERRIDE;
189 virtual void OnProfileAvatarChanged(const FilePath& profile_path) OVERRIDE;
136 190
137 ProfileShortcutInfo()
138 : flags(string16()),
139 profile_name(string16()),
140 avatar_image(gfx::Image()) {
141 }
142
143 ProfileShortcutInfo(
144 string16 p_flags,
145 string16 p_profile_name,
146 gfx::Image p_avatar_image)
147 : flags(p_flags),
148 profile_name(p_profile_name),
149 avatar_image(p_avatar_image) {
150 }
151 };
152
153 // TODO(hallielaine): Repopulate this map on chrome session startup
154 typedef std::map<FilePath, ProfileShortcutInfo> ProfileShortcutsMap;
155 ProfileShortcutsMap profile_shortcuts_;
156 }; 191 };
157 192
158 // static 193 // static
159 bool ProfileShortcutManager::IsFeatureEnabled() { 194 bool ProfileShortcutManager::IsFeatureEnabled() {
160 if (CommandLine::ForCurrentProcess()->HasSwitch( 195 if (CommandLine::ForCurrentProcess()->HasSwitch(
161 switches::kProfileDesktopShortcuts)) { 196 switches::kProfileDesktopShortcuts)) {
162 return true; 197 return true;
163 } 198 }
164 return false; 199 return false;
165 } 200 }
(...skipping 17 matching lines...) Expand all
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
184 FilePath chrome_exe; 219 FilePath chrome_exe;
185 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) 220 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
186 return; 221 return;
187 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 222 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
188 string16 description; 223 string16 description;
189 if (!dist) 224 if (!dist)
190 return; 225 return;
191 description = WideToUTF16(dist->GetAppDescription()); 226 description = WideToUTF16(dist->GetAppDescription());
192 227
193 // Add the profile to the map if it doesn't exist already
194 if (!profile_shortcuts_.count(profile_path)) {
195 string16 flags = CreateProfileShortcutFlags(profile_path);
196 profile_shortcuts_.insert(std::make_pair(profile_path,
197 ProfileShortcutInfo(flags, profile_name,
198 avatar_image)));
199 }
200
201 ShellUtil::CreateChromeDesktopShortcut( 228 ShellUtil::CreateChromeDesktopShortcut(
202 dist, 229 dist,
203 chrome_exe.value(), 230 chrome_exe.value(),
204 description, 231 description,
205 profile_shortcuts_[profile_path].profile_name, 232 profile_name,
206 profile_shortcuts_[profile_path].flags, 233 CreateProfileShortcutFlags(profile_path),
207 shortcut_icon.empty() ? chrome_exe.value() : shortcut_icon.value(), 234 shortcut_icon.empty() ? chrome_exe.value() : shortcut_icon.value(),
208 shortcut_icon.empty() ? dist->GetIconIndex() : 0, 235 shortcut_icon.empty() ? dist->GetIconIndex() : 0,
209 ShellUtil::CURRENT_USER, 236 ShellUtil::CURRENT_USER,
210 ShellUtil::SHORTCUT_CREATE_ALWAYS); 237 ShellUtil::SHORTCUT_CREATE_ALWAYS);
211 } 238 }
212 239
213 void ProfileShortcutManagerWin::DeleteChromeDesktopShortcut( 240 void ProfileShortcutManagerWin::DeleteChromeDesktopShortcut(
214 const FilePath& profile_path) { 241 const FilePath& profile_path, const string16& profile_name) {
215 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 242 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
216 string16 shortcut; 243 string16 shortcut;
217 // If we can find the shortcut, delete it 244 // If we can find the shortcut, delete it
218 if (ShellUtil::GetChromeShortcutName(dist, false, 245 if (ShellUtil::GetChromeShortcutName(dist, false,
219 profile_shortcuts_[profile_path].profile_name, &shortcut)) { 246 profile_name, &shortcut)) {
220 std::vector<string16> appended_names(1, shortcut); 247 std::vector<string16> appended_names(1, shortcut);
221 BrowserThread::PostTask( 248 BrowserThread::PostTask(
222 BrowserThread::FILE, FROM_HERE, 249 BrowserThread::FILE, FROM_HERE,
223 base::Bind(&CallShellUtilBoolFunction, base::Bind( 250 base::Bind(&CallShellUtilBoolFunction, base::Bind(
224 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames, 251 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames,
225 appended_names))); 252 appended_names)));
226 profile_shortcuts_.erase(profile_path);
227 } 253 }
228 } 254 }
229 255
256 void ProfileShortcutManagerWin::OnProfileAdded(const FilePath& profile_path) {
257
258 }
259
260 void ProfileShortcutManagerWin::OnProfileWillBeRemoved(
261 const FilePath& profile_path) {
262
263 }
264
265 void ProfileShortcutManagerWin::OnProfileWasRemoved(
266 const FilePath& profile_path,
267 const string16& profile_name) {
268 }
269
270 void ProfileShortcutManagerWin::OnProfileNameChanged(
271 const FilePath& profile_path,
272 const string16& old_profile_name) {
273 ProfileInfoCache& cache =
274 g_browser_process->profile_manager()->GetProfileInfoCache();
275
276 string16 new_profile_name =
277 cache.GetNameOfProfileAtIndex(
278 cache.GetIndexOfProfileWithPath(profile_path));
sail 2012/08/21 21:59:26 any time you call GetIndexOfProfileWithPath() you
Halli 2012/08/23 17:59:20 Done.
279
280 string16 old_shortcut;
281 string16 new_shortcut;
282 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
283 if (ShellUtil::GetChromeShortcutName(
284 dist, false, old_profile_name, &old_shortcut) &&
285 ShellUtil::GetChromeShortcutName(
286 dist, false, new_profile_name, &new_shortcut)) {
287 BrowserThread::PostTask(
288 BrowserThread::FILE, FROM_HERE,
289 base::Bind(&RenameChromeDesktopShortcutForProfile,
290 old_shortcut,
291 new_shortcut));
292 }
293 }
294
295 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
296 const FilePath& profile_path) {
297 ProfileInfoCache& cache =
298 g_browser_process->profile_manager()->GetProfileInfoCache();
299
300 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_path);
301 string16 profile_name =
302 cache.GetNameOfProfileAtIndex(profile_index);
303 string16 shortcut;
304
305 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
306 if (ShellUtil::GetChromeShortcutName(
307 dist, false, profile_name, &shortcut)) {
308
309 size_t new_icon_index = cache.GetAvatarIconIndexOfProfileAtIndex(
310 profile_index);
311 gfx::Image avatar_image =
312 ResourceBundle::GetSharedInstance().GetNativeImageNamed(
313 cache.GetDefaultAvatarIconResourceIDAtIndex(new_icon_index));
314
315 // We make a copy of the Image to ensure that the underlying image data is
316 // AddRef'd, in case the original copy gets deleted.
317 gfx::Image* avatar_copy = new gfx::Image(avatar_image);
sail 2012/08/21 21:59:26 I don't think this is thread safe. There was a rec
Halli 2012/08/23 17:59:20 Oshima confirmed that gfx::Image and ToSkBitmap()
318 BrowserThread::PostTask(
319 BrowserThread::FILE, FROM_HERE,
320 base::Bind(&UpdateChromeDesktopShortcutForProfile,
321 shortcut,
322 CreateProfileShortcutFlags(profile_path),
323 profile_path,
324 base::Owned(avatar_copy)));
325 }
326 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698