Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2011 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/chromeos/keyboard_overlay_ui.h"
      6 
      7 #include "base/callback.h"
      8 #include "base/memory/weak_ptr.h"
      9 #include "base/values.h"
     10 #include "chrome/browser/chromeos/cros/cros_library.h"
     11 #include "chrome/browser/chromeos/cros/input_method_library.h"
     12 #include "chrome/browser/chromeos/input_method/input_method_util.h"
     13 #include "chrome/browser/profiles/profile.h"
     14 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
     15 #include "chrome/common/jstemplate_builder.h"
     16 #include "chrome/common/url_constants.h"
     17 #include "content/browser/browser_thread.h"
     18 #include "content/browser/tab_contents/tab_contents.h"
     19 #include "grit/browser_resources.h"
     20 #include "grit/generated_resources.h"
     21 #include "third_party/cros/chromeos_input_method.h"
     22 #include "ui/base/l10n/l10n_util.h"
     23 #include "ui/base/resource/resource_bundle.h"
     24 
     25 
     26 class KeyboardOverlayUIHTMLSource : public ChromeURLDataManager::DataSource {
     27  public:
     28   KeyboardOverlayUIHTMLSource();
     29 
     30   // Called when the keyboard overlay has requested a resource underneath
     31   // the path we registered.
     32   virtual void StartDataRequest(const std::string& path,
     33                                 bool is_incognito,
     34                                 int request_id);
     35   virtual std::string GetMimeType(const std::string&) const {
     36     return "text/html";
     37   }
     38 
     39  private:
     40   ~KeyboardOverlayUIHTMLSource() {}
     41 
     42   DISALLOW_COPY_AND_ASSIGN(KeyboardOverlayUIHTMLSource);
     43 };
     44 
     45 
     46 // The handler for Javascript messages related to the "keyboardoverlay" view.
     47 class KeyboardOverlayHandler
     48     : public WebUIMessageHandler,
     49       public base::SupportsWeakPtr<KeyboardOverlayHandler> {
     50  public:
     51   KeyboardOverlayHandler();
     52   virtual ~KeyboardOverlayHandler();
     53 
     54   // WebUIMessageHandler implementation.
     55   virtual WebUIMessageHandler* Attach(WebUI* web_ui);
     56   virtual void RegisterMessages();
     57 
     58  private:
     59   // Called when the page requires the keyboard overaly ID corresponding to the
     60   // current input method or keyboard layout during initialization.
     61   void GetKeyboardOverlayId(const ListValue* args);
     62 
     63   DISALLOW_COPY_AND_ASSIGN(KeyboardOverlayHandler);
     64 };
     65 
     66 ////////////////////////////////////////////////////////////////////////////////
     67 //
     68 // KeyboardOverlayUIHTMLSource
     69 //
     70 ////////////////////////////////////////////////////////////////////////////////
     71 
     72 KeyboardOverlayUIHTMLSource::KeyboardOverlayUIHTMLSource()
     73     : DataSource(chrome::kChromeUIKeyboardOverlayHost,
     74                  MessageLoop::current()) {
     75 }
     76 
     77 void KeyboardOverlayUIHTMLSource::StartDataRequest(const std::string& path,
     78                                                    bool is_incognito,
     79                                                    int request_id) {
     80   DictionaryValue localized_strings;
     81   localized_strings.SetString("keyboardOverlayTitle",
     82       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_TITLE));
     83   localized_strings.SetString("keyboardOverlayInstructions",
     84       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_INSTRUCTIONS));
     85   localized_strings.SetString("keyboardOverlayInstructionsHide",
     86       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_INSTRUCTIONS_HIDE));
     87   localized_strings.SetString("keyboardOverlayActivateLastTab",
     88       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_LAST_TAB));
     89   localized_strings.SetString("keyboardOverlayActivateNextTab",
     90       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_NEXT_TAB));
     91   localized_strings.SetString("keyboardOverlayActivatePreviousTab",
     92       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_PREVIOUS_TAB));
     93   localized_strings.SetString("keyboardOverlayActivateTab1",
     94       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_TAB_1));
     95   localized_strings.SetString("keyboardOverlayActivateTab2",
     96       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_TAB_2));
     97   localized_strings.SetString("keyboardOverlayActivateTab3",
     98       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_TAB_3));
     99   localized_strings.SetString("keyboardOverlayActivateTab4",
    100       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_TAB_4));
    101   localized_strings.SetString("keyboardOverlayActivateTab5",
    102       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_TAB_5));
    103   localized_strings.SetString("keyboardOverlayActivateTab6",
    104       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_TAB_6));
    105   localized_strings.SetString("keyboardOverlayActivateTab7",
    106       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_TAB_7));
    107   localized_strings.SetString("keyboardOverlayActivateTab8",
    108       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ACTIVATE_TAB_8));
    109   localized_strings.SetString("keyboardOverlayAddWwwAndComAndOpenAddress",
    110       l10n_util::GetStringUTF16(
    111           IDS_KEYBOARD_OVERLAY_ADD_WWW_AND_COM_AND_OPEN_ADDRESS));
    112   localized_strings.SetString("keyboardOverlayBookmarkCurrentPage",
    113       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_BOOKMARK_CURRENT_PAGE));
    114   localized_strings.SetString("keyboardOverlayBookmarkAllTabs",
    115       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_BOOKMARK_ALL_TABS));
    116   localized_strings.SetString("keyboardOverlayClearBrowsingDataDialog",
    117       l10n_util::GetStringUTF16(
    118           IDS_KEYBOARD_OVERLAY_CLEAR_BROWSING_DATA_DIALOG));
    119   localized_strings.SetString("keyboardOverlayCloseTab",
    120       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_CLOSE_TAB));
    121   localized_strings.SetString("keyboardOverlayCloseWindow",
    122       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_CLOSE_WINDOW));
    123   localized_strings.SetString("keyboardOverlayDeleteWord",
    124       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_DELETE_WORD));
    125   localized_strings.SetString("keyboardOverlayDeveloperTools",
    126       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_DEVELOPER_TOOLS));
    127   localized_strings.SetString("keyboardOverlayFindAgain",
    128       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FIND_AGAIN));
    129   localized_strings.SetString("keyboardOverlayFindPrevious",
    130       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FIND_PREVIOUS));
    131   localized_strings.SetString("keyboardOverlayFindPrevious",
    132       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FIND_PREVIOUS));
    133   localized_strings.SetString("keyboardOverlayFindText",
    134       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FIND_TEXT));
    135   localized_strings.SetString("keyboardOverlayFocusAddressBar",
    136       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FOCUS_ADDRESS_BAR));
    137   localized_strings.SetString("keyboardOverlayFocusAddressBar",
    138       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FOCUS_ADDRESS_BAR));
    139   localized_strings.SetString("keyboardOverlayFocusAddressBarInSearchMode",
    140       l10n_util::GetStringUTF16(
    141           IDS_KEYBOARD_OVERLAY_FOCUS_ADDRESS_BAR_IN_SEARCH_MODE));
    142   localized_strings.SetString("keyboardOverlayDomInspector",
    143       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_DOM_INSPECTOR));
    144   localized_strings.SetString("keyboardOverlayDownloads",
    145       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_DOWNLOADS));
    146   localized_strings.SetString("keyboardOverlayTaskManager",
    147       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_TASK_MANAGER));
    148   localized_strings.SetString("keyboardOverlayBack",
    149       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_BACK));
    150   localized_strings.SetString("keyboardOverlayForward",
    151       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FORWARD));
    152   localized_strings.SetString("keyboardOverlayForward",
    153       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FORWARD));
    154   localized_strings.SetString("keyboardOverlayHistory",
    155       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_HISTORY));
    156   localized_strings.SetString("keyboardOverlayNewTab",
    157       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_NEW_TAB));
    158   localized_strings.SetString("keyboardOverlayOpenAddressInNewTab",
    159       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_OPEN_ADDRESS_IN_NEW_TAB));
    160   localized_strings.SetString("keyboardOverlayNewIncognitoWindow",
    161       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_NEW_INCOGNITO_WINDOW));
    162   localized_strings.SetString("keyboardOverlayNewWindow",
    163       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_NEW_WINDOW));
    164   localized_strings.SetString("keyboardOverlayPasteAsPlainText",
    165       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_PASTE_AS_PLAIN_TEXT));
    166   localized_strings.SetString("keyboardOverlayPrint",
    167       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_PRINT));
    168   localized_strings.SetString("keyboardOverlayReloadCurrentPage",
    169       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_RELOAD_CURRENT_PAGE));
    170   localized_strings.SetString("keyboardOverlayReopenLastClosedTab",
    171       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_REOPEN_LAST_CLOSED_TAB));
    172   localized_strings.SetString("keyboardOverlayResetZoom",
    173       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_RESET_ZOOM));
    174   localized_strings.SetString("keyboardOverlaySelectWordAtATime",
    175       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_SELECT_WORD_AT_A_TIME));
    176   localized_strings.SetString("keyboardOverlaySelectWordAtATime",
    177       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_SELECT_WORD_AT_A_TIME));
    178   localized_strings.SetString("keyboardOverlayToggleBookmarkBar",
    179       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_TOGGLE_BOOKMARK_BAR));
    180   localized_strings.SetString("keyboardOverlayViewSource",
    181       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_VIEW_SOURCE));
    182   localized_strings.SetString("keyboardOverlayZoomIn",
    183       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ZOOM_IN));
    184   localized_strings.SetString("keyboardOverlayZoomOut",
    185       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_ZOOM_OUT));
    186   localized_strings.SetString("keyboardOverlayResetZoom",
    187       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_RESET_ZOOM));
    188   localized_strings.SetString("keyboardOverlayFocusAddressBarInSearchMode",
    189       l10n_util::GetStringUTF16(
    190           IDS_KEYBOARD_OVERLAY_FOCUS_ADDRESS_BAR_IN_SEARCH_MODE));
    191   localized_strings.SetString("keyboardOverlayFullScreen",
    192       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_FULL_SCREEN));
    193   localized_strings.SetString("keyboardOverlayTakeScreenshot",
    194       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_TAKE_SCREENSHOT));
    195   localized_strings.SetString("keyboardOverlayHome",
    196       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_HOME));
    197   localized_strings.SetString("keyboardOverlayEnd",
    198       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_END));
    199   localized_strings.SetString("keyboardOverlayNextWindow",
    200       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_NEXT_WINDOW));
    201   localized_strings.SetString("keyboardOverlayContentBrowser",
    202       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_CONTENT_BROWSER));
    203   localized_strings.SetString("keyboardOverlayPageUp",
    204       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_PAGE_UP));
    205   localized_strings.SetString("keyboardOverlayPageDown",
    206       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_PAGE_DOWN));
    207   localized_strings.SetString("keyboardOverlayPreviousWindow",
    208       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_PREVIOUS_WINDOW));
    209   localized_strings.SetString("keyboardOverlayUseExternalMonitor",
    210       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_USE_EXTERNAL_MONITOR));
    211   localized_strings.SetString("keyboardOverlayReloadIgnoringCache",
    212       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_RELOAD_IGNORING_CACHE));
    213   localized_strings.SetString("keyboardOverlaySave",
    214       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_SAVE));
    215   localized_strings.SetString("keyboardOverlayScrollUpOnePage",
    216       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_SCROLL_UP_ONE_PAGE));
    217   localized_strings.SetString("keyboardOverlaySettings",
    218       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_SETTINGS));
    219   localized_strings.SetString("keyboardOverlaySignOut",
    220       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_SIGN_OUT));
    221   localized_strings.SetString("keyboardOverlayUndo",
    222       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_UNDO));
    223   localized_strings.SetString("keyboardOverlayWordMove",
    224       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_WORD_MOVE));
    225   localized_strings.SetString("keyboardOverlaySelectAll",
    226       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_SELECT_ALL));
    227   localized_strings.SetString("keyboardOverlaySelectPreviousInputMethod",
    228       l10n_util::GetStringUTF16(
    229           IDS_KEYBOARD_OVERLAY_SELECT_PREVIOUS_INPUT_METHOD));
    230   localized_strings.SetString("keyboardOverlayCycleThroughInputMethods",
    231       l10n_util::GetStringUTF16(
    232           IDS_KEYBOARD_OVERLAY_CYCLE_THROUGH_INPUT_METHODS));
    233   localized_strings.SetString("keyboardOverlayCloseWindow",
    234       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_CLOSE_WINDOW));
    235   localized_strings.SetString("keyboardOverlayViewKeyboardOverlay",
    236       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_VIEW_KEYBOARD_OVERLAY));
    237   localized_strings.SetString("keyboardOverlayCut",
    238       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_CUT));
    239   localized_strings.SetString("keyboardOverlayCopy",
    240       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_COPY));
    241   localized_strings.SetString("keyboardOverlayPaste",
    242       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_PASTE));
    243   localized_strings.SetString("keyboardOverlayHelp",
    244       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_HELP));
    245   localized_strings.SetString("keyboardOverlayLockScreenOrPowerOff",
    246       l10n_util::GetStringUTF16(IDS_KEYBOARD_OVERLAY_LOCK_SCREEN_OR_POWER_OFF));
    247 
    248   static const base::StringPiece keyboard_overlay_html(
    249       ResourceBundle::GetSharedInstance().GetRawDataResource(
    250           IDR_KEYBOARD_OVERLAY_HTML));
    251   const std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
    252       keyboard_overlay_html, &localized_strings);
    253 
    254   scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
    255   html_bytes->data.resize(full_html.size());
    256   std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
    257 
    258   SendResponse(request_id, html_bytes);
    259 }
    260 
    261 ////////////////////////////////////////////////////////////////////////////////
    262 //
    263 // KeyboardOverlayHandler
    264 //
    265 ////////////////////////////////////////////////////////////////////////////////
    266 KeyboardOverlayHandler::KeyboardOverlayHandler() {
    267 }
    268 
    269 KeyboardOverlayHandler::~KeyboardOverlayHandler() {
    270 }
    271 
    272 WebUIMessageHandler* KeyboardOverlayHandler::Attach(WebUI* web_ui) {
    273   return WebUIMessageHandler::Attach(web_ui);
    274 }
    275 
    276 void KeyboardOverlayHandler::RegisterMessages() {
    277   DCHECK(web_ui_);
    278   web_ui_->RegisterMessageCallback("getKeyboardOverlayId",
    279       NewCallback(this, &KeyboardOverlayHandler::GetKeyboardOverlayId));
    280 }
    281 
    282 void KeyboardOverlayHandler::GetKeyboardOverlayId(const ListValue* args) {
    283   chromeos::InputMethodLibrary* library =
    284       chromeos::CrosLibrary::Get()->GetInputMethodLibrary();
    285   const chromeos::InputMethodDescriptor& descriptor =
    286       library->current_input_method();
    287   const std::string keyboard_overlay_id =
    288       library->GetKeyboardOverlayId(descriptor.id);
    289   StringValue param(keyboard_overlay_id);
    290   web_ui_->CallJavascriptFunction("initKeyboardOverlayId", param);
    291 }
    292 
    293 ////////////////////////////////////////////////////////////////////////////////
    294 //
    295 // KeyboardOverlayUI
    296 //
    297 ////////////////////////////////////////////////////////////////////////////////
    298 
    299 KeyboardOverlayUI::KeyboardOverlayUI(TabContents* contents)
    300     : HtmlDialogUI(contents) {
    301   KeyboardOverlayHandler* handler = new KeyboardOverlayHandler();
    302   AddMessageHandler((handler)->Attach(this));
    303   KeyboardOverlayUIHTMLSource* html_source = new KeyboardOverlayUIHTMLSource();
    304 
    305   // Set up the chrome://keyboardoverlay/ source.
    306   contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source);
    307 }
    308