Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: chrome/browser/safe_json_parser.cc

Issue 15342003: app_list: Add web store search. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase and address nits in #3 Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 void SafeJsonParser::Start() {
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
23 BrowserThread::PostTask(
24 BrowserThread::IO,
25 FROM_HERE,
26 base::Bind(&SafeJsonParser::StartWorkOnIOThread, this));
27 }
28
29 SafeJsonParser::~SafeJsonParser() {}
30
31 void SafeJsonParser::StartWorkOnIOThread() {
32 DCHECK(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 DCHECK(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 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
51 error_ = error_message;
52 ReportResults();
53 }
54
55 void SafeJsonParser::ReportResults() {
56 DCHECK(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() {
65 DCHECK(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 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_json_parser.h ('k') | chrome/browser/ui/app_list/search/search_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698