Notify the webcontents of the background page of extensions that it's hidden, if...
[chromium-blink-merge.git] / win8 / metro_driver / settings_handler.cc
blob6feae24320f7390c736f7cc2e3c33de095b6a67e
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.
5 #include "stdafx.h"
6 #include "settings_handler.h"
8 // This include allows to send WM_SYSCOMMANDs to chrome.
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome_app_view.h"
11 #include "winrt_utils.h"
13 typedef winfoundtn::ITypedEventHandler<
14 winui::ApplicationSettings::SettingsPane*,
15 winui::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs*>
16 CommandsRequestedHandler;
18 namespace {
20 // String identifiers for the settings pane commands.
21 const wchar_t* kSettingsId = L"settings";
22 const wchar_t* kHelpId = L"help";
23 const wchar_t* kAboutId = L"about";
27 SettingsHandler::SettingsHandler() {
28 DVLOG(1) << __FUNCTION__;
31 SettingsHandler::~SettingsHandler() {
32 DVLOG(1) << __FUNCTION__;
35 HRESULT SettingsHandler::Initialize() {
36 mswr::ComPtr<winui::ApplicationSettings::ISettingsPaneStatics>
37 settings_pane_statics;
38 HRESULT hr = winrt_utils::CreateActivationFactory(
39 RuntimeClass_Windows_UI_ApplicationSettings_SettingsPane,
40 settings_pane_statics.GetAddressOf());
41 CheckHR(hr, "Failed to activate ISettingsPaneStatics");
43 mswr::ComPtr<winui::ApplicationSettings::ISettingsPane> settings_pane;
44 hr = settings_pane_statics->GetForCurrentView(&settings_pane);
45 CheckHR(hr, "Failed to get ISettingsPane");
47 hr = settings_pane->add_CommandsRequested(
48 mswr::Callback<CommandsRequestedHandler>(
49 this,
50 &SettingsHandler::OnSettingsCommandsRequested).Get(),
51 &settings_token_);
52 CheckHR(hr, "Failed to add CommandsRequested");
54 return hr;
57 HRESULT SettingsHandler::OnSettingsCommandsRequested(
58 winui::ApplicationSettings::ISettingsPane* settings_pane,
59 winui::ApplicationSettings::ISettingsPaneCommandsRequestedEventArgs* args) {
60 mswr::ComPtr<winui::ApplicationSettings::ISettingsCommandFactory>
61 settings_command_factory;
62 HRESULT hr = winrt_utils::CreateActivationFactory(
63 RuntimeClass_Windows_UI_ApplicationSettings_SettingsCommand,
64 settings_command_factory.GetAddressOf());
65 CheckHR(hr, "Failed to activate ISettingsCommandFactory");
67 mswr::ComPtr<winui::ApplicationSettings::ISettingsPaneCommandsRequest>
68 settings_command_request;
69 hr = args->get_Request(&settings_command_request);
70 CheckHR(hr, "Failed to get_Request");
72 mswr::ComPtr<SettingsHandler::ISettingsCommandVector> application_commands;
73 hr = settings_command_request->get_ApplicationCommands(&application_commands);
74 CheckHR(hr, "Failed to get_ApplicationCommands");
76 // TODO(mad): Internationalize the hard coded user visible strings.
77 hr = AppendNewSettingsCommand(
78 kSettingsId, L"Settings", settings_command_factory.Get(),
79 application_commands.Get());
80 CheckHR(hr, "Failed to append new settings command");
82 hr = AppendNewSettingsCommand(
83 kHelpId, L"Help", settings_command_factory.Get(),
84 application_commands.Get());
85 CheckHR(hr, "Failed to append new help command");
87 hr = AppendNewSettingsCommand(
88 kAboutId, L"About", settings_command_factory.Get(),
89 application_commands.Get());
90 CheckHR(hr, "Failed to append new about command");
92 return hr;
95 HRESULT SettingsHandler::AppendNewSettingsCommand(
96 const wchar_t* id,
97 const wchar_t* name,
98 winui::ApplicationSettings::ISettingsCommandFactory*
99 settings_command_factory,
100 SettingsHandler::ISettingsCommandVector* settings_command_vector) {
101 mswr::ComPtr<winfoundtn::IPropertyValue> settings_id;
102 HRESULT hr = GetSettingsId(id, &settings_id);
103 CheckHR(hr, "Can't get settings id");
105 mswrw::HString settings_name;
106 settings_name.Attach(MakeHString(name));
107 mswr::ComPtr<winui::Popups::IUICommand> command;
108 hr = settings_command_factory->CreateSettingsCommand(
109 settings_id.Get(),
110 settings_name.Get(),
111 mswr::Callback<winui::Popups::IUICommandInvokedHandler>(
112 &SettingsHandler::OnSettings).Get(),
113 command.GetAddressOf());
114 CheckHR(hr, "Can't create settings command");
116 hr = settings_command_vector->Append(command.Get());
117 CheckHR(hr, "Failed to append settings command");
119 return hr;
122 HRESULT SettingsHandler::OnSettings(winui::Popups::IUICommand* command) {
123 mswr::ComPtr<winfoundtn::IPropertyValue> settings_id;
124 HRESULT hr = GetSettingsId(kSettingsId, &settings_id);
125 CheckHR(hr, "Failed to get settings id");
127 mswr::ComPtr<winfoundtn::IPropertyValue> help_id;
128 hr = GetSettingsId(kHelpId, &help_id);
129 CheckHR(hr, "Failed to get settings id");
131 mswr::ComPtr<winfoundtn::IPropertyValue> about_id;
132 hr = GetSettingsId(kAboutId, &about_id);
133 CheckHR(hr, "Failed to get settings id");
135 mswr::ComPtr<winfoundtn::IPropertyValue> command_id;
136 hr = command->get_Id(&command_id);
137 CheckHR(hr, "Failed to get command id");
139 INT32 result = -1;
140 hr = winrt_utils::CompareProperties(
141 command_id.Get(), settings_id.Get(), &result);
142 CheckHR(hr, "Failed to compare ids");
144 HWND chrome_window = globals.host_windows.front().first;
146 if (result == 0) {
147 ::PostMessageW(chrome_window, WM_SYSCOMMAND, IDC_OPTIONS, 0);
148 return S_OK;
151 hr = winrt_utils::CompareProperties(command_id.Get(), help_id.Get(), &result);
152 CheckHR(hr, "Failed to compare ids");
153 if (result == 0) {
154 ::PostMessageW(chrome_window, WM_SYSCOMMAND, IDC_HELP_PAGE_VIA_MENU, 0);
155 return S_OK;
158 hr = winrt_utils::CompareProperties(
159 command_id.Get(), about_id.Get(), &result);
160 CheckHR(hr, "Failed to compare ids");
161 if (result == 0) {
162 ::PostMessageW(chrome_window, WM_SYSCOMMAND, IDC_ABOUT, 0);
163 return S_OK;
166 return S_OK;
169 HRESULT SettingsHandler::GetSettingsId(
170 const wchar_t* value, winfoundtn::IPropertyValue** settings_id) {
171 mswrw::HString property_value_string;
172 property_value_string.Attach(MakeHString(value));
173 return winrt_utils::CreateStringProperty(property_value_string.Get(),
174 settings_id);