From 891b7434dbeb1d2272404618de61eb0e5cb18841 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 11 Jul 2014 22:22:23 +0800 Subject: [PATCH] Separate the message parsing into a new function. --- .../browser/inspectable_web_contents_impl.cc | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index d9e988cac3df..e7a151811035 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -52,6 +52,34 @@ void DictionaryToRect(const base::DictionaryValue& dict, gfx::Rect* bounds) { *bounds = gfx::Rect(x, y, width, height); } +bool ParseMessage(const std::string& message, + std::string* method, + base::ListValue* params, + int* id) { + scoped_ptr parsed_message(base::JSONReader::Read(message)); + if (!parsed_message) + return false; + + base::DictionaryValue* dict = NULL; + if (!parsed_message->GetAsDictionary(&dict)) + return false; + if (!dict->GetString(kFrontendHostMethod, method)) + return false; + + // "params" is optional. + if (dict->HasKey(kFrontendHostParams)) { + base::ListValue* internal_params; + if (dict->GetList(kFrontendHostParams, &internal_params)) + params->Swap(internal_params); + else + return false; + } + + *id = 0; + dict->GetInteger(kFrontendHostId, id); + return true; +} + } // namespace // Implemented separately on each platform. @@ -227,24 +255,14 @@ void InspectableWebContentsImpl::ResetZoom() { void InspectableWebContentsImpl::DispatchOnEmbedder( const std::string& message) { std::string method; - base::ListValue empty_params; - base::ListValue* params = &empty_params; - - base::DictionaryValue* dict = NULL; - scoped_ptr parsed_message(base::JSONReader::Read(message)); - if (!parsed_message || - !parsed_message->GetAsDictionary(&dict) || - !dict->GetString(kFrontendHostMethod, &method) || - (dict->HasKey(kFrontendHostParams) && - !dict->GetList(kFrontendHostParams, ¶ms))) { + base::ListValue params; + int id; + if (!ParseMessage(message, &method, ¶ms, &id)) { LOG(ERROR) << "Invalid message was sent to embedder: " << message; return; } - int id = 0; - dict->GetInteger(kFrontendHostId, &id); - - std::string error = embedder_message_dispatcher_->Dispatch(method, params); + std::string error = embedder_message_dispatcher_->Dispatch(method, ¶ms); if (id) { std::string ack = base::StringPrintf( "InspectorFrontendAPI.embedderMessageAck(%d, \"%s\");", id, error.c_str());