在做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()