| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <list> | 10 #include <list> |
| 11 | 11 |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/process.h" |
| 16 #include "chrome/browser/extensions/extension_info_map.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 16 #include "content/browser/browser_thread.h" | 19 #include "content/browser/browser_thread.h" |
| 17 #include "content/common/notification_observer.h" | 20 #include "content/common/notification_observer.h" |
| 18 #include "content/common/notification_registrar.h" | 21 #include "content/common/notification_registrar.h" |
| 22 #include "ipc/ipc_message.h" |
| 19 | 23 |
| 20 class Browser; | 24 class Browser; |
| 25 class ChromeRenderMessageFilter; |
| 21 class ExtensionFunction; | 26 class ExtensionFunction; |
| 22 class ExtensionFunctionDispatcher; | 27 class ExtensionFunctionDispatcher; |
| 23 class UIThreadExtensionFunction; | 28 class UIThreadExtensionFunction; |
| 29 class IOThreadExtensionFunction; |
| 24 class ListValue; | 30 class ListValue; |
| 25 class QuotaLimitHeuristic; | 31 class QuotaLimitHeuristic; |
| 26 class RenderViewHost; | 32 class RenderViewHost; |
| 27 class Value; | 33 class Value; |
| 28 | 34 |
| 29 #define EXTENSION_FUNCTION_VALIDATE(test) do { \ | 35 #define EXTENSION_FUNCTION_VALIDATE(test) do { \ |
| 30 if (!(test)) { \ | 36 if (!(test)) { \ |
| 31 bad_message_ = true; \ | 37 bad_message_ = true; \ |
| 32 return false; \ | 38 return false; \ |
| 33 } \ | 39 } \ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 52 | 58 |
| 53 // Abstract base class for extension functions the ExtensionFunctionDispatcher | 59 // Abstract base class for extension functions the ExtensionFunctionDispatcher |
| 54 // knows how to dispatch to. | 60 // knows how to dispatch to. |
| 55 class ExtensionFunction | 61 class ExtensionFunction |
| 56 : public base::RefCountedThreadSafe<ExtensionFunction, | 62 : public base::RefCountedThreadSafe<ExtensionFunction, |
| 57 ExtensionFunctionDeleteTraits> { | 63 ExtensionFunctionDeleteTraits> { |
| 58 public: | 64 public: |
| 59 ExtensionFunction(); | 65 ExtensionFunction(); |
| 60 | 66 |
| 61 virtual UIThreadExtensionFunction* AsUIThreadExtensionFunction(); | 67 virtual UIThreadExtensionFunction* AsUIThreadExtensionFunction(); |
| 68 virtual IOThreadExtensionFunction* AsIOThreadExtensionFunction(); |
| 62 | 69 |
| 63 // Execute the API. Clients should initialize the ExtensionFunction using | 70 // Execute the API. Clients should initialize the ExtensionFunction using |
| 64 // SetArgs(), set_request_id(), and the other setters before calling this | 71 // SetArgs(), set_request_id(), and the other setters before calling this |
| 65 // method. Derived classes should be ready to return GetResult() and | 72 // method. Derived classes should be ready to return GetResult() and |
| 66 // GetError() before returning from this function. | 73 // GetError() before returning from this function. |
| 67 // Note that once Run() returns, dispatcher() can be NULL, so be sure to | 74 // Note that once Run() returns, dispatcher() can be NULL, so be sure to |
| 68 // NULL-check. | 75 // NULL-check. |
| 69 virtual void Run(); | 76 virtual void Run(); |
| 70 | 77 |
| 71 // Returns a quota limit heuristic suitable for this function. | 78 // Returns a quota limit heuristic suitable for this function. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 // Helper method for ExtensionFunctionDeleteTraits. Deletes this object. | 124 // Helper method for ExtensionFunctionDeleteTraits. Deletes this object. |
| 118 virtual void Destruct() const = 0; | 125 virtual void Destruct() const = 0; |
| 119 | 126 |
| 120 // Derived classes should implement this method to do their work and return | 127 // Derived classes should implement this method to do their work and return |
| 121 // success/failure. | 128 // success/failure. |
| 122 virtual bool RunImpl() = 0; | 129 virtual bool RunImpl() = 0; |
| 123 | 130 |
| 124 // Sends the result back to the extension. | 131 // Sends the result back to the extension. |
| 125 virtual void SendResponse(bool success) = 0; | 132 virtual void SendResponse(bool success) = 0; |
| 126 | 133 |
| 134 // Common implementation for SenderResponse. |
| 135 void SendResponseImpl(base::ProcessHandle process, |
| 136 IPC::Message::Sender* ipc_sender, |
| 137 int routing_id, |
| 138 bool success); |
| 139 |
| 127 // Called when we receive an extension api request that is invalid in a way | 140 // Called when we receive an extension api request that is invalid in a way |
| 128 // that JSON validation in the renderer should have caught. This should never | 141 // that JSON validation in the renderer should have caught. This should never |
| 129 // happen and could be an attacker trying to exploit the browser, so we crash | 142 // happen and could be an attacker trying to exploit the browser, so we crash |
| 130 // the renderer instead. | 143 // the renderer instead. |
| 131 virtual void HandleBadMessage() = 0; | 144 void HandleBadMessage(base::ProcessHandle process); |
| 132 | 145 |
| 133 // Return true if the argument to this function at |index| was provided and | 146 // Return true if the argument to this function at |index| was provided and |
| 134 // is non-null. | 147 // is non-null. |
| 135 bool HasOptionalArgument(size_t index); | 148 bool HasOptionalArgument(size_t index); |
| 136 | 149 |
| 137 // Id of this request, used to map the response back to the caller. | 150 // Id of this request, used to map the response back to the caller. |
| 138 int request_id_; | 151 int request_id_; |
| 139 | 152 |
| 140 // The ID of the Profile of this function's extension. | 153 // The ID of the Profile of this function's extension. |
| 141 ProfileId profile_id_; | 154 ProfileId profile_id_; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 public: | 263 public: |
| 251 explicit RenderViewHostTracker(UIThreadExtensionFunction* function); | 264 explicit RenderViewHostTracker(UIThreadExtensionFunction* function); |
| 252 private: | 265 private: |
| 253 virtual void Observe(NotificationType type, | 266 virtual void Observe(NotificationType type, |
| 254 const NotificationSource& source, | 267 const NotificationSource& source, |
| 255 const NotificationDetails& details); | 268 const NotificationDetails& details); |
| 256 UIThreadExtensionFunction* function_; | 269 UIThreadExtensionFunction* function_; |
| 257 NotificationRegistrar registrar_; | 270 NotificationRegistrar registrar_; |
| 258 }; | 271 }; |
| 259 | 272 |
| 260 virtual void HandleBadMessage(); | |
| 261 | |
| 262 virtual void Destruct() const; | 273 virtual void Destruct() const; |
| 263 | 274 |
| 264 scoped_ptr<RenderViewHostTracker> tracker_; | 275 scoped_ptr<RenderViewHostTracker> tracker_; |
| 276 }; |
| 265 | 277 |
| 278 // Extension functions that run on the IO thread. |
| 279 class IOThreadExtensionFunction : public ExtensionFunction { |
| 280 public: |
| 281 IOThreadExtensionFunction(); |
| 282 |
| 283 virtual IOThreadExtensionFunction* AsIOThreadExtensionFunction() OVERRIDE; |
| 284 |
| 285 void set_ipc_sender(base::WeakPtr<ChromeRenderMessageFilter> ipc_sender, |
| 286 int routing_id) { |
| 287 ipc_sender_ = ipc_sender; |
| 288 routing_id_ = routing_id; |
| 289 } |
| 290 ChromeRenderMessageFilter* ipc_sender() const { return ipc_sender_.get(); } |
| 291 int routing_id() const { return routing_id_; } |
| 292 |
| 293 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender_weak() const { |
| 294 return ipc_sender_; |
| 295 } |
| 296 |
| 297 void set_extension_info_map(const ExtensionInfoMap* extension_info_map) { |
| 298 extension_info_map_ = extension_info_map; |
| 299 } |
| 300 const ExtensionInfoMap* extension_info_map() const { |
| 301 return extension_info_map_.get(); |
| 302 } |
| 303 |
| 304 protected: |
| 305 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
| 306 friend class DeleteTask<IOThreadExtensionFunction>; |
| 307 |
| 308 virtual ~IOThreadExtensionFunction(); |
| 309 |
| 310 virtual void Destruct() const; |
| 311 |
| 312 virtual void SendResponse(bool success); |
| 313 |
| 314 private: |
| 315 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender_; |
| 316 int routing_id_; |
| 317 |
| 318 scoped_refptr<const ExtensionInfoMap> extension_info_map_; |
| 266 }; | 319 }; |
| 267 | 320 |
| 268 // Base class for an extension function that runs asynchronously *relative to | 321 // Base class for an extension function that runs asynchronously *relative to |
| 269 // the browser's UI thread*. | 322 // the browser's UI thread*. |
| 270 class AsyncExtensionFunction : public UIThreadExtensionFunction { | 323 class AsyncExtensionFunction : public UIThreadExtensionFunction { |
| 271 public: | 324 public: |
| 272 AsyncExtensionFunction(); | 325 AsyncExtensionFunction(); |
| 273 | 326 |
| 274 protected: | 327 protected: |
| 275 virtual ~AsyncExtensionFunction(); | 328 virtual ~AsyncExtensionFunction(); |
| 276 }; | 329 }; |
| 277 | 330 |
| 278 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously | 331 // A SyncExtensionFunction is an ExtensionFunction that runs synchronously |
| 279 // *relative to the browser's UI thread*. Note that this has nothing to do with | 332 // *relative to the browser's UI thread*. Note that this has nothing to do with |
| 280 // running synchronously relative to the extension process. From the extension | 333 // running synchronously relative to the extension process. From the extension |
| 281 // process's point of view, the function is still asynchronous. | 334 // process's point of view, the function is still asynchronous. |
| 282 // | 335 // |
| 283 // This kind of function is convenient for implementing simple APIs that just | 336 // This kind of function is convenient for implementing simple APIs that just |
| 284 // need to interact with things on the browser UI thread. | 337 // need to interact with things on the browser UI thread. |
| 285 class SyncExtensionFunction : public UIThreadExtensionFunction { | 338 class SyncExtensionFunction : public UIThreadExtensionFunction { |
| 286 public: | 339 public: |
| 287 SyncExtensionFunction(); | 340 SyncExtensionFunction(); |
| 288 | 341 |
| 289 virtual void Run() OVERRIDE; | 342 virtual void Run() OVERRIDE; |
| 290 | 343 |
| 291 protected: | 344 protected: |
| 292 virtual ~SyncExtensionFunction(); | 345 virtual ~SyncExtensionFunction(); |
| 346 }; |
| 293 | 347 |
| 294 private: | 348 class SyncIOThreadExtensionFunction : public IOThreadExtensionFunction { |
| 295 DISALLOW_COPY_AND_ASSIGN(SyncExtensionFunction); | 349 public: |
| 350 SyncIOThreadExtensionFunction(); |
| 351 |
| 352 virtual void Run() OVERRIDE; |
| 353 |
| 354 protected: |
| 355 virtual ~SyncIOThreadExtensionFunction(); |
| 296 }; | 356 }; |
| 297 | 357 |
| 298 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ | 358 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ |
| OLD | NEW |