Chrome扩展及应用开发
includeglobs/excludeglobs
- 可选的
- 由于matches是必须选的,所以只能用来限制matches匹配的页面
- 通配符?匹配任意单个字符,比matches语法灵活
参考
JavaScript getAttribute() 和 setAttribute()
object.getAttribute("attribute")
object.setAttribute("attribute","value");
参考
event_page.js
参考
unlimitedStorage
提供一个用于存储HTML5的客户端的数据,如数据库和本地存储的文件,不设限额。如果没有这个权限,扩展限制为5MB本地存储空间。
{
"manifest_version":2,
"name":"crx",
"description:"chrome extension",
"version":"0.0.0",
"permissions":["unlimitedStorage"]
}
关闭网页后,chrome.storage.StorageArea.get存储的数据不被清除。
desktop notify demo
popup和background都没有webkitNotifications。
chrome offical notification example
chrome.tabs.sendMessage 不指定tabId
有多个extension使用了chromeurloverrides 最后安装的extension起作用
chrome app启动
- chrome.exe --profile-directory=Default --app-id=
- chrome://apps/
filesystem
- chooseEntry
- Entry
- FileEntry
- DirectoryEntry
- FileWriter
- FileReader
- DirectoryReader
chooseEntry
|
create
|
V
Entry
|
derive
/\
/ +---------------------+
/ |
FileEntry DirectoryEntry
| |
+ |
/ \ +------+-------+--------------+
create contain-file-+ | | | |
| | create getFile getDirectory removeRecursively
FileWriter FileReader |
DirectoryReader
JavaScript apply()
Function.apply(obj,args)方法能接收两个参数
obj:这个对象将代替Function类里this对象
args:这个是数组,它将作为参数传给Function(args-->arguments)
参考
文字转语音
loudspeaker
参考资料
360 chrome extension development documents
crxdoczh镜像
crx samples
日期: 2014-12-19 17:30:06, 10 years and 28 days ago
实现 implementation
- PlatformThread - 平台级线程函数的封装
A namespace for low-level thread functions.
PlatformThread::Create(PlatformThread::Delegate delegate) {
CreateThreadInternal(delegate);
}
PlatformThread::CreateThreadInternal() {
params->delegate = delegate_;
CreateThread(
NULL, statck_size, ThreadFunc,params, flags, NULL);
}
DWORD ThreadFunc(param) {
delegate->ThreadMain();
}
PlatformThread::Creat调用CreateThread,ThreadFunc为thread过程函数,
delegate作为参数的一部分传入。
ThreadFunc中调用ThreadMain()。
in PlatformThread::Create the CreateThread is called,
ThreadFunc is the thread process function.
delegate is a part of the ThreadFunc parameter.
in ThreadFunc, ThreadMain function is called
- Thread - 线程抽象
A simple thread abstraction that establishes a MessageLoop on a new thread.
Thread : PlatformThread::Delegate {
// PlatformThread::Delegate methods:
virtual void ThreadMain() OVERRIDE;
}
void Thread::ThreadMain() {
// Let the thread do extra initialization.
// Let's do this before signaling we are started.
Init();
Run();
// Let the thread do extra cleanup.
CleanUp();
}
Thread继承PlatformThreadDelegate实现纯虚函数ThreadMain(),
ThreadMain默认构建MessageLoop,
也可以不构建MessageLoop,message-pump-factory.is_null(),
MessageLoop构建成功后通过PostTask ThreadQuitHelper退出线程
Thread class inherits the PlatformThreadDelegate.
the virtual ThreadMain function is implemented.
in ThreadMain function, MessageLoop is usually constructed and run.
But if the message pump's factory is null, the MessageLoop will not be constructed.
if MessageLoop is constructed, the thread exit by post ThreadQuitHelper task
message_loop_->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper));
日期: 2014-09-05 17:30:06, 10 years and 133 days ago
message loop
- 线程的事件处理循环
- 每个thread至少有一个message loop
- message loop主要处理task,依靠不同类型的message pump也可以处理其他任务,如 UI消息等
- message loop 继承MessagePump::Delegate 实现委托接口
- message loop 有可重入保护,一个任务未执行完,其他任务不可以开始
- 只有当创建嵌入message pump时,才可发生可重入,如创建模态对话框,OLE函数(拖拽),打印等。
- destruction_observers message loop 销毁时,通知此列表的对象。list结构,观察者模式
- PostTask() 多线程调用,使task在messageloop所属线程的run()中被处理
- DeleteSoon() ReleaseSoon() 保持变量,使之一直在消息循环中有效
- 运行相关函数被重新封装至RunLoop类中
- taskobservers task被处理之前和之后通知 观察者模式
- type_ 类型 与 message pump相关
- nestabletasksallowed_ 可重入标识
- 两个队列 workqueue(需要被处理的task的队列), delayedworkqueue(按时间排序),
- IncomingTaskQueue智能指针 IncomingTaskQueue批量复制到workqueue或delayedwork_queue中起到减少锁等待的作用
- MessageLoopForUI/MessageLoopForIO 提供为pump加入(删除)观察者的接口
- lazytlsptr 静态对象保持message loop指针
message loop proxy
- 多线程访问message loop对象,使用proxy管理message loop生命周期
- 返回带引用计数的message loop的智能指针
- 相关文件 messageloopproxy.cc(h) messageloopimpl.cc(h)
message pump
- 消息泵,派发task,处理非task任务
- mesage loop的pump_指向message pump
- loop按类型包含一个同类型的pump
- 用来负责处理task以外的消息,决定task处理的时机,委托给loop处理task 委托模式
- pump 的 run loop过程
- DoInernalWork() 处理非task任务,如UI消息,完成端口消息
- WaitForWork() 阻塞等待任务
- 循环调用DoInternalWork(), DoWork(), DoDelayedWork()保证所有队列任务都可被执行
- 每次循环检查退出标志
for (;;) {
bool did_work = DoInternalWork();
if (should_quit_)
break;
did_work |= delegate_->DoWork();
if (should_quit_)
break;
TimeTicks next_time;
did_work |= delegate_->DoDelayedWork(&next_time);
if (should_quit_)
break;
if (did_work)
continue;
did_work = delegate_->DoIdleWork();
if (should_quit_)
break;
if (did_work)
continue;
WaitForWork();
}
- message loop default
- evnet唤醒loop
- delayedworktime 定时处理delayed work
- message pump ozone:This class implements a message-pump for processing events from input devices Refer to MessagePump for further documentation.
- message pump dispatcher:嵌套message loop 的消息派发相关
- messagepumpobserver.h x11相关
windows 下 message pump的实现
MessagePumpWin
|
interit
|
+---------------+
| |
MessagePumpForUI MessagePumpForUI
- MessagePumpWin
- 观察者列表 在处理消息的前后分别通知观察者,观察者模式
- 延时任务运行时间
- RunState 消息运行,包括消息重入的处理
- MessagePumpForUI
- 实现为一个传统的本地windows message pump
- kMsgHaveWork是一个特殊消息作为一个event去唤醒sub pump处理task
- 避免kMsgHaveWork被重复放入消息队列 检查havework变量保证
- sub pump无task时自动挂起,有task时被自动唤醒
- WMPAINT和WMTIMER优先级总是高于其他投递的消息(如,kMsgHaveWork)
- DoRunLoop() 消息泵,相当于
while(GetMessage(&Msg,NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
- WndProcThunk() 消息处理函数
- 完成端口
- 观察者列表 在处理消息的前后分别通知观察者。观察者模式
run loop
- 消息循环
- 从message loop剥离,在栈上分配对象,速度更快;降低message loop的复杂度
- _loop为message loop对象的指针
- previousrunloop_ 上一个run loop的指针
- BeforeRun()中push Runloop stack
- AfterRun()中pop Runloop stack
- 每次run前都发一个quit task目的是如果之前有message loop则退出。
线程内消息
线程内部是如何进行的。但你需要进行费时的操作时候,可以派发一个事件和回调函数给
自身线程的MessageLoop,然后MessageLoop会调度该回调函数以执行其操作。问题是这有必要
吗?直接调用不就可以吗?答案是不可以,或者说是最好不要这么做,其原因在于,如果当前的
MessageLoop里面有优先级更高的事件和任务需要处理时,你这样做会阻碍它们的执行。
参考
理解WebKit和Chromium: 消息循环(Message Loop)
Chrome学习笔记(一):线程模型,消息循环
理解WebKit和Chromium_ Chromium的多线程机制
日期: 2014-08-29 17:30:06, 10 years and 140 days ago
task
日期: 2014-08-22 17:30:06, 10 years and 147 days ago
aligned_memory
- static存储一个类,并需要手动调用对象的构造析构函数
- lazy instance 中使用
discardable_memory
- 可回收内存,用于缓存大对象而不用担心内存不足的问题。
- 当内存不足时,系统可回收已解锁的内存资源。
- 使用listener模式,注册对内存压力进行监听。
ref_counted
- RefCounted(Base|ThreadSafeBase) 不能作为参数被用于传递应该使用scoped_refptr
- RefCounted
- RefCountedThreadSafe 线程安全引用计数
- scoped_refptr 引用计数对象的智能指针
- RefCountedDeleteOnMessageLoop 在指定的消息循环中删除的引用计数
- RefCountedStaticMemory
- RefCountedBytes - std::vector实现
- RefCountedString - std::string 实现
scoped_handle
智能 FILE 封装
scopedopenprocess
智能 进程 句柄 封装
scoped_ptr
scoper 帮助管理指针的生命周期
shared_memory
windows下用内存文件映射(file map)实现
singleton
单例模式
weak ptr
- weak pointers 指向一个对象,此对象被多个对象拥有,其中一个拥有者释放了此对象,
其他拥有者能正常运行。
- 通过工厂类获取(WeakPtrFactory), 工厂类包含一个拥有者(WeakReferenceOwner),
拥有者包含一个智能指针和一个是否有效标志
日期: 2014-08-15 17:30:06, 10 years and 154 days ago