ui_dump_dom_tree.cpp 14.5 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dfx/ui_dump_dom_tree.h"

#if ENABLE_DEBUG
#include "components/root_view.h"
P
pssea 已提交
20 21
#include "components/ui_abstract_clock.h"
#include "components/ui_abstract_progress.h"
M
mamingshuai 已提交
22 23
#include "components/ui_checkbox.h"
#include "components/ui_image_view.h"
P
pssea 已提交
24 25
#include "components/ui_label.h"
#include "components/ui_label_button.h"
M
mamingshuai 已提交
26 27
#include "components/ui_list.h"
#include "components/ui_picker.h"
P
pssea 已提交
28
#include "components/ui_scroll_view.h"
M
mamingshuai 已提交
29 30
#include "components/ui_swipe_view.h"
#include "components/ui_time_picker.h"
P
pssea 已提交
31 32
#include "components/ui_toggle_button.h"
#include "components/ui_view.h"
M
mamingshuai 已提交
33
#include "draw/draw_image.h"
Z
zhangguyuan 已提交
34 35
#include "gfx_utils/file.h"
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
36 37 38
#endif // ENABLE_DEBUG
namespace OHOS {
#if ENABLE_DEBUG
Z
zhdengc 已提交
39 40 41 42 43 44
UIDumpDomTree* UIDumpDomTree::GetInstance()
{
    static UIDumpDomTree instance;
    return &instance;
}

M
mamingshuai 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
void UIDumpDomTree::AddNameField(UIViewType type, cJSON* usr) const
{
    if (usr == nullptr) {
        return;
    }

    if (type < UI_NUMBER_MAX) {
        cJSON_AddStringToObject(usr, "name", VIEW_TYPE_STRING[type]);
    } else {
        cJSON_AddStringToObject(usr, "name", "UnknownType");
    }
}

void UIDumpDomTree::AddImageViewSpecialField(const UIView* view, cJSON* usr) const
{
    if ((view == nullptr) || (usr == nullptr)) {
        return;
    }
    const UIImageView* tmpImageView = static_cast<const UIImageView*>(view);
    ImageSrcType srcType = tmpImageView->GetSrcType();
    if (srcType == IMG_SRC_FILE) {
        cJSON_AddStringToObject(usr, "src", reinterpret_cast<const char*>(tmpImageView->GetPath()));
    } else if (srcType == IMG_SRC_VARIABLE) {
        const ImageInfo* imageInfo = reinterpret_cast<const ImageInfo*>(tmpImageView->GetImageInfo());
        if ((imageInfo == nullptr) || (imageInfo->userData == nullptr)) {
            cJSON_AddStringToObject(usr, "src", "");
            return;
        }
        uintptr_t userData = reinterpret_cast<uintptr_t>(imageInfo->userData);
        cJSON_AddNumberToObject(usr, "src", static_cast<uint32_t>(userData));
    } else {
        cJSON_AddStringToObject(usr, "src", "");
    }
}

P
pssea 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
void UIDumpDomTree::AddLabelField(const UIView* view, cJSON* usr) const
{
    const UILabel* tmpLabel = static_cast<const UILabel*>(view);
    cJSON_AddStringToObject(usr, "text", tmpLabel->GetText());
    tmpLabel = nullptr;
}

void UIDumpDomTree::AddLabelButtonField(const UIView* view, cJSON* usr) const
{
    const UILabelButton* tmpLabelButton = static_cast<const UILabelButton*>(view);
    cJSON_AddStringToObject(usr, "text", tmpLabelButton->GetText());
    tmpLabelButton = nullptr;
}

void UIDumpDomTree::AddCheckboxField(const UIView* view, cJSON* usr) const
{
    const UICheckBox* tmpCheckBox = static_cast<const UICheckBox*>(view);
    if (tmpCheckBox->GetState()) {
        cJSON_AddStringToObject(usr, "state", "UNSELECTED");
    } else {
        cJSON_AddStringToObject(usr, "state", "SELECTED");
    }
    tmpCheckBox = nullptr;
}

void UIDumpDomTree::AddToggleButtonField(const UIView* view, cJSON* usr) const
{
    const UIToggleButton* tmpToggleButton = static_cast<const UIToggleButton*>(view);
    cJSON_AddBoolToObject(usr, "state", tmpToggleButton->GetState());
    tmpToggleButton = nullptr;
}

void UIDumpDomTree::AddProgressField(const UIView* view, cJSON* usr) const
{
    const UIAbstractProgress* tmpAbstractProgress = static_cast<const UIAbstractProgress*>(view);
    cJSON_AddNumberToObject(usr, "currValue", static_cast<double>(tmpAbstractProgress->GetValue()));
    cJSON_AddNumberToObject(usr, "rangeMin", static_cast<double>(tmpAbstractProgress->GetRangeMin()));
    cJSON_AddNumberToObject(usr, "rangeMax", static_cast<double>(tmpAbstractProgress->GetRangeMax()));
    tmpAbstractProgress = nullptr;
}

void UIDumpDomTree::AddScrollViewField(const UIView* view, cJSON* usr) const
{
    const UIScrollView* tmpScrollView = static_cast<const UIScrollView*>(view);
    cJSON_AddBoolToObject(usr, "xScrollable", tmpScrollView->GetHorizontalScrollState());
    cJSON_AddBoolToObject(usr, "yScrollable", tmpScrollView->GetVerticalScrollState());
    tmpScrollView = nullptr;
}

void UIDumpDomTree::AddListField(const UIView* view, cJSON* usr) const
{
    UIList* tmpList = static_cast<UIList*>(const_cast<UIView*>(view));
    cJSON_AddBoolToObject(usr, "isLoopList", tmpList->GetLoopState());
    UIView* selectView = tmpList->GetSelectView();
    if (selectView != nullptr) {
        cJSON_AddNumberToObject(usr, "selectedIndex", static_cast<double>(selectView->GetViewIndex()));
        selectView = nullptr;
    }
    tmpList = nullptr;
}

void UIDumpDomTree::AddClockField(const UIView* view, cJSON* usr) const
{
    const UIAbstractClock* tmpAbstractClock = static_cast<const UIAbstractClock*>(view);
    cJSON_AddNumberToObject(usr, "currentHour", static_cast<double>(tmpAbstractClock->GetCurrentHour()));
    cJSON_AddNumberToObject(usr, "currentMinute", static_cast<double>(tmpAbstractClock->GetCurrentMinute()));
    cJSON_AddNumberToObject(usr, "currentSecond", static_cast<double>(tmpAbstractClock->GetCurrentSecond()));
    tmpAbstractClock = nullptr;
}

void UIDumpDomTree::AddPickerField(const UIView* view, cJSON* usr) const
{
    const UIPicker* tmpPicker = static_cast<const UIPicker*>(view);
    cJSON_AddNumberToObject(usr, "selectedIndex", static_cast<double>(tmpPicker->GetSelected()));
    tmpPicker = nullptr;
}

void UIDumpDomTree::AddSwipeViewField(const UIView* view, cJSON* usr) const
{
    const UISwipeView* tmpSwipeView = static_cast<const UISwipeView*>(view);
    cJSON_AddNumberToObject(usr, "currentIndex", static_cast<double>(tmpSwipeView->GetCurrentPage()));
    cJSON_AddNumberToObject(usr, "direction", static_cast<double>(tmpSwipeView->GetDirection()));
    tmpSwipeView = nullptr;
}

void UIDumpDomTree::AddTimePickerField(const UIView* view, cJSON* usr) const
{
    const UITimePicker* tmpTimePicker = static_cast<const UITimePicker*>(view);
    cJSON_AddStringToObject(usr, "selectedHour", tmpTimePicker->GetSelectHour());
    cJSON_AddStringToObject(usr, "selectedMinute", tmpTimePicker->GetSelectMinute());
    cJSON_AddStringToObject(usr, "selectedSecond", tmpTimePicker->GetSelectSecond());
    tmpTimePicker = nullptr;
}

M
mamingshuai 已提交
174 175 176 177 178 179 180
void UIDumpDomTree::AddSpecialField(const UIView* view, cJSON* usr) const
{
    if ((view == nullptr) || (usr == nullptr)) {
        return;
    }
    switch (view->GetViewType()) {
        case UI_LABEL:
P
pssea 已提交
181 182
        case UI_ARC_LABEL:
            AddLabelField(view, usr);
M
mamingshuai 已提交
183
            break;
P
pssea 已提交
184 185 186

        case UI_LABEL_BUTTON:
            AddLabelButtonField(view, usr);
M
mamingshuai 已提交
187
            break;
P
pssea 已提交
188

M
mamingshuai 已提交
189
        case UI_CHECK_BOX:
P
pssea 已提交
190 191
        case UI_RADIO_BUTTON:
            AddCheckboxField(view, usr);
M
mamingshuai 已提交
192
            break;
P
pssea 已提交
193 194 195

        case UI_TOGGLE_BUTTON:
            AddToggleButtonField(view, usr);
M
mamingshuai 已提交
196
            break;
P
pssea 已提交
197
        case UI_IMAGE_VIEW:
M
mamingshuai 已提交
198 199
            AddImageViewSpecialField(view, usr);
            break;
P
pssea 已提交
200

M
mamingshuai 已提交
201 202 203
        // case below are all progress, thus has same attr.
        case UI_BOX_PROGRESS:
        case UI_SLIDER:
P
pssea 已提交
204 205
        case UI_CIRCLE_PROGRESS:
            AddProgressField(view, usr);
M
mamingshuai 已提交
206
            break;
P
pssea 已提交
207 208 209

        case UI_SCROLL_VIEW:
            AddScrollViewField(view, usr);
M
mamingshuai 已提交
210
            break;
P
pssea 已提交
211 212 213

        case UI_LIST:
            AddListField(view, usr);
M
mamingshuai 已提交
214
            break;
P
pssea 已提交
215

M
mamingshuai 已提交
216
        case UI_DIGITAL_CLOCK:
P
pssea 已提交
217 218
        case UI_ANALOG_CLOCK:
            AddClockField(view, usr);
M
mamingshuai 已提交
219
            break;
P
pssea 已提交
220 221 222

        case UI_PICKER:
            AddPickerField(view, usr);
M
mamingshuai 已提交
223
            break;
P
pssea 已提交
224 225 226

        case UI_SWIPE_VIEW:
            AddSwipeViewField(view, usr);
M
mamingshuai 已提交
227
            break;
P
pssea 已提交
228 229 230

        case UI_TIME_PICKER:
            AddTimePickerField(view, usr);
M
mamingshuai 已提交
231
            break;
P
pssea 已提交
232

M
mamingshuai 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        default:
            break;
    }
}

void UIDumpDomTree::AddCommonField(UIView* view, cJSON* usr) const
{
    if ((view == nullptr) || (usr == nullptr)) {
        return;
    }
    cJSON_AddNumberToObject(usr, "x", static_cast<double>(view->GetOrigRect().GetX()));
    cJSON_AddNumberToObject(usr, "y", static_cast<double>(view->GetOrigRect().GetY()));
    cJSON_AddNumberToObject(usr, "width", static_cast<double>(view->GetWidth()));
    cJSON_AddNumberToObject(usr, "height", static_cast<double>(view->GetHeight()));
    cJSON_AddStringToObject(usr, "id", view->GetViewId());
    cJSON_AddBoolToObject(usr, "visible", view->IsVisible());
    cJSON_AddBoolToObject(usr, "touchable", view->IsTouchable());
    cJSON_AddBoolToObject(usr, "draggable", view->IsDraggable());
    cJSON_AddBoolToObject(usr, "onClickListener", (view->GetOnClickListener() != nullptr));
    cJSON_AddBoolToObject(usr, "onDragListener", (view->GetOnDragListener() != nullptr));
    cJSON_AddBoolToObject(usr, "onLongPressListener", (view->GetOnLongPressListener() != nullptr));
}

void UIDumpDomTree::OutputDomNode(UIView* view)
{
    if (view == nullptr) {
        return;
    }
    /* Output current view's info into cJSON structure. */
    cJSON* dumpUsr = cJSON_CreateObject();
    if (dumpUsr == nullptr) {
264
        GRAPHIC_LOGE("UIDumpDomTree::OutputDomNode cJSON create object failed Err!\n");
M
mamingshuai 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        return;
    }

    AddNameField(view->GetViewType(), dumpUsr);
    AddCommonField(view, dumpUsr);
    AddSpecialField(view, dumpUsr);

    pJson_ = cJSON_Print(dumpUsr);
    cJSON_Delete(dumpUsr);
}

bool UIDumpDomTree::WriteDumpFile() const
{
    unlink(path_);
    int32_t fd = open(path_, O_CREAT | O_RDWR, DEFAULT_FILE_PERMISSION);
    if (fd < 0) {
281
        GRAPHIC_LOGE("UIDumpDomTree::WriteDumpFile open file failed Err!\n");
M
mamingshuai 已提交
282 283 284 285 286 287 288 289 290 291
        return false;
    }

    if (pJson_ == nullptr) {
        close(fd);
        return false;
    }

    uint32_t length = strlen(pJson_);
    if (static_cast<uint32_t>(write(fd, pJson_, length)) != length) {
292
        GRAPHIC_LOGE("UIDumpDomTree::WriteDumpFile write file failed Err!\n");
M
mamingshuai 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        close(fd);
        return false;
    }

    if (close(fd) < 0) {
        return false;
    }
    return true;
}

void UIDumpDomTree::OutputDomTree(UIView* view, cJSON* usr)
{
    if (allocErrorFlag_) {
        return;
    }

    cJSON* dumpUsr = usr;
    if (dumpUsr != rootJson_) {
        dumpUsr = cJSON_CreateObject();
        if (dumpUsr == nullptr) {
            allocErrorFlag_ = true;
314
            GRAPHIC_LOGE("UIDumpDomTree::OutputDomTree cJSON create object failed Err!\n");
M
mamingshuai 已提交
315 316
            return;
        }
L
liuyuxiang-bear 已提交
317
        /* usr must be an array. */
M
mamingshuai 已提交
318 319 320 321 322 323 324 325 326 327 328
        cJSON_AddItemToArray(usr, dumpUsr);
    }

    AddNameField(view->GetViewType(), dumpUsr);
    AddCommonField(view, dumpUsr);
    AddSpecialField(view, dumpUsr);

    if (view->IsViewGroup()) {
        cJSON* arrayJson = cJSON_CreateArray();
        if (arrayJson == nullptr) {
            allocErrorFlag_ = true;
329
            GRAPHIC_LOGE("UIDumpDomTree::OutputDomTree cJSON create object failed Err!\n");
M
mamingshuai 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
            return;
        }

        cJSON_AddItemToObject(dumpUsr, "child", arrayJson);
        UIViewGroup* tmpViewGroup = static_cast<UIViewGroup*>(view);
        UIView* childView = tmpViewGroup->GetChildrenHead();
        while (childView != nullptr) {
            OutputDomTree(childView, arrayJson);
            childView = childView->GetNextSibling();
        }
    }
}

void UIDumpDomTree::DumpJsonById(UIView* view, const char* id, DumpMode mode)
{
    if (searchFlag_) {
        return;
    }

    /* Check whether current view is the view we are looking for. */
    if ((view->GetViewId() != nullptr) && !strcmp(view->GetViewId(), id)) {
        if (mode == DUMP_NODE) {
            /* Find the view with right id, output its info. */
            OutputDomNode(view);
        } else if (mode == DUMP_TREE) {
            /* Find the view with right id, output its dom tree's info. */
            OutputDomTree(view, rootJson_);
        }
        /* Set flag to stop the tranversion. */
        searchFlag_ = true;
    } else {
        /* Look through all childrens of the current viewGroup. */
        if (view->IsViewGroup()) {
            UIViewGroup* tmpViewGroup = static_cast<UIViewGroup*>(view);
            UIView* childView = tmpViewGroup->GetChildrenHead();
            while (childView != nullptr) {
                DumpJsonById(childView, id, mode);
                childView = childView->GetNextSibling();
            }
        }
    }
}
#endif // ENABLE_DEBUG

char* UIDumpDomTree::DumpDomNode(const char* id)
{
#if ENABLE_DEBUG
    if (id == nullptr) {
        return nullptr;
    }
    /* Reset dump info */
    pJson_ = nullptr;

    RootView* rootView = RootView::GetInstance();
    UIView* currView = static_cast<UIView*>(rootView);
    /* Search through all views from rootView. */
    DumpJsonById(currView, id, DUMP_NODE);

    /* Reset the search flag and pJson for next search */
    searchFlag_ = false;
    return pJson_;
#else
    return nullptr;
#endif // ENABLE_DEBUG
}

bool UIDumpDomTree::DumpDomTree(const char* id, const char* path)
{
#if ENABLE_DEBUG
    path_ = (path == nullptr) ? DEFAULT_DUMP_DOM_TREE_PATH : path;

    RootView* rootView = RootView::GetInstance();
    UIView* currView = static_cast<UIView*>(rootView);
    rootJson_ = cJSON_CreateObject();
    if (rootJson_ == nullptr) {
405
        GRAPHIC_LOGE("UIDumpDomTree::DumpDomTree cJSON create object failed Err!\n");
M
mamingshuai 已提交
406 407 408 409 410 411 412 413 414 415
        return false;
    }

    if (id == nullptr) {
        OutputDomTree(currView, rootJson_);
    } else {
        DumpJsonById(currView, id, DUMP_TREE);
        if (!searchFlag_) {
            cJSON_Delete(rootJson_);
            rootJson_ = nullptr;
416
            GRAPHIC_LOGI("UIDumpDomTree::DumpDomTree can not find the node \n");
M
mamingshuai 已提交
417 418 419 420 421 422 423 424 425 426
            return false;
        }
        /* Reset the search flag. */
        searchFlag_ = false;
    }

    pJson_ = cJSON_Print(rootJson_);
    cJSON_Delete(rootJson_);
    rootJson_ = nullptr;
    if (pJson_ == nullptr) {
427
        GRAPHIC_LOGE("UIDumpDomTree::DumpDomTree convert cJSON object to string failed Err!\n");
M
mamingshuai 已提交
428 429 430 431 432 433
        return false;
    }

    if (!WriteDumpFile()) {
        cJSON_free(pJson_);
        pJson_ = nullptr;
434
        GRAPHIC_LOGE("UIDumpDomTree::DumpDomTree file operation failed Err!\n");
M
mamingshuai 已提交
435 436 437 438 439 440 441 442 443 444 445
        return false;
    }

    cJSON_free(pJson_);
    pJson_ = nullptr;
    allocErrorFlag_ = false;
    return true;
#else
    return false;
#endif // ENABLE_DEBUG
}
P
pssea 已提交
446
} // namespace OHOS