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/printing/cloud_print/cloud_print_setup_message_handler.h" 6 7 #include "base/json/json_reader.h" 8 #include "base/json/json_writer.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "chrome/browser/printing/cloud_print/cloud_print_setup_flow.h" 11 12 WebUIMessageHandler* CloudPrintSetupMessageHandler::Attach(WebUI* web_ui) { 13 // Pass the WebUI object to the setup flow. 14 flow_->Attach(web_ui); 15 return WebUIMessageHandler::Attach(web_ui); 16 } 17 18 void CloudPrintSetupMessageHandler::RegisterMessages() { 19 web_ui_->RegisterMessageCallback("SubmitAuth", 20 NewCallback(this, &CloudPrintSetupMessageHandler::HandleSubmitAuth)); 21 web_ui_->RegisterMessageCallback("PrintTestPage", 22 NewCallback(this, &CloudPrintSetupMessageHandler::HandlePrintTestPage)); 23 web_ui_->RegisterMessageCallback("LearnMore", 24 NewCallback(this, &CloudPrintSetupMessageHandler::HandleLearnMore)); 25 } 26 27 void CloudPrintSetupMessageHandler::HandleSubmitAuth(const ListValue* args) { 28 std::string json; 29 args->GetString(0, &json); 30 std::string username, password, captcha, access_code; 31 if (json.empty()) { 32 NOTREACHED() << "Empty json string"; 33 return; 34 } 35 36 scoped_ptr<Value> parsed_value(base::JSONReader::Read(json, false)); 37 if (!parsed_value.get() || !parsed_value->IsType(Value::TYPE_DICTIONARY)) { 38 NOTREACHED() << "Unable to parse auth data"; 39 return; 40 } 41 42 DictionaryValue* result = static_cast<DictionaryValue*>(parsed_value.get()); 43 if (!result->GetString("user", &username) || 44 !result->GetString("pass", &password) || 45 !result->GetString("captcha", &captcha) || 46 !result->GetString("access_code", &access_code)) { 47 NOTREACHED() << "Unable to parse auth data"; 48 return; 49 } 50 51 // Pass the information to the flow. 52 if (flow_) 53 flow_->OnUserSubmittedAuth(username, password, captcha, access_code); 54 } 55 56 void CloudPrintSetupMessageHandler::HandlePrintTestPage(const ListValue* args) { 57 if (flow_) 58 flow_->OnUserClickedPrintTestPage(); 59 } 60 61 void CloudPrintSetupMessageHandler::HandleLearnMore(const ListValue* args) { 62 if (flow_) 63 flow_->OnUserClickedLearnMore(); 64 } 65