Merge pull request #11607 from bughit/frame_navigation_api

frame navigation API
This commit is contained in:
Cheng Zhao 2018-02-26 15:50:11 +09:00 committed by GitHub
commit fdd66bd76d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 148 additions and 2 deletions

View file

@ -19,6 +19,7 @@
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
#include "third_party/WebKit/public/platform/WebCache.h" #include "third_party/WebKit/public/platform/WebCache.h"
#include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFrameWidget.h" #include "third_party/WebKit/public/web/WebFrameWidget.h"
#include "third_party/WebKit/public/web/WebImeTextSpan.h" #include "third_party/WebKit/public/web/WebImeTextSpan.h"
#include "third_party/WebKit/public/web/WebInputMethodController.h" #include "third_party/WebKit/public/web/WebInputMethodController.h"
@ -117,6 +118,11 @@ WebFrame::WebFrame(v8::Isolate* isolate)
Init(isolate); Init(isolate);
} }
WebFrame::WebFrame(v8::Isolate* isolate, blink::WebLocalFrame* blink_frame)
: web_frame_(blink_frame) {
Init(isolate);
}
WebFrame::~WebFrame() { WebFrame::~WebFrame() {
} }
@ -369,6 +375,79 @@ void WebFrame::ClearCache(v8::Isolate* isolate) {
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
} }
v8::Local<v8::Value> WebFrame::Opener() const {
blink::WebFrame* frame = web_frame_->Opener();
if (frame && frame->IsWebLocalFrame())
return mate::CreateHandle(isolate(),
new WebFrame(isolate(),
frame->ToWebLocalFrame())).ToV8();
else
return v8::Null(isolate());
}
v8::Local<v8::Value> WebFrame::Parent() const {
blink::WebFrame* frame = web_frame_->Parent();
if (frame && frame->IsWebLocalFrame())
return mate::CreateHandle(isolate(),
new WebFrame(isolate(),
frame->ToWebLocalFrame())).ToV8();
else
return v8::Null(isolate());
}
v8::Local<v8::Value> WebFrame::Top() const {
blink::WebFrame* frame = web_frame_->Top();
if (frame && frame->IsWebLocalFrame())
return mate::CreateHandle(isolate(),
new WebFrame(isolate(),
frame->ToWebLocalFrame())).ToV8();
else
return v8::Null(isolate());
}
v8::Local<v8::Value> WebFrame::FirstChild() const {
blink::WebFrame* frame = web_frame_->FirstChild();
if (frame && frame->IsWebLocalFrame())
return mate::CreateHandle(isolate(),
new WebFrame(isolate(),
frame->ToWebLocalFrame())).ToV8();
else
return v8::Null(isolate());
}
v8::Local<v8::Value> WebFrame::NextSibling() const {
blink::WebFrame* frame = web_frame_->NextSibling();
if (frame && frame->IsWebLocalFrame())
return mate::CreateHandle(isolate(),
new WebFrame(isolate(),
frame->ToWebLocalFrame())).ToV8();
else
return v8::Null(isolate());
}
v8::Local<v8::Value> WebFrame::GetFrameForSelector(
const std::string& selector) const {
blink::WebElement element = web_frame_->GetDocument().QuerySelector(
blink::WebString::FromUTF8(selector));
blink::WebLocalFrame* element_frame =
blink::WebLocalFrame::FromFrameOwnerElement(element);
if (element_frame)
return mate::CreateHandle(isolate(),
new WebFrame(isolate(), element_frame)).ToV8();
else
return v8::Null(isolate());
}
v8::Local<v8::Value> WebFrame::FindFrameByName(const std::string& name) const {
blink::WebLocalFrame* local_frame = web_frame_->FindFrameByName(
blink::WebString::FromUTF8(name))->ToWebLocalFrame();
if (local_frame)
return mate::CreateHandle(isolate(),
new WebFrame(isolate(), local_frame)).ToV8();
else
return v8::Null(isolate());
}
// static // static
void WebFrame::BuildPrototype( void WebFrame::BuildPrototype(
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) { v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
@ -408,7 +487,14 @@ void WebFrame::BuildPrototype(
.SetMethod("setIsolatedWorldHumanReadableName", .SetMethod("setIsolatedWorldHumanReadableName",
&WebFrame::SetIsolatedWorldHumanReadableName) &WebFrame::SetIsolatedWorldHumanReadableName)
.SetMethod("getResourceUsage", &WebFrame::GetResourceUsage) .SetMethod("getResourceUsage", &WebFrame::GetResourceUsage)
.SetMethod("clearCache", &WebFrame::ClearCache); .SetMethod("clearCache", &WebFrame::ClearCache)
.SetMethod("getFrameForSelector", &WebFrame::GetFrameForSelector)
.SetMethod("findFrameByName", &WebFrame::FindFrameByName)
.SetProperty("opener", &WebFrame::Opener)
.SetProperty("parent", &WebFrame::Parent)
.SetProperty("top", &WebFrame::Top)
.SetProperty("firstChild", &WebFrame::FirstChild)
.SetProperty("nextSibling", &WebFrame::NextSibling);
} }
} // namespace api } // namespace api

View file

@ -38,6 +38,7 @@ class WebFrame : public mate::Wrappable<WebFrame> {
private: private:
explicit WebFrame(v8::Isolate* isolate); explicit WebFrame(v8::Isolate* isolate);
explicit WebFrame(v8::Isolate* isolate, blink::WebLocalFrame* blink_frame);
~WebFrame() override; ~WebFrame() override;
void SetName(const std::string& name); void SetName(const std::string& name);
@ -93,6 +94,15 @@ class WebFrame : public mate::Wrappable<WebFrame> {
blink::WebCache::ResourceTypeStats GetResourceUsage(v8::Isolate* isolate); blink::WebCache::ResourceTypeStats GetResourceUsage(v8::Isolate* isolate);
void ClearCache(v8::Isolate* isolate); void ClearCache(v8::Isolate* isolate);
// Frame navigation
v8::Local<v8::Value> Opener() const;
v8::Local<v8::Value> Parent() const;
v8::Local<v8::Value> Top() const;
v8::Local<v8::Value> FirstChild() const;
v8::Local<v8::Value> NextSibling() const;
v8::Local<v8::Value> GetFrameForSelector(const std::string& selector) const;
v8::Local<v8::Value> FindFrameByName(const std::string& name) const;
std::unique_ptr<SpellCheckClient> spell_check_client_; std::unique_ptr<SpellCheckClient> spell_check_client_;
blink::WebLocalFrame* web_frame_; blink::WebLocalFrame* web_frame_;

View file

@ -4,6 +4,10 @@
Process: [Renderer](../glossary.md#renderer-process) Process: [Renderer](../glossary.md#renderer-process)
`webFrame` export of the electron module is an instance of the `WebFrame`
class representing the top frame of the current `BrowserWindow`. Sub-frames can
be retrieved by certain properties and methods (e.g. `webFrame.firstChild`).
An example of zooming current page to 200%. An example of zooming current page to 200%.
```javascript ```javascript
@ -14,7 +18,7 @@ webFrame.setZoomFactor(2)
## Methods ## Methods
The `webFrame` module has the following methods: The `WebFrame` class has the following instance methods:
### `webFrame.setZoomFactor(factor)` ### `webFrame.setZoomFactor(factor)`
@ -214,3 +218,49 @@ memory (i.e. you have navigated from a super heavy page to a mostly empty one,
and intend to stay there). and intend to stay there).
[spellchecker]: https://github.com/atom/node-spellchecker [spellchecker]: https://github.com/atom/node-spellchecker
### `webFrame.getFrameForSelector(selector)`
* `selector` String - CSS selector for a frame element.
Returns `WebFrame` - The frame element in `webFrame's` document selected by
`selector`, `null` would be returned if `selector` does not select a frame or
if the frame is not in the current renderer process.
### `webFrame.findFrameByName(name)`
* `name` String
Returns `WebFrame` - A child of `webFrame` with the supplied `name`, `null`
would be returned if there's no such frame or if the frame is not in the current
renderer process.
## Properties
### `webFrame.top`
A `WebFrame` representing top frame in frame hierarchy to which `webFrame`
belongs, the property would be `null` if top frame is not in the current
renderer process.
### `webFrame.opener`
A `WebFrame` representing the frame which opened `webFrame`, the property would
be `null` if there's no opener or opener is not in the current renderer process.
### `webFrame.parent`
A `WebFrame` representing parent frame of `webFrame`, the property would be
`null` if `webFrame` is top or parent is not in the current renderer process.
### `webFrame.firstChild`
A `WebFrame` representing the first child frame of `webFrame`, the property
would be `null` if `webFrame` has no children or if first child is not in the
current renderer process.
### `webFrame.nextSibling`
A `WebFrame` representing next sibling frame, the property would be `null` if
`webFrame` is the last frame its parent or if the next sibling is not in the
current renderer process.