如何在android上打印dom树,render树,display_tree

本文详细介绍了如何在Android浏览器中通过框架接口实现打印DOM树、渲染树和显示树的功能,包括在browser.xml文件中增加打印选项,修改Controller.java文件来触发树的打印,并在WebViewClassic.java中调整dump方法以支持不同类型的树打印。同时,还对Android框架中的日志记录进行了相应修改,以便于在不同的位置保存打印出的树信息。
摘要由CSDN通过智能技术生成

  在做chromium的时候,我们在debug的时候经常使用dumprendertree去打印网页对应的dom树等内容。

   但是在刚接触android的时候,一直非常烦恼怎么用android自带的browser去打印出来这些内容。

   幸好的是,framework里面提供了一些接口,让我们可以实现这个功能。

   于是,写了个新的feature,在android的browser里面,按下menu键,就会增加三个新的选项。点击响应的选项,就会打印出来相对应的tree.

   废话不多说,直接贴code。

 

   在browser的app中做下列修改:

 

diff --git a/res/menu/browser.xml b/res/menu/browser.xml
index 49fc4a4..08a240f 100644
--- a/res/menu/browser.xml
+++ b/res/menu/browser.xml
@@ -80,6 +80,18 @@
<item
android:id="@+id/save_snapshot_menu_id"
android:title="@string/menu_save_snapshot" />
+ <!--Add print_dom_tree by chao S -->
+ <item
+ android:id="@+id/print_dom_tree"
+ android:title="@string/dom_tree_title" />
+ <item
+ android:id="@+id/print_render_tree"
+ android:title="@string/render_tree_title" />
+ <item
+ android:id="@+id/print_display_tree"
+ android:title="@string/display_tree_title" />
+
+ <!--Add print_dom_tree by chao E -->
</group>
<group
android:id="@+id/SNAPSHOT_MENU">
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 870d0c7..c5dbf50 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -908,7 +908,11 @@
<!-- The description of the suggestion that lets the user search the web for the query currently
in the text field. -->
<string name="search_the_web">Search the web</string>
-
+ <!-- Add print_dom_tree by chao S -->
+ <string name="dom_tree_title">Print Dom tree</string>
+ <string name="render_tree_title">Print render tree</string>
+ <string name="display_tree_title">Print display tree</string>
+ <!-- Add print_dom_tree by chao E -->
<!-- HTML5 dialogs -->
<!-- Used as a status bar notification when the browser is running out of space trying to allocate or increase the database quota for an HTML5 databae or application cache. -->
<string name="webstorage_outofspace_notification_title">Browser storage full</string>
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 358e746..ac3d813 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -1967,6 +1967,23 @@ public class Controller extends FjController
if (source == null) break;
new SaveSnapshotTask(source).execute();
break;
+ // -- Add print_dom_tree by chao 
+ case R.id.print_dom_tree:
+ final Tab dom = getTabControl().getCurrentTab();
+ dom.getWebViewClassic().dumpDomTree(true);
+ break;
+
+ case R.id.print_render_tree:
+ final Tab render = getTabControl().getCurrentTab();
+ render.getWebViewClassic().dumpRenderTree(true);
+ break;
+
+ case R.id.print_display_tree:
+ final Tab display = getTabControl().getCurrentTab();
+ display.getWebViewClassic().dumpDisplayTree();
+ break;
+
+ // Add print_dom_tree by chao --

case R.id.page_info_menu_id:
showPageInfo();

 

 

 

 

 

在framework中做下列修改:

 

diff --git a/core/java/android/webkit/WebViewClassic.java b/core/java/android/webkit/WebViewClassic.java
index 4bfce04..d6c7cf3 100755
--- a/core/java/android/webkit/WebViewClassic.java
+++ b/core/java/android/webkit/WebViewClassic.java
@@ -5339,7 +5339,10 @@ public final class WebViewClassic extends FjWebViewClassic
* debug only
*/
public void dumpDomTree(boolean toFile) {
- mWebViewCore.sendMessage(EventHub.DUMP_DOMTREE, toFile ? 1 : 0, 0);
+ // -- Add print_dom_tree by chao 
+ //mWebViewCore.sendMessage(EventHub.DUMP_DOMTREE, toFile ? 1 : 0, 0);
+ mWebViewCore.sendMessage(EventHub.DUMP_DOMTREE, toFile ? 1 : 1, 0);
+ // Add print_dom_tree by chao --
}

/**
@@ -5349,7 +5352,10 @@ public final class WebViewClassic extends FjWebViewClassic
* debug only
*/
public void dumpRenderTree(boolean toFile) {
- mWebViewCore.sendMessage(EventHub.DUMP_RENDERTREE, toFile ? 1 : 0, 0);
+ // -- Add print_dom_tree by chao
+ // mWebViewCore.sendMessage(EventHub.DUMP_RENDERTREE, toFile ? 1 : 0, 0);
+ mWebViewCore.sendMessage(EventHub.DUMP_RENDERTREE, toFile ? 1 : 1, 0);
+ // Add print_dom_tree by chao --
}

/**


extern/webkit中的修改如下:

 

 

diff --git a/Source/WebKit/android/AndroidLog.h b/Source/WebKit/android/AndroidLog.h
index f034d35..2efab96 100644
--- a/Source/WebKit/android/AndroidLog.h
+++ b/Source/WebKit/android/AndroidLog.h
@@ -37,12 +37,18 @@
#ifdef ANDROID_DOM_LOGGING
#include <stdio.h>
extern FILE* gDomTreeFile;
-#define DOM_TREE_LOG_FILE "/sdcard/domTree.txt"
+// -- Add print_dom_tree by chao 
+// #define DOM_TREE_LOG_FILE "/sdcard/domTree.txt"
+#define DOM_TREE_LOG_FILE "/data/local/domTree.txt"
+// Add print_dom_tree by chao --
#define DUMP_DOM_LOGD(...) { if (gDomTreeFile) \
fprintf(gDomTreeFile, __VA_ARGS__); else ALOGD(__VA_ARGS__); }

extern FILE* gRenderTreeFile;
-#define RENDER_TREE_LOG_FILE "/sdcard/renderTree.txt"
+// -- Add print_dom_tree by chao
+// #define RENDER_TREE_LOG_FILE "/sdcard/renderTree.txt"
+#define RENDER_TREE_LOG_FILE "/data/local/renderTree.txt"
+// Add print_dom_tree by chao --
#define DUMP_RENDER_LOGD(...) { if (gRenderTreeFile) \
fprintf(gRenderTreeFile, __VA_ARGS__); else ALOGD(__VA_ARGS__); }
#else
@@ -50,8 +56,12 @@ extern FILE* gRenderTreeFile;
#define DUMP_RENDER_LOGD(...) ((void)0)
#endif /* ANDROID_DOM_LOGGING */

-#define DISPLAY_TREE_LOG_FILE "/sdcard/displayTree.txt"
-#define LAYERS_TREE_LOG_FILE "/sdcard/layersTree.plist"
+// -- Add print_dom_tree by chao
+//#define DISPLAY_TREE_LOG_FILE "/sdcard/displayTree.txt"
+//#define LAYERS_TREE_LOG_FILE "/sdcard/layersTree.plist"
+#define DISPLAY_TREE_LOG_FILE "/data/local/displayTree.txt"
+#define LAYERS_TREE_LOG_FILE "/data/local/layersTree.plist"
+// Add print_dom_tree by chao --

#define FLOAT_RECT_FORMAT "[x=%.2f,y=%.2f,w=%.2f,h=%.2f]"
#define FLOAT_RECT_ARGS(fr) fr.x(), fr.y(), fr.width(), fr.height()

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值