转自:http://blog.****.net/gensmusic/article/details/27089541
这里演示的是如何在chrome里添加一个extensions的api.
先看下需求:
平时做过插件的人都用过 chrome.XXx ,现在我们想自己来做个,让其在插件的js运行时候可以调用,并且在c++的代码里执行一些特殊的代码。
参考: http://dev.chromium.org/developers/design-documents/extensions/proposed-changes/creating-new-apis
其实按照google上面的文档就可以了,但是有些地方不详细,所以这里才会细致的一些来写一下步骤。
假设我们的api叫 egg,里面有一个函数 pain, 使用的时候 chrome.egg.pain()
1. 在 chrome/common/extensions/api/ 下面增加描述文件,这里有2种方式,一个是用IDL,另外一个是JSON的方式
IDL方式: egg.idl
- //
- [permissions=egg]
- namespace egg{
- callback eggCallback = void(DOMString result);
- interface Functions {
- static void post(DOMString input, eggCallback callback);
- };
- };
permissions=egg 意思是别人在使用你的时候,应该加的名字,比如,你在写插件的时候,在manifest.json 里面的 "permissions":["http://*/","storage"], 别人在用这个api的时候,就可以写 "permissions":["http://*/","storage", "egg"]
2. 在 chrome/common/extensions/api/api.gyp 里加上我们刚加的 文件
- 'dns.idl',
- 'egg.idl',
- 'downloads.idl',
3. 要告诉 extension架构,我们加了一个 egg的api 3.1 chrome/common/extensions/api/_permission_features.json
- "extension_types": ["extension", "legacy_packaged_app", "platform_app"],
- "min_manifest_version": 2
- },
- "egg": {
- "channel": "stable",
- "extension_types": ["extension", "platform_app"]
- },
- "alwaysOnTopWindows": [
- {
- "channel": "dev",
- "contexts": ["blessed_extension"],
- "dependencies": ["permission:app.runtime"]
- },
- "egg": {
- "contexts": ["blessed_extension"],
- "dependencies": ["permission:egg"]
- },
- "app.window": {
- "noparent": true,
- "contexts": ["blessed_extension"],
3.3 extensions/common/permissions/api_permission.h #注意目录目前是在 src/extensions,跟上面的不一样,跟下面也不一样
- kAdView,
- kAlarms,
- kEgg,
- kAlwaysOnTopWindows,
- kAppCurrentWindowInternal,
3.4 chrome/common/extensions/permissions/chrome_api_permissions.cc
- { APIPermission::kActiveTab, "activeTab" },
- { APIPermission::kAdView, "adview" },
- { APIPermission::kAlarms, "alarms" },
- { APIPermission::kEgg, "egg" },
- { APIPermission::kBookmark, "bookmarks", APIPermissionInfo::kFlagNone,
- IDS_EXTENSION_PROMPT_WARNING_BOOKMARKS,
- PermissionMessage::kBookmarks },
chrome/browser/extensions/extension_function_histogram_value.h,目前该文件已经迁移到extensions\browser目录
- MEDIAGALLERIES_GETMETADATA,
- INPUT_IME_SENDKEYEVENTS,
- VIRTUALKEYBOARDPRIVATE_LOCKKEYBOARD,
- EGG_PAIN,
- ENUM_BOUNDARY // Last entry: Add new entries above.
chrome/browser/extensions/api/egg/egg_api.h
- #include "chrome/common/extensions/api/egg.h" //这个自动生成的头文件,加上就行
- #include "chrome/browser/extensions/chrome_extension_function.h"
- namespace extensions {
- class EggPainFunction : public AsyncExtensionFunction {
- public:
- EggPainFunction();
- virtual bool RunImpl() OVERRIDE;
- protected:
- virtual ~EggPainFunction() {}
- private:
- DECLARE_EXTENSION_FUNCTION("egg.pain", DAFEI_POST);
- };
- }
- #include "egg_api.h"
- #include "base/logging.h"
- #include "sys/types.h"
- #include "unistd.h"
- namespace extensions {
- namespace egg = api::egg;
- EggPainFunction::EggPainFunction() {}
- bool EggPainFunction::RunImpl() {
- scoped_ptr<egg::Pain::Params> params(
- egg::Pain::Params::Create(*args_));
- EXTENSION_FUNCTION_VALIDATE(params.get());
- std::string tmp = "egg in process:";
- char buf[20] = {0};
- sprintf(buf, "%d", getpid());
- tmp.append(std::string(buf));
- LOG(INFO) << tmp;
- WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, tmp);
- return true;
- }
- }
加到gypi里 chrome/chrome_browser_extensions.gypi
- 'browser/extensions/api/alarms/alarm_manager.h',
- 'browser/extensions/api/alarms/alarms_api.cc',
- 'browser/extensions/api/alarms/alarms_api.h',
- 'browser/extensions/api/egg/egg_api.h',
- 'browser/extensions/api/egg/egg_api.cc',
- 'browser/extensions/api/app_current_window_internal/app_current_window_internal_api.cc',
- 'browser/extensions/api/app_current_window_internal/app_current_window_internal_api.h',
- 'browser/extensions/api/app_runtime/app_runtime_api.cc',
5.好了,现在去编译就OK了