1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_WEB_RESOURCE_JSON_ASYNCHRONOUS_UNPACKER_H_ 6#define CHROME_BROWSER_WEB_RESOURCE_JSON_ASYNCHRONOUS_UNPACKER_H_ 7 8#include <string> 9 10#include "base/values.h" 11 12// A delegate interface for users of JSONAsynchronousUnpacker. 13class JSONAsynchronousUnpackerDelegate { 14 public: 15 virtual ~JSONAsynchronousUnpackerDelegate() {} 16 17 // This will be called when the response is parsed properly. |parsed_json| 18 // contains the decoded information. 19 virtual void OnUnpackFinished(const base::DictionaryValue& parsed_json) = 0; 20 21 // This will be called if there is an error while parsing the data. 22 virtual void OnUnpackError(const std::string& error_message) = 0; 23}; 24 25// This class coordinates a web resource unpack and parse task which is run 26// asynchronously. Results are sent back to the delegate via one of its 27// OnUnpack<xxx> methods. 28class JSONAsynchronousUnpacker { 29 public: 30 // Creates a WebResourceServiceUnpacker appropriate for the platform. The 31 // delegate must be kept around until one of the delegate methods is called. 32 // In case the delegate is no longer valid, ClearDelegate() must be called. 33 static JSONAsynchronousUnpacker* 34 Create(JSONAsynchronousUnpackerDelegate* delegate); 35 36 virtual ~JSONAsynchronousUnpacker() {} 37 38 // Start the decoding. The concrete implementation should delete itself after 39 // calling the delegate method. 40 virtual void Start(const std::string& json_data) = 0; 41 42 // If the delegate is going away it needs to inform the unpacker about it. 43 void ClearDelegate() { 44 delegate_ = NULL; 45 }; 46 47 protected: 48 explicit JSONAsynchronousUnpacker(JSONAsynchronousUnpackerDelegate* delegate) 49 : delegate_(delegate) { 50 } 51 52 JSONAsynchronousUnpackerDelegate* delegate_; 53}; 54 55#endif // CHROME_BROWSER_WEB_RESOURCE_JSON_ASYNCHRONOUS_UNPACKER_H_ 56