utility_process_host.h revision dc0f95d653279beabeb9817299e2902918ba123e
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/common/extensions/update_manifest.h" 16#include "content/browser/browser_child_process_host.h" 17#include "content/browser/browser_thread.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 // Called when an IDBKey was injected into a 93 // SerializedScriptValue. If injection failed, SerializedScriptValue is 94 // empty. 95 virtual void OnInjectIDBKeyFinished( 96 const SerializedScriptValue& new_value) {} 97 98 protected: 99 friend class base::RefCountedThreadSafe<Client>; 100 101 virtual ~Client() {} 102 103 private: 104 friend class UtilityProcessHost; 105 106 bool OnMessageReceived(const IPC::Message& message); 107 108 DISALLOW_COPY_AND_ASSIGN(Client); 109 }; 110 111 UtilityProcessHost(ResourceDispatcherHost* rdh, Client* client, 112 BrowserThread::ID client_thread_id); 113 virtual ~UtilityProcessHost(); 114 115 // Start a process to unpack the extension at the given path. The process 116 // will be given access to the directory subtree that the extension file is 117 // in, so the caller is expected to have moved that file into a quarantined 118 // location first. 119 bool StartExtensionUnpacker(const FilePath& extension); 120 121 // Start a process to unpack and parse a web resource from the given JSON 122 // data. Any links that need to be downloaded from the parsed data 123 // (thumbnails, etc.) will be unpacked in resource_dir. 124 // TODO(mrc): Right now, the unpacker just parses the JSON data, and 125 // doesn't do any unpacking. This should change once we finalize the 126 // web resource server format(s). 127 bool StartWebResourceUnpacker(const std::string& data); 128 129 // Start parsing an extensions auto-update manifest xml file. 130 bool StartUpdateManifestParse(const std::string& xml); 131 132 // Start image decoding. 133 bool StartImageDecoding(const std::vector<unsigned char>& encoded_data); 134 135 // Starts extracting |key_path| from |serialized_values|, and replies with the 136 // corresponding IndexedDBKeys via OnIDBKeysFromValuesAndKeyPathSucceeded. 137 bool StartIDBKeysFromValuesAndKeyPath( 138 int id, const std::vector<SerializedScriptValue>& serialized_values, 139 const string16& key_path); 140 141 // Starts injecting |key| into |value| via |key_path|, and replies with the 142 // updated value via OnInjectIDBKeyFinished. 143 bool StartInjectIDBKey(const IndexedDBKey& key, 144 const SerializedScriptValue& value, 145 const string16& key_path); 146 147 // Starts utility process in batch mode. Caller must call EndBatchMode() 148 // to finish the utility process. 149 bool StartBatchMode(); 150 151 // Ends the utility process. Must be called after StartBatchMode(). 152 void EndBatchMode(); 153 154 protected: 155 // Allow these methods to be overridden for tests. 156 virtual FilePath GetUtilityProcessCmd(); 157 158 private: 159 // Starts a process if necessary. Returns true if it succeeded or a process 160 // has already been started via StartBatchMode(). 161 bool StartProcess(const FilePath& exposed_dir); 162 163 // IPC messages: 164 virtual bool OnMessageReceived(const IPC::Message& message); 165 166 // BrowserChildProcessHost: 167 virtual void OnProcessCrashed(int exit_code); 168 virtual bool CanShutdown(); 169 170 // A pointer to our client interface, who will be informed of progress. 171 scoped_refptr<Client> client_; 172 BrowserThread::ID client_thread_id_; 173 // True when running in batch mode, i.e., StartBatchMode() has been called 174 // and the utility process will run until EndBatchMode(). 175 bool is_batch_mode_; 176 177 DISALLOW_COPY_AND_ASSIGN(UtilityProcessHost); 178}; 179 180#endif // CHROME_BROWSER_UTILITY_PROCESS_HOST_H_ 181