Small code cleanup

This commit is contained in:
Cheng Zhao 2015-10-29 10:53:48 +08:00
parent 0f89f67a84
commit 917b33dbe7
4 changed files with 24 additions and 27 deletions

View file

@ -60,6 +60,16 @@ void OnCapturePageDone(
callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap));
}
#if defined(OS_WIN)
v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
auto buffer = node::Buffer::New(isolate, static_cast<char*>(val), size);
if (buffer.IsEmpty())
return v8::Null(isolate);
else
return buffer.ToLocalChecked();
}
#endif
} // namespace
@ -190,21 +200,11 @@ void Window::OnExecuteWindowsCommand(const std::string& command_name) {
}
#if defined(OS_WIN)
v8::Local<v8::Value> Window::ToBuffer(void* val, int size) {
auto buffer = node::Buffer::New(isolate(), static_cast<char*>(val), size);
if (buffer.IsEmpty()) {
return v8::Null(isolate());
} else {
return buffer.ToLocalChecked();
}
}
void Window::OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {
if (IsWindowMessageHooked(message)) {
messages_callback_map_[message].Run(
ToBuffer(static_cast<void*>(&w_param), sizeof(WPARAM)),
ToBuffer(static_cast<void*>(&l_param), sizeof(LPARAM)));
ToBuffer(isolate(), static_cast<void*>(&w_param), sizeof(WPARAM)),
ToBuffer(isolate(), static_cast<void*>(&l_param), sizeof(LPARAM)));
}
}
#endif

View file

@ -77,8 +77,7 @@ class Window : public mate::TrackableObject<Window>,
void OnExecuteWindowsCommand(const std::string& command_name) override;
#if defined(OS_WIN)
void OnWindowMessage(UINT message, WPARAM w_param,
LPARAM l_param) override;
void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) override;
#endif
// mate::Wrappable:
@ -150,15 +149,10 @@ class Window : public mate::TrackableObject<Window>,
void SetAspectRatio(double aspect_ratio, mate::Arguments* args);
#if defined(OS_WIN)
v8::Local<v8::Value> ToBuffer(void* val, int size);
typedef base::Callback<void(v8::Local<v8::Value>,
v8::Local<v8::Value>)> MessageCallback;
typedef std::map<UINT, MessageCallback> MessageCallbackMap;
MessageCallbackMap messages_callback_map_;
bool HookWindowMessage(UINT message,
const MessageCallback& callback);
bool HookWindowMessage(UINT message, const MessageCallback& callback);
bool IsWindowMessageHooked(UINT message);
void UnhookWindowMessage(UINT message);
void UnhookAllWindowMessages();
@ -174,6 +168,11 @@ class Window : public mate::TrackableObject<Window>,
int32_t ID() const;
v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
#if defined(OS_WIN)
typedef std::map<UINT, MessageCallback> MessageCallbackMap;
MessageCallbackMap messages_callback_map_;
#endif
v8::Global<v8::Value> web_contents_;
v8::Global<v8::Value> menu_;

View file

@ -465,12 +465,10 @@ void NativeWindow::NotifyWindowExecuteWindowsCommand(
}
#if defined(OS_WIN)
void NativeWindow::NotifyWindowMessage(UINT message, WPARAM w_param,
LPARAM l_param) {
FOR_EACH_OBSERVER(
NativeWindowObserver, observers_,
OnWindowMessage(message, w_param,
l_param));
void NativeWindow::NotifyWindowMessage(
UINT message, WPARAM w_param, LPARAM l_param) {
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
OnWindowMessage(message, w_param, l_param));
}
#endif

View file

@ -61,7 +61,7 @@ class NativeWindowObserver {
// Called when window message received
#if defined(OS_WIN)
virtual void OnWindowMessage(UINT message, WPARAM wparam, LPARAM lparam) {}
virtual void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {}
#endif
// Called when renderer is hung.