Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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/safe_json_parser.h" | |
| 6 | |
| 7 #include "chrome/common/chrome_utility_messages.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 #include "content/public/browser/utility_process_host.h" | |
| 10 | |
| 11 using content::BrowserThread; | |
| 12 using content::UtilityProcessHost; | |
| 13 | |
| 14 SafeJsonParser::SafeJsonParser(const std::string& unsafe_json, | |
| 15 const SuccessCallback& success_callback, | |
| 16 const ErrorCallback& error_callback) | |
| 17 : unsafe_json_(unsafe_json), | |
| 18 success_callback_(success_callback), | |
| 19 error_callback_(error_callback) {} | |
| 20 | |
| 21 SafeJsonParser::~SafeJsonParser() {} | |
| 22 | |
| 23 void SafeJsonParser::Start() { | |
| 24 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
|
James Cook
2013/05/20 14:39:34
Could the thread assertions be DCHECKs?
xiyuan
2013/05/20 16:56:31
Done. Thread assertions are changed to use DCHECKs
| |
| 25 BrowserThread::PostTask( | |
| 26 BrowserThread::IO, | |
| 27 FROM_HERE, | |
| 28 base::Bind(&SafeJsonParser::StartWorkOnIOThread, this)); | |
| 29 } | |
| 30 | |
| 31 void SafeJsonParser::StartWorkOnIOThread() { | |
| 32 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 33 UtilityProcessHost* host = | |
| 34 UtilityProcessHost::Create( | |
| 35 this, base::MessageLoopProxy::current()); | |
| 36 host->EnableZygote(); | |
| 37 host->Send(new ChromeUtilityMsg_ParseJSON(unsafe_json_)); | |
| 38 } | |
| 39 | |
| 40 void SafeJsonParser::OnJSONParseSucceeded(const base::ListValue& wrapper) { | |
| 41 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 42 const Value* value = NULL; | |
| 43 CHECK(wrapper.Get(0, &value)); | |
| 44 | |
| 45 parsed_json_.reset(value->DeepCopy()); | |
| 46 ReportResults(); | |
| 47 } | |
| 48 | |
| 49 void SafeJsonParser::OnJSONParseFailed(const std::string& error_message) { | |
| 50 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 51 error_ = error_message; | |
| 52 ReportResults(); | |
| 53 } | |
| 54 | |
| 55 void SafeJsonParser::ReportResults() { | |
| 56 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 57 | |
| 58 BrowserThread::PostTask( | |
| 59 BrowserThread::UI, | |
| 60 FROM_HERE, | |
| 61 base::Bind(&SafeJsonParser::ReportResultOnUIThread, this)); | |
| 62 } | |
| 63 | |
| 64 void SafeJsonParser::ReportResultOnUIThread() { | |
|
James Cook
2013/05/20 14:39:34
Actually, who deletes this object? The destructor
xiyuan
2013/05/20 16:56:31
SafeJsonParser derives from UtilityProcessHostClie
| |
| 65 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 66 if (error_.empty() && parsed_json_) { | |
| 67 if (!success_callback_.is_null()) | |
| 68 success_callback_.Run(parsed_json_.Pass()); | |
| 69 } else { | |
| 70 if (!error_callback_.is_null()) | |
| 71 error_callback_.Run(error_); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 bool SafeJsonParser::OnMessageReceived(const IPC::Message& message) { | |
| 76 bool handled = true; | |
| 77 IPC_BEGIN_MESSAGE_MAP(SafeJsonParser, message) | |
| 78 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded, | |
| 79 OnJSONParseSucceeded) | |
| 80 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed, | |
| 81 OnJSONParseFailed) | |
| 82 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 83 IPC_END_MESSAGE_MAP() | |
| 84 return handled; | |
| 85 } | |
| OLD | NEW |