local_discovery_ui.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
19ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
29ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
39ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// found in the LICENSE file.
49ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
59ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/browser/ui/webui/local_discovery/local_discovery_ui.h"
69ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
79ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/browser/profiles/profile.h"
86d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
96d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h"
1158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/webui/metrics_handler.h"
1268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "chrome/common/pref_names.h"
139ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/common/url_constants.h"
146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "content/public/browser/web_contents.h"
159ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "content/public/browser/web_ui.h"
169ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "content/public/browser/web_ui_data_source.h"
179ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "grit/browser_resources.h"
183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "grit/generated_resources.h"
199ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
209ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochnamespace {
219ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
229ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochcontent::WebUIDataSource* CreateLocalDiscoveryHTMLSource() {
239ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  content::WebUIDataSource* source =
24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      content::WebUIDataSource::Create(chrome::kChromeUIDevicesHost);
259ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
269ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  source->SetDefaultResource(IDR_LOCAL_DISCOVERY_HTML);
279ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  source->AddResourcePath("local_discovery.css", IDR_LOCAL_DISCOVERY_CSS);
289ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  source->AddResourcePath("local_discovery.js", IDR_LOCAL_DISCOVERY_JS);
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  source->AddResourcePath("device.png", IDR_LOCAL_DISCOVERY_DEVICE_PNG);
3058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddResourcePath("printer.png", IDR_LOCAL_DISCOVERY_PRINTER_PNG);
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  source->SetUseJsonJSFormatV2();
333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  source->AddLocalizedString("serviceRegister",
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_SERVICE_REGISTER);
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  source->AddLocalizedString("manageDevice", IDS_LOCAL_DISCOVERY_MANAGE_DEVICE);
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  source->AddLocalizedString("registerPrinterConfirmMessage",
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_REGISTER_PRINTER_CONFIRMATION);
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  source->AddLocalizedString("registerDeviceConfirmMessage",
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_REGISTER_DEVICE_CONFIRMATION);
41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  source->AddLocalizedString("registerUser",
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_REGISTER_USER);
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  source->AddLocalizedString("confirmRegistration",
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_CONFIRM_REGISTRATION);
45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  source->AddLocalizedString("addingPrinter",
46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_ADDING_PRINTER);
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  source->AddLocalizedString("addingDevice", IDS_LOCAL_DISCOVERY_ADDING_DEVICE);
48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  source->AddLocalizedString("addingError",
49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_ERROR_OCURRED);
50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  source->AddLocalizedString("addingErrorMessage",
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_ERROR_OCURRED_MESSAGE);
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  source->AddLocalizedString("addingCanceledMessage",
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_REGISTER_CANCELED_ON_PRINTER);
544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  source->AddLocalizedString("addingTimeoutMessage",
554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_REGISTER_TIMEOUT_ON_PRINTER);
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  source->AddLocalizedString("addingPrinterMessage1",
57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_ADDING_PRINTER_MESSAGE1);
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  source->AddLocalizedString("addingPrinterMessage2",
59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_ADDING_PRINTER_MESSAGE2);
605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  source->AddLocalizedString("addingDeviceMessage1",
615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_ADDING_DEVICE_MESSAGE1);
625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  source->AddLocalizedString("addingDeviceConfirmCodeMessage",
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_CONFIRM_CODE_MESSAGE);
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  source->AddLocalizedString("confirmCode", IDS_LOCAL_DISCOVERY_CONFIRM_CODE);
65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  source->AddLocalizedString("devicesTitle",
66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_DEVICES_PAGE_TITLE);
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  source->AddLocalizedString("noDescriptionDevice",
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_NO_DESCRIPTION_DEVICE);
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  source->AddLocalizedString("noDescriptionPrinter",
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_NO_DESCRIPTION_PRINTER);
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddLocalizedString("printersOnNetworkZero",
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_PRINTERS_ON_NETWORK_ZERO);
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddLocalizedString("printersOnNetworkOne",
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_PRINTERS_ON_NETWORK_ONE);
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddLocalizedString("printersOnNetworkMultiple",
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_PRINTERS_ON_NETWORK_MULTIPLE);
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddLocalizedString("cancel", IDS_CANCEL);
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddLocalizedString("ok", IDS_OK);
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddLocalizedString("loading", IDS_LOCAL_DISCOVERY_LOADING);
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddLocalizedString("addPrinters", IDS_LOCAL_DISCOVERY_ADD_PRINTERS);
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  source->AddLocalizedString(
8258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      "noPrintersOnNetworkExplanation",
8358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      IDS_LOCAL_DISCOVERY_NO_PRINTERS_ON_NETWORK_EXPLANATION);
84d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  source->AddLocalizedString("cloudDevicesUnavailable",
85d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_CLOUD_DEVICES_UNAVAILABLE);
86d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  source->AddLocalizedString("retryLoadCloudDevices",
87d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_RETRY_LOAD_CLOUD_DEVICES);
88d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  source->AddLocalizedString("cloudDevicesNeedLogin",
89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_CLOUD_DEVICES_NEED_LOGIN);
90d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  source->AddLocalizedString("cloudDevicesLogin",
91d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_CLOUD_DEVICES_LOGIN);
92d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  source->AddLocalizedString("registerNeedLogin",
93d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                             IDS_LOCAL_DISCOVERY_REGISTER_NEED_LOGIN);
9468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  source->AddLocalizedString("availableDevicesTitle",
9568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_AVAILABLE_DEVICES);
9668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  source->AddLocalizedString("myDevicesTitle",
9768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_MY_DEVICES);
986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  source->AddLocalizedString("backButton", IDS_SETTINGS_TITLE);
9968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
10068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Cloud print connector-related strings.
10168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(ENABLE_FULL_PRINTING) && !defined(OS_CHROMEOS)
10268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  source->AddLocalizedString("cloudPrintConnectorEnablingButton",
10368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                             IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLING_BUTTON);
10468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  source->AddLocalizedString("cloudPrintConnectorDisabledButton",
10568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                             IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_DISABLED_BUTTON);
10668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  source->AddLocalizedString("cloudPrintConnectorEnabledButton",
10768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                             IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_BUTTON);
10868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  source->AddLocalizedString("cloudPrintName",
10968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                             IDS_GOOGLE_CLOUD_PRINT);
11068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  source->AddLocalizedString("titleConnector",
11168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                             IDS_LOCAL_DISCOVERY_CONNECTOR_SECTION);
11268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif
1133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  source->SetJsonPath("strings.js");
1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  source->DisableDenyXFrameOptions();
117424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1189ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  return source;
1199ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}
1209ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1219ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}  // namespace
1229ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1239ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben MurdochLocalDiscoveryUI::LocalDiscoveryUI(content::WebUI* web_ui)
1249ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch    : WebUIController(web_ui) {
1259ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // Set up the chrome://devices/ source.
1266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  content::WebUIDataSource* source = CreateLocalDiscoveryHTMLSource();
1276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  Browser* browser =
1286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      chrome::FindBrowserWithWebContents(web_ui->GetWebContents());
1296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Show a back button pointing to Settings if the browser has no location bar.
1306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  if (browser && browser->is_trusted_source())
1316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    source->AddString("backButtonURL", chrome::kChromeUISettingsURL);
1326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source);
1339ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
1349ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // TODO(gene): Use LocalDiscoveryUIHandler to send updated to the devices
1359ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // page. For example
1364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  web_ui->AddMessageHandler(new local_discovery::LocalDiscoveryUIHandler());
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  web_ui->AddMessageHandler(new MetricsHandler());
1389ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}
13968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
14068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)void LocalDiscoveryUI::RegisterProfilePrefs(
14168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
14268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  registry->RegisterBooleanPref(
14368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      prefs::kLocalDiscoveryNotificationsEnabled,
14468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(OS_WIN)
14568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      false,
14668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#else
14768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      true,
14868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif
14968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
15068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
151