Home | History | Annotate | Download | only in login
      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.
      4 
      5 #ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
      6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/bind.h"
     11 #include "base/bind_helpers.h"
     12 #include "base/callback.h"
     13 #include "base/strings/string16.h"
     14 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler_utils.h"
     15 #include "content/public/browser/web_ui.h"
     16 #include "content/public/browser/web_ui_message_handler.h"
     17 #include "ui/gfx/native_widget_types.h"
     18 
     19 namespace base {
     20 class DictionaryValue;
     21 class ListValue;
     22 class Value;
     23 }
     24 
     25 namespace chromeos {
     26 
     27 // Class that collects Localized Values for translation.
     28 class LocalizedValuesBuilder {
     29  public:
     30   explicit LocalizedValuesBuilder(base::DictionaryValue* dict);
     31   // Method to declare localized value. |key| is the i18n key used in html.
     32   // |message| is text of the message.
     33   void Add(const std::string& key, const std::string& message);
     34 
     35   // Method to declare localized value. |key| is the i18n key used in html.
     36   // |message| is text of the message.
     37   void Add(const std::string& key, const base::string16& message);
     38 
     39   // Method to declare localized value. |key| is the i18n key used in html.
     40   // |message_id| is a resource id of message.
     41   void Add(const std::string& key, int message_id);
     42 
     43   // Method to declare localized value. |key| is the i18n key used in html.
     44   // |message_id| is a resource id of message. Message is expected to have
     45   // one format parameter subsituted by |a|.
     46   void AddF(const std::string& key,
     47             int message_id,
     48             const base::string16& a);
     49 
     50   // Method to declare localized value. |key| is the i18n key used in html.
     51   // |message_id| is a resource id of message. Message is expected to have
     52   // two format parameters subsituted by |a| and |b| respectively.
     53   void AddF(const std::string& key,
     54             int message_id,
     55             const base::string16& a,
     56             const base::string16& b);
     57 
     58   // Method to declare localized value. |key| is the i18n key used in html.
     59   // |message_id| is a resource id of message. Message is expected to have
     60   // one format parameter subsituted by resource identified by |message_id_a|.
     61   void AddF(const std::string& key,
     62             int message_id,
     63             int message_id_a);
     64 
     65   // Method to declare localized value. |key| is the i18n key used in html.
     66   // |message_id| is a resource id of message. Message is expected to have
     67   // two format parameters subsituted by resource identified by |message_id_a|
     68   // and |message_id_b| respectively.
     69   void AddF(const std::string& key,
     70             int message_id,
     71             int message_id_a,
     72             int message_id_b);
     73  private:
     74   // Not owned.
     75   base::DictionaryValue* dict_;
     76 };
     77 
     78 // Base class for the OOBE/Login WebUI handlers.
     79 class BaseScreenHandler : public content::WebUIMessageHandler {
     80  public:
     81   // C-tor used when JS screen prefix is not needed.
     82   BaseScreenHandler();
     83 
     84   // C-tor used when JS screen prefix is needed.
     85   explicit BaseScreenHandler(const std::string& js_screen_path);
     86 
     87   virtual ~BaseScreenHandler();
     88 
     89   // Gets localized strings to be used on the page.
     90   void GetLocalizedStrings(
     91       base::DictionaryValue* localized_strings);
     92 
     93   // This method is called when page is ready. It propagates to inherited class
     94   // via virtual Initialize() method (see below).
     95   void InitializeBase();
     96 
     97   void set_async_assets_load_id(const std::string& async_assets_load_id) {
     98     async_assets_load_id_ = async_assets_load_id;
     99   }
    100   const std::string& async_assets_load_id() const {
    101     return async_assets_load_id_;
    102   }
    103 
    104  protected:
    105   // All subclasses should implement this method to provide localized values.
    106   virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) = 0;
    107 
    108   // Subclasses can override these methods to pass additional parameters
    109   // to loadTimeData. Generally, it is a bad approach, and it should be replaced
    110   // with Context at some point.
    111   virtual void GetAdditionalParameters(base::DictionaryValue* parameters);
    112 
    113   // Shortcut for calling JS methods on WebUI side.
    114   void CallJS(const std::string& method);
    115 
    116   template<typename A1>
    117   void CallJS(const std::string& method, const A1& arg1) {
    118     web_ui()->CallJavascriptFunction(FullMethodPath(method), MakeValue(arg1));
    119   }
    120 
    121   template<typename A1, typename A2>
    122   void CallJS(const std::string& method, const A1& arg1, const A2& arg2) {
    123     web_ui()->CallJavascriptFunction(FullMethodPath(method), MakeValue(arg1),
    124                                      MakeValue(arg2));
    125   }
    126 
    127   template<typename A1, typename A2, typename A3>
    128   void CallJS(const std::string& method,
    129               const A1& arg1,
    130               const A2& arg2,
    131               const A3& arg3) {
    132     web_ui()->CallJavascriptFunction(FullMethodPath(method),
    133                                      MakeValue(arg1),
    134                                      MakeValue(arg2),
    135                                      MakeValue(arg3));
    136   }
    137 
    138   template<typename A1, typename A2, typename A3, typename A4>
    139   void CallJS(const std::string& method,
    140               const A1& arg1,
    141               const A2& arg2,
    142               const A3& arg3,
    143               const A4& arg4) {
    144     web_ui()->CallJavascriptFunction(FullMethodPath(method),
    145                                      MakeValue(arg1),
    146                                      MakeValue(arg2),
    147                                      MakeValue(arg3),
    148                                      MakeValue(arg4));
    149   }
    150 
    151   // Shortcut methods for adding WebUI callbacks.
    152   template<typename T>
    153   void AddRawCallback(const std::string& name,
    154                       void (T::*method)(const base::ListValue* args)) {
    155     web_ui()->RegisterMessageCallback(
    156         name,
    157         base::Bind(method, base::Unretained(static_cast<T*>(this))));
    158   }
    159 
    160   template<typename T>
    161   void AddCallback(const std::string& name, void (T::*method)()) {
    162     base::Callback<void()> callback =
    163         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    164     web_ui()->RegisterMessageCallback(
    165         name, base::Bind(&CallbackWrapper0, callback));
    166   }
    167 
    168   template<typename T, typename A1>
    169   void AddCallback(const std::string& name, void (T::*method)(A1 arg1)) {
    170     base::Callback<void(A1)> callback =
    171         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    172     web_ui()->RegisterMessageCallback(
    173         name, base::Bind(&CallbackWrapper1<A1>, callback));
    174   }
    175 
    176   template<typename T, typename A1, typename A2>
    177   void AddCallback(const std::string& name,
    178                    void (T::*method)(A1 arg1, A2 arg2)) {
    179     base::Callback<void(A1, A2)> callback =
    180         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    181     web_ui()->RegisterMessageCallback(
    182         name, base::Bind(&CallbackWrapper2<A1, A2>, callback));
    183   }
    184 
    185   template<typename T, typename A1, typename A2, typename A3>
    186   void AddCallback(const std::string& name,
    187                    void (T::*method)(A1 arg1, A2 arg2, A3 arg3)) {
    188     base::Callback<void(A1, A2, A3)> callback =
    189         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    190     web_ui()->RegisterMessageCallback(
    191         name, base::Bind(&CallbackWrapper3<A1, A2, A3>, callback));
    192   }
    193 
    194   template<typename T, typename A1, typename A2, typename A3, typename A4>
    195   void AddCallback(const std::string& name,
    196                    void (T::*method)(A1 arg1, A2 arg2, A3 arg3, A4 arg4)) {
    197     base::Callback<void(A1, A2, A3, A4)> callback =
    198         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    199     web_ui()->RegisterMessageCallback(
    200         name, base::Bind(&CallbackWrapper4<A1, A2, A3, A4>, callback));
    201   }
    202 
    203   template <typename Method>
    204   void AddPrefixedCallback(const std::string& unprefixed_name,
    205                            const Method& method) {
    206     AddCallback(FullMethodPath(unprefixed_name), method);
    207   }
    208 
    209   // Called when the page is ready and handler can do initialization.
    210   virtual void Initialize() = 0;
    211 
    212   // Show selected WebUI |screen|. Optionally it can pass screen initialization
    213   // data via |data| parameter.
    214   void ShowScreen(const char* screen, const base::DictionaryValue* data);
    215 
    216   // Whether page is ready.
    217   bool page_is_ready() const { return page_is_ready_; }
    218 
    219   // Returns the window which shows us.
    220   virtual gfx::NativeWindow GetNativeWindow();
    221 
    222  private:
    223   // Returns full name of JS method based on screen and method
    224   // names.
    225   std::string FullMethodPath(const std::string& method) const;
    226 
    227   // Keeps whether page is ready.
    228   bool page_is_ready_;
    229 
    230   base::DictionaryValue* localized_values_;
    231 
    232   // Full name of the corresponding JS screen object. Can be empty, if
    233   // there are no corresponding screen object or several different
    234   // objects.
    235   std::string js_screen_path_prefix_;
    236 
    237   // The string id used in the async asset load in JS. If it is set to a
    238   // non empty value, the Initialize will be deferred until the underlying load
    239   // is finished.
    240   std::string async_assets_load_id_;
    241 
    242   DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler);
    243 };
    244 
    245 }  // namespace chromeos
    246 
    247 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
    248