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/webui/local_discovery/local_discovery_ui.h"
6
7#include "chrome/browser/profiles/profile.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/browser/ui/browser_finder.h"
10#include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h"
11#include "chrome/browser/ui/webui/metrics_handler.h"
12#include "chrome/common/pref_names.h"
13#include "chrome/common/url_constants.h"
14#include "chrome/grit/generated_resources.h"
15#include "content/public/browser/web_contents.h"
16#include "content/public/browser/web_ui.h"
17#include "content/public/browser/web_ui_data_source.h"
18#include "grit/browser_resources.h"
19
20namespace {
21
22content::WebUIDataSource* CreateLocalDiscoveryHTMLSource() {
23  content::WebUIDataSource* source =
24      content::WebUIDataSource::Create(chrome::kChromeUIDevicesHost);
25
26  source->SetDefaultResource(IDR_LOCAL_DISCOVERY_HTML);
27  source->AddResourcePath("local_discovery.css", IDR_LOCAL_DISCOVERY_CSS);
28  source->AddResourcePath("local_discovery.js", IDR_LOCAL_DISCOVERY_JS);
29  source->AddResourcePath("device.png", IDR_LOCAL_DISCOVERY_DEVICE_PNG);
30  source->AddResourcePath("printer.png", IDR_LOCAL_DISCOVERY_PRINTER_PNG);
31
32  source->SetUseJsonJSFormatV2();
33  source->AddLocalizedString("serviceRegister",
34                             IDS_LOCAL_DISCOVERY_SERVICE_REGISTER);
35  source->AddLocalizedString("manageDevice", IDS_LOCAL_DISCOVERY_MANAGE_DEVICE);
36
37  source->AddLocalizedString("registerPrinterConfirmMessage",
38                             IDS_LOCAL_DISCOVERY_REGISTER_PRINTER_CONFIRMATION);
39  source->AddLocalizedString("registerDeviceConfirmMessage",
40                             IDS_LOCAL_DISCOVERY_REGISTER_DEVICE_CONFIRMATION);
41  source->AddLocalizedString("registerUser",
42                             IDS_LOCAL_DISCOVERY_REGISTER_USER);
43  source->AddLocalizedString("confirmRegistration",
44                             IDS_LOCAL_DISCOVERY_CONFIRM_REGISTRATION);
45  source->AddLocalizedString("addingPrinter",
46                             IDS_LOCAL_DISCOVERY_ADDING_PRINTER);
47  source->AddLocalizedString("addingDevice", IDS_LOCAL_DISCOVERY_ADDING_DEVICE);
48  source->AddLocalizedString("addingError",
49                             IDS_LOCAL_DISCOVERY_ERROR_OCURRED);
50  source->AddLocalizedString("addingErrorMessage",
51                             IDS_LOCAL_DISCOVERY_ERROR_OCURRED_MESSAGE);
52  source->AddLocalizedString("addingCanceledMessage",
53                             IDS_LOCAL_DISCOVERY_REGISTER_CANCELED_ON_PRINTER);
54  source->AddLocalizedString("addingTimeoutMessage",
55                             IDS_LOCAL_DISCOVERY_REGISTER_TIMEOUT_ON_PRINTER);
56  source->AddLocalizedString("addingPrinterMessage1",
57                             IDS_LOCAL_DISCOVERY_ADDING_PRINTER_MESSAGE1);
58  source->AddLocalizedString("addingPrinterMessage2",
59                             IDS_LOCAL_DISCOVERY_ADDING_PRINTER_MESSAGE2);
60  source->AddLocalizedString("addingDeviceMessage1",
61                             IDS_LOCAL_DISCOVERY_ADDING_DEVICE_MESSAGE1);
62  source->AddLocalizedString("addingDeviceConfirmCodeMessage",
63                             IDS_LOCAL_DISCOVERY_CONFIRM_CODE_MESSAGE);
64  source->AddLocalizedString("confirmCode", IDS_LOCAL_DISCOVERY_CONFIRM_CODE);
65  source->AddLocalizedString("devicesTitle",
66                             IDS_LOCAL_DISCOVERY_DEVICES_PAGE_TITLE);
67  source->AddLocalizedString("noDescriptionDevice",
68                             IDS_LOCAL_DISCOVERY_NO_DESCRIPTION_DEVICE);
69  source->AddLocalizedString("noDescriptionPrinter",
70                             IDS_LOCAL_DISCOVERY_NO_DESCRIPTION_PRINTER);
71  source->AddLocalizedString("printersOnNetworkZero",
72                             IDS_LOCAL_DISCOVERY_PRINTERS_ON_NETWORK_ZERO);
73  source->AddLocalizedString("printersOnNetworkOne",
74                             IDS_LOCAL_DISCOVERY_PRINTERS_ON_NETWORK_ONE);
75  source->AddLocalizedString("printersOnNetworkMultiple",
76                             IDS_LOCAL_DISCOVERY_PRINTERS_ON_NETWORK_MULTIPLE);
77  source->AddLocalizedString("cancel", IDS_CANCEL);
78  source->AddLocalizedString("ok", IDS_OK);
79  source->AddLocalizedString("loading", IDS_LOCAL_DISCOVERY_LOADING);
80  source->AddLocalizedString("addPrinters", IDS_LOCAL_DISCOVERY_ADD_PRINTERS);
81  source->AddLocalizedString(
82      "noPrintersOnNetworkExplanation",
83      IDS_LOCAL_DISCOVERY_NO_PRINTERS_ON_NETWORK_EXPLANATION);
84  source->AddLocalizedString("cloudDevicesUnavailable",
85                             IDS_LOCAL_DISCOVERY_CLOUD_DEVICES_UNAVAILABLE);
86  source->AddLocalizedString("retryLoadCloudDevices",
87                             IDS_LOCAL_DISCOVERY_RETRY_LOAD_CLOUD_DEVICES);
88  source->AddLocalizedString("cloudDevicesNeedLogin",
89                             IDS_LOCAL_DISCOVERY_CLOUD_DEVICES_NEED_LOGIN);
90  source->AddLocalizedString("cloudDevicesLogin",
91                             IDS_LOCAL_DISCOVERY_CLOUD_DEVICES_LOGIN);
92  source->AddLocalizedString("registerNeedLogin",
93                             IDS_LOCAL_DISCOVERY_REGISTER_NEED_LOGIN);
94  source->AddLocalizedString("availableDevicesTitle",
95                             IDS_LOCAL_DISCOVERY_AVAILABLE_DEVICES);
96  source->AddLocalizedString("myDevicesTitle",
97                             IDS_LOCAL_DISCOVERY_MY_DEVICES);
98  source->AddLocalizedString("backButton", IDS_SETTINGS_TITLE);
99
100  // Cloud print connector-related strings.
101#if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS)
102  source->AddLocalizedString("cloudPrintConnectorEnablingButton",
103                             IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLING_BUTTON);
104  source->AddLocalizedString("cloudPrintConnectorDisabledButton",
105                             IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_DISABLED_BUTTON);
106  source->AddLocalizedString("cloudPrintConnectorEnabledButton",
107                             IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_BUTTON);
108  source->AddLocalizedString("cloudPrintName",
109                             IDS_GOOGLE_CLOUD_PRINT);
110  source->AddLocalizedString("titleConnector",
111                             IDS_LOCAL_DISCOVERY_CONNECTOR_SECTION);
112#endif
113
114  source->SetJsonPath("strings.js");
115
116  source->DisableDenyXFrameOptions();
117
118  return source;
119}
120
121}  // namespace
122
123LocalDiscoveryUI::LocalDiscoveryUI(content::WebUI* web_ui)
124    : WebUIController(web_ui) {
125  // Set up the chrome://devices/ source.
126  content::WebUIDataSource* source = CreateLocalDiscoveryHTMLSource();
127  Browser* browser =
128      chrome::FindBrowserWithWebContents(web_ui->GetWebContents());
129  // Show a back button pointing to Settings if the browser has no location bar.
130  if (browser && browser->is_trusted_source())
131    source->AddString("backButtonURL", chrome::kChromeUISettingsURL);
132  content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source);
133
134  // TODO(gene): Use LocalDiscoveryUIHandler to send updated to the devices
135  // page. For example
136  web_ui->AddMessageHandler(new local_discovery::LocalDiscoveryUIHandler());
137  web_ui->AddMessageHandler(new MetricsHandler());
138}
139
140void LocalDiscoveryUI::RegisterProfilePrefs(
141    user_prefs::PrefRegistrySyncable* registry) {
142  registry->RegisterBooleanPref(
143      prefs::kLocalDiscoveryNotificationsEnabled,
144#if defined(OS_WIN)
145      false,
146#else
147      true,
148#endif
149      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
150}
151