utility_process_host.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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_UTILITY_PROCESS_HOST_H_ 6#define CHROME_BROWSER_UTILITY_PROCESS_HOST_H_ 7#pragma once 8 9#include <string> 10#include <vector> 11 12#include "base/basictypes.h" 13#include "base/ref_counted.h" 14#include "base/task.h" 15#include "chrome/browser/browser_child_process_host.h" 16#include "chrome/browser/browser_thread.h" 17#include "chrome/common/extensions/update_manifest.h" 18 19class DictionaryValue; 20class IndexedDBKey; 21class SerializedScriptValue; 22class SkBitmap; 23 24// This class acts as the browser-side host to a utility child process. A 25// utility process is a short-lived sandboxed process that is created to run 26// a specific task. This class lives solely on the IO thread. 27// If you need a single method call in the sandbox, use StartFooBar(p). 28// If you need multiple batches of work to be done in the sandboxed process, 29// use StartBatchMode(), then multiple calls to StartFooBar(p), 30// then finish with EndBatchMode(). 31class UtilityProcessHost : public BrowserChildProcessHost { 32 public: 33 // An interface to be implemented by consumers of the utility process to 34 // get results back. All functions are called on the thread passed along 35 // to UtilityProcessHost. 36 class Client : public base::RefCountedThreadSafe<Client> { 37 public: 38 Client() {} 39 40 // Called when the process has crashed. 41 virtual void OnProcessCrashed(int exit_code) {} 42 43 // Called when the extension has unpacked successfully. |manifest| is the 44 // parsed manifest.json file. |catalogs| contains list of all parsed 45 // message catalogs. |images| contains a list of decoded images and the 46 // associated paths where those images live on disk. 47 virtual void OnUnpackExtensionSucceeded(const DictionaryValue& manifest) {} 48 49 // Called when an error occurred while unpacking the extension. 50 // |error_message| contains a description of the problem. 51 virtual void OnUnpackExtensionFailed(const std::string& error_message) {} 52 53 // Called when the web resource has been successfully parsed. |json_data| 54 // contains the parsed list of web resource items downloaded from the 55 // web resource server. 56 virtual void OnUnpackWebResourceSucceeded( 57 const DictionaryValue& json_data) {} 58 59 // Called when an error occurred while parsing the resource data. 60 // |error_message| contains a description of the problem. 61 virtual void OnUnpackWebResourceFailed(const std::string& error_message) {} 62 63 // Called when an update manifest xml file was successfully parsed. 64 virtual void OnParseUpdateManifestSucceeded( 65 const UpdateManifest::Results& results) {} 66 67 // Called when an update manifest xml file failed parsing. |error_message| 68 // contains details suitable for logging. 69 virtual void OnParseUpdateManifestFailed( 70 const std::string& error_message) {} 71 72 // Called when image data was successfully decoded. |decoded_image| 73 // stores the result. 74 virtual void OnDecodeImageSucceeded( 75 const SkBitmap& decoded_image) {} 76 77 // Called when image data decoding failed. 78 virtual void OnDecodeImageFailed() {} 79 80 // Called when we have successfully obtained the IndexedDBKey after 81 // a call to StartIDBKeysFromValuesAndKeyPath. 82 // |id| is the corresponding identifier. 83 // |keys| the corresponding IndexedDBKey. 84 virtual void OnIDBKeysFromValuesAndKeyPathSucceeded( 85 int id, const std::vector<IndexedDBKey>& keys) {} 86 87 // Called when IDBKeyPath has failed. 88 // |id| is the corresponding identifier passed on 89 // StartIDBKeysFromValuesAndKeyPath. 90 virtual void OnIDBKeysFromValuesAndKeyPathFailed(int id) {} 91 92 protected: 93 friend class base::RefCountedThreadSafe<Client>; 94 95 virtual ~Client() {} 96 97 private: 98 friend class UtilityProcessHost; 99 100 bool OnMessageReceived(const IPC::Message& message); 101 102 DISALLOW_COPY_AND_ASSIGN(Client); 103 }; 104 105 UtilityProcessHost(ResourceDispatcherHost* rdh, Client* client, 106 BrowserThread::ID client_thread_id); 107 virtual ~UtilityProcessHost(); 108 109 // Start a process to unpack the extension at the given path. The process 110 // will be given access to the directory subtree that the extension file is 111 // in, so the caller is expected to have moved that file into a quarantined 112 // location first. 113 bool StartExtensionUnpacker(const FilePath& extension); 114 115 // Start a process to unpack and parse a web resource from the given JSON 116 // data. Any links that need to be downloaded from the parsed data 117 // (thumbnails, etc.) will be unpacked in resource_dir. 118 // TODO(mrc): Right now, the unpacker just parses the JSON data, and 119 // doesn't do any unpacking. This should change once we finalize the 120 // web resource server format(s). 121 bool StartWebResourceUnpacker(const std::string& data); 122 123 // Start parsing an extensions auto-update manifest xml file. 124 bool StartUpdateManifestParse(const std::string& xml); 125 126 // Start image decoding. 127 bool StartImageDecoding(const std::vector<unsigned char>& encoded_data); 128 129 // Starts extracting |key_path| from |serialized_values|, and replies with the 130 // corresponding IndexedDBKeys via OnIDBKeysFromValuesAndKeyPathSucceeded. 131 bool StartIDBKeysFromValuesAndKeyPath( 132 int id, const std::vector<SerializedScriptValue>& serialized_values, 133 const string16& key_path); 134 135 // Starts utility process in batch mode. Caller must call EndBatchMode() 136 // to finish the utility process. 137 bool StartBatchMode(); 138 139 // Ends the utility process. Must be called after StartBatchMode(). 140 void EndBatchMode(); 141 142 protected: 143 // Allow these methods to be overridden for tests. 144 virtual FilePath GetUtilityProcessCmd(); 145 146 private: 147 // Starts a process if necessary. Returns true if it succeeded or a process 148 // has already been started via StartBatchMode(). 149 bool StartProcess(const FilePath& exposed_dir); 150 151 // IPC messages: 152 virtual bool OnMessageReceived(const IPC::Message& message); 153 154 // BrowserChildProcessHost: 155 virtual void OnProcessCrashed(int exit_code); 156 virtual bool CanShutdown(); 157 158 // A pointer to our client interface, who will be informed of progress. 159 scoped_refptr<Client> client_; 160 BrowserThread::ID client_thread_id_; 161 // True when running in batch mode, i.e., StartBatchMode() has been called 162 // and the utility process will run until EndBatchMode(). 163 bool is_batch_mode_; 164 165 DISALLOW_COPY_AND_ASSIGN(UtilityProcessHost); 166}; 167 168#endif // CHROME_BROWSER_UTILITY_PROCESS_HOST_H_ 169