OLD | NEW |
(Empty) | |
| 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/login/oobe_ui.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/string_piece.h" |
| 12 #include "base/values.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/chromium_strings.h" |
| 21 #include "grit/generated_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/base/resource/resource_bundle.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 // JS API callbacks names. |
| 28 const char kJsApiScreenStateInitialize[] = "screenStateInitialize"; |
| 29 |
| 30 // Page JS API function names. |
| 31 // const char kJsApiScreenStateChanged[] = "cr.ui.Oobe.screenStateChanged"; |
| 32 |
| 33 // OOBE screen state variables which are passed to the page. |
| 34 const char kState[] = "state"; |
| 35 |
| 36 } // namespace |
| 37 |
| 38 namespace chromeos { |
| 39 |
| 40 class OobeUIHTMLSource : public ChromeURLDataManager::DataSource { |
| 41 public: |
| 42 OobeUIHTMLSource(); |
| 43 |
| 44 // Called when the network layer has requested a resource underneath |
| 45 // the path we registered. |
| 46 virtual void StartDataRequest(const std::string& path, |
| 47 bool is_incognito, |
| 48 int request_id); |
| 49 virtual std::string GetMimeType(const std::string&) const { |
| 50 return "text/html"; |
| 51 } |
| 52 |
| 53 private: |
| 54 virtual ~OobeUIHTMLSource() {} |
| 55 |
| 56 std::string service_path_; |
| 57 DISALLOW_COPY_AND_ASSIGN(OobeUIHTMLSource); |
| 58 }; |
| 59 |
| 60 // The handler for Javascript messages related to the "oobe" view. |
| 61 class OobeHandler : public WebUIMessageHandler, |
| 62 public base::SupportsWeakPtr<OobeHandler> { |
| 63 public: |
| 64 OobeHandler(); |
| 65 virtual ~OobeHandler(); |
| 66 |
| 67 // Init work after Attach. |
| 68 void Init(TabContents* contents); |
| 69 |
| 70 // WebUIMessageHandler implementation. |
| 71 virtual WebUIMessageHandler* Attach(WebUI* web_ui); |
| 72 virtual void RegisterMessages(); |
| 73 |
| 74 private: |
| 75 // Should keep this state enum in sync with similar one in JS code. |
| 76 typedef enum ScreenState { |
| 77 SCREEN_LOADING = -1, |
| 78 SCREEN_NONE = 0, |
| 79 SCREEN_WELCOME = 1, |
| 80 SCREEN_EULA = 2, |
| 81 SCREEN_UPDATE = 3, |
| 82 } ScreenState; |
| 83 |
| 84 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { |
| 85 public: |
| 86 explicit TaskProxy(const base::WeakPtr<OobeHandler>& handler) |
| 87 : handler_(handler) { |
| 88 } |
| 89 |
| 90 void HandleInitialize() { |
| 91 if (handler_) |
| 92 handler_->InitializeScreenState(); |
| 93 } |
| 94 |
| 95 private: |
| 96 base::WeakPtr<OobeHandler> handler_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(TaskProxy); |
| 99 }; |
| 100 |
| 101 // Handlers for JS WebUI messages. |
| 102 void HandleScreenStateInitialize(const ListValue* args); |
| 103 |
| 104 // Initializes current OOBE state, passes that to page. |
| 105 void InitializeScreenState(); |
| 106 |
| 107 // Updates page states. |
| 108 void UpdatePage(); |
| 109 |
| 110 TabContents* tab_contents_; |
| 111 ScreenState state_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(OobeHandler); |
| 114 }; |
| 115 |
| 116 // OobeUIHTMLSource ------------------------------------------------------- |
| 117 |
| 118 OobeUIHTMLSource::OobeUIHTMLSource() |
| 119 : DataSource(chrome::kChromeUIOobeHost, MessageLoop::current()) { |
| 120 } |
| 121 |
| 122 void OobeUIHTMLSource::StartDataRequest(const std::string& path, |
| 123 bool is_incognito, |
| 124 int request_id) { |
| 125 DictionaryValue strings; |
| 126 // OOBE title is not actually seen in UI, use title of the welcome screen. |
| 127 strings.SetString("title", |
| 128 l10n_util::GetStringUTF16(IDS_NETWORK_SELECTION_TITLE)); |
| 129 strings.SetString("welcomeScreenTitle", |
| 130 l10n_util::GetStringFUTF16(IDS_WELCOME_SCREEN_TITLE, |
| 131 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); |
| 132 strings.SetString("languageSelect", |
| 133 l10n_util::GetStringUTF16(IDS_LANGUAGE_SELECTION_SELECT)); |
| 134 strings.SetString("keyboardSelect", |
| 135 l10n_util::GetStringUTF16(IDS_KEYBOARD_SELECTION_SELECT)); |
| 136 strings.SetString("networkSelect", |
| 137 l10n_util::GetStringUTF16(IDS_NETWORK_SELECTION_SELECT)); |
| 138 strings.SetString("continue", |
| 139 l10n_util::GetStringUTF16(IDS_NETWORK_SELECTION_CONTINUE_BUTTON)); |
| 140 strings.SetString("eulaScreenTitle", |
| 141 l10n_util::GetStringFUTF16(IDS_EULA_SCREEN_TITLE, |
| 142 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); |
| 143 strings.SetString("checkboxLogging", |
| 144 l10n_util::GetStringUTF16(IDS_EULA_CHECKBOX_ENABLE_LOGGING)); |
| 145 strings.SetString("learnMore", |
| 146 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); |
| 147 strings.SetString("back", |
| 148 l10n_util::GetStringUTF16(IDS_EULA_BACK_BUTTON)); |
| 149 strings.SetString("acceptAgreement", |
| 150 l10n_util::GetStringUTF16(IDS_EULA_ACCEPT_AND_CONTINUE_BUTTON)); |
| 151 SetFontAndTextDirection(&strings); |
| 152 |
| 153 static const base::StringPiece html( |
| 154 ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_OOBE_HTML)); |
| 155 |
| 156 const std::string& full_html = jstemplate_builder::GetI18nTemplateHtml( |
| 157 html, &strings); |
| 158 |
| 159 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes()); |
| 160 html_bytes->data.resize(full_html.size()); |
| 161 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 162 |
| 163 SendResponse(request_id, html_bytes); |
| 164 } |
| 165 |
| 166 // OobeHandler ------------------------------------------------------------ |
| 167 |
| 168 OobeHandler::OobeHandler() |
| 169 : tab_contents_(NULL), |
| 170 state_(SCREEN_LOADING) { |
| 171 } |
| 172 |
| 173 OobeHandler::~OobeHandler() { |
| 174 } |
| 175 |
| 176 WebUIMessageHandler* OobeHandler::Attach(WebUI* web_ui) { |
| 177 return WebUIMessageHandler::Attach(web_ui); |
| 178 } |
| 179 |
| 180 void OobeHandler::Init(TabContents* contents) { |
| 181 tab_contents_ = contents; |
| 182 } |
| 183 |
| 184 void OobeHandler::RegisterMessages() { |
| 185 web_ui_->RegisterMessageCallback(kJsApiScreenStateInitialize, |
| 186 NewCallback(this, &OobeHandler::HandleScreenStateInitialize)); |
| 187 } |
| 188 |
| 189 void OobeHandler::HandleScreenStateInitialize(const ListValue* args) { |
| 190 const size_t kScreenStateInitializeParamCount = 0; |
| 191 if (args->GetSize() != kScreenStateInitializeParamCount) { |
| 192 NOTREACHED(); |
| 193 return; |
| 194 } |
| 195 scoped_refptr<TaskProxy> task = new TaskProxy(AsWeakPtr()); |
| 196 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 197 NewRunnableMethod(task.get(), &TaskProxy::HandleInitialize)); |
| 198 } |
| 199 |
| 200 void OobeHandler::InitializeScreenState() { |
| 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 202 // TODO(nkostylev): Integrated with OOBE flow, controllers. |
| 203 state_ = SCREEN_WELCOME; |
| 204 UpdatePage(); |
| 205 } |
| 206 |
| 207 void OobeHandler::UpdatePage() { |
| 208 DictionaryValue screen_info_dict; |
| 209 VLOG(1) << "New state: " << state_; |
| 210 screen_info_dict.SetInteger(kState, state_); |
| 211 // TODO(nkostylev): Initialize page state. |
| 212 // web_ui_->CallJavascriptFunction(kJsApiScreenStateChanged, |
| 213 // screen_info_dict); |
| 214 } |
| 215 |
| 216 // OobeUI ---------------------------------------------------------------------- |
| 217 |
| 218 OobeUI::OobeUI(TabContents* contents) : WebUI(contents) { |
| 219 OobeHandler* handler = new OobeHandler(); |
| 220 AddMessageHandler((handler)->Attach(this)); |
| 221 handler->Init(contents); |
| 222 OobeUIHTMLSource* html_source = new OobeUIHTMLSource(); |
| 223 |
| 224 // Set up the chrome://oobe/ source. |
| 225 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); |
| 226 } |
| 227 |
| 228 } // namespace chromeos |
OLD | NEW |