1// Copyright 2013 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/ui/views/frame/browser_window_property_manager_win.h"
6
7#include "base/command_line.h"
8#include "base/prefs/pref_service.h"
9#include "base/strings/utf_string_conversions.h"
10#include "base/win/windows_version.h"
11#include "chrome/browser/browser_process.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/profiles/profile_manager.h"
14#include "chrome/browser/profiles/profile_shortcut_manager_win.h"
15#include "chrome/browser/shell_integration.h"
16#include "chrome/browser/ui/host_desktop.h"
17#include "chrome/browser/ui/views/frame/browser_view.h"
18#include "chrome/browser/web_applications/web_app.h"
19#include "chrome/browser/web_applications/web_app_win.h"
20#include "chrome/common/pref_names.h"
21#include "extensions/browser/extension_registry.h"
22#include "ui/base/win/shell.h"
23#include "ui/views/win/hwnd_util.h"
24
25using extensions::ExtensionRegistry;
26
27BrowserWindowPropertyManager::BrowserWindowPropertyManager(BrowserView* view)
28    : view_(view) {
29  DCHECK(view_);
30  profile_pref_registrar_.Init(view_->browser()->profile()->GetPrefs());
31
32  // Monitor the profile icon version on Windows so that we can set the browser
33  // relaunch icon when the version changes (e.g on initial icon creation).
34  profile_pref_registrar_.Add(
35      prefs::kProfileIconVersion,
36      base::Bind(&BrowserWindowPropertyManager::OnProfileIconVersionChange,
37                 base::Unretained(this)));
38}
39
40BrowserWindowPropertyManager::~BrowserWindowPropertyManager() {
41}
42
43void BrowserWindowPropertyManager::UpdateWindowProperties(HWND hwnd) {
44  DCHECK(hwnd);
45  Browser* browser = view_->browser();
46  Profile* profile = browser->profile();
47
48  // Set the app user model id for this application to that of the application
49  // name. See http://crbug.com/7028.
50  base::string16 app_id = browser->is_app() ?
51      ShellIntegration::GetAppModelIdForProfile(
52          base::UTF8ToWide(browser->app_name()),
53          profile->GetPath()) :
54      ShellIntegration::GetChromiumModelIdForProfile(profile->GetPath());
55  base::string16 icon_path_string;
56  base::string16 command_line_string;
57  base::string16 pinned_name;
58  ProfileManager* profile_manager = g_browser_process->profile_manager();
59  ProfileShortcutManager* shortcut_manager = NULL;
60
61  // Apps set their relaunch details based on app's details.
62  if (browser->is_app()) {
63    ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
64    const extensions::Extension* extension = registry->GetExtensionById(
65        web_app::GetExtensionIdFromApplicationName(browser->app_name()),
66        ExtensionRegistry::EVERYTHING);
67    if (extension) {
68      ui::win::SetAppIdForWindow(app_id, hwnd);
69      web_app::UpdateRelaunchDetailsForApp(profile, extension, hwnd);
70      return;
71    }
72  }
73
74  // The profile manager may be NULL in testing.
75  if (profile_manager)
76    shortcut_manager = profile_manager->profile_shortcut_manager();
77
78  if (!browser->is_app() && shortcut_manager &&
79      profile->GetPrefs()->HasPrefPath(prefs::kProfileIconVersion)) {
80    const base::FilePath& profile_path = profile->GetPath();
81
82    // Set relaunch details to use profile.
83    CommandLine command_line(CommandLine::NO_PROGRAM);
84    base::FilePath icon_path;
85    shortcut_manager->GetShortcutProperties(profile_path, &command_line,
86                                            &pinned_name, &icon_path);
87    command_line_string = command_line.GetCommandLineString();
88    icon_path_string = icon_path.value();
89  }
90  ui::win::SetAppDetailsForWindow(
91      app_id,
92      icon_path_string,
93      command_line_string,
94      pinned_name,
95      hwnd);
96}
97
98// static
99scoped_ptr<BrowserWindowPropertyManager>
100    BrowserWindowPropertyManager::CreateBrowserWindowPropertyManager(
101        BrowserView* view) {
102  if (base::win::GetVersion() < base::win::VERSION_WIN7 ||
103      view->browser()->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) {
104    return scoped_ptr<BrowserWindowPropertyManager>();
105  }
106
107  return scoped_ptr<BrowserWindowPropertyManager>(
108      new BrowserWindowPropertyManager(view));
109}
110
111void BrowserWindowPropertyManager::OnProfileIconVersionChange() {
112  UpdateWindowProperties(views::HWNDForNativeWindow(view_->GetNativeWindow()));
113}
114