summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_function.h
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-28 19:56:51 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-28 19:56:51 +0000
commit703e807a70e50e32b3c4821c84c23bde69f73272 (patch)
tree615d9f7d150e5ec51821a892eecc8ebdb269b447 /chrome/browser/extensions/extension_function.h
parenta137bdac37451b1b218cf4c43a798aa3abd4c875 (diff)
downloadchromium_src-703e807a70e50e32b3c4821c84c23bde69f73272.zip
chromium_src-703e807a70e50e32b3c4821c84c23bde69f73272.tar.gz
chromium_src-703e807a70e50e32b3c4821c84c23bde69f73272.tar.bz2
Add more of the server-side plumbing for extension APIs. Also
added getTabsForWindow() to drive it. Review URL: http://codereview.chromium.org/42680 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12744 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_function.h')
-rw-r--r--chrome/browser/extensions/extension_function.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_function.h b/chrome/browser/extensions/extension_function.h
new file mode 100644
index 0000000..421df81
--- /dev/null
+++ b/chrome/browser/extensions/extension_function.h
@@ -0,0 +1,81 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_
+#define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_
+
+#include <string>
+
+#include "base/values.h"
+#include "base/scoped_ptr.h"
+
+class ExtensionFunctionDispatcher;
+
+// Base class for an extension function.
+// TODO(aa): This will have to become reference counted when we introduce APIs
+// that live beyond a single stack frame.
+class ExtensionFunction {
+ public:
+ virtual ~ExtensionFunction() {}
+
+ void set_dispatcher(ExtensionFunctionDispatcher* dispatcher) {
+ dispatcher_ = dispatcher;
+ }
+ void set_args(Value* args) { args_ = args; }
+
+ void set_callback_id(int callback_id) { callback_id_ = callback_id; }
+ int callback_id() { return callback_id_; }
+
+ Value* result() { return result_.get(); }
+ const std::string& error() { return error_; }
+
+ // Whether the extension has registered a callback and is waiting for a
+ // response. APIs can use this to avoid doing unnecessary work in the case
+ // that the extension is not expecting a response.
+ bool has_callback() { return callback_id_ != -1; }
+
+ // Execute the API. Clients should call set_args() and set_callback_id()
+ // before calling this method. Derived classes should populate result_ and
+ // error_ before returning.
+ virtual void Run() = 0;
+
+ protected:
+ void SendResponse(bool success);
+
+ // The arguments to the API. Only non-null if argument were specfied.
+ Value* args_;
+
+ // The result of the API. This should be populated by the derived class before
+ // Run() returns.
+ scoped_ptr<Value> result_;
+
+ // Any detailed error from the API. This should be populated by the derived
+ // class before Run() returns.
+ std::string error_;
+
+ private:
+ ExtensionFunctionDispatcher* dispatcher_;
+ int callback_id_;
+};
+
+
+// A SyncExtensionFunction is an ExtensionFunction that runs synchronously
+// *relative to the browser's UI thread*. Note that this has nothing to do with
+// running synchronously relative to the extension process. From the extension
+// process's point of view, the function is still asynchronous.
+//
+// This kind of function is convenient for implementing simple APIs that just
+// need to interact with things on the browser UI thread.
+class SyncExtensionFunction : public ExtensionFunction {
+ public:
+ // Derived classes should implement this method to do their work and return
+ // success/failure.
+ virtual bool RunImpl() = 0;
+
+ virtual void Run() {
+ SendResponse(RunImpl());
+ }
+};
+
+#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_