1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/renderer/context_menu_params_builder.h"
6
7#include "base/logging.h"
8#include "content/common/ssl_status_serialization.h"
9#include "content/public/common/context_menu_params.h"
10#include "content/renderer/history_serialization.h"
11#include "content/renderer/menu_item_builder.h"
12#include "third_party/WebKit/public/web/WebElement.h"
13#include "third_party/WebKit/public/web/WebNode.h"
14
15namespace content {
16
17// static
18ContextMenuParams ContextMenuParamsBuilder::Build(
19    const blink::WebContextMenuData& data) {
20  ContextMenuParams params;
21  params.media_type = data.mediaType;
22  params.x = data.mousePosition.x;
23  params.y = data.mousePosition.y;
24  params.link_url = data.linkURL;
25  params.unfiltered_link_url = data.linkURL;
26  params.src_url = data.srcURL;
27  params.has_image_contents = data.hasImageContents;
28  params.page_url = data.pageURL;
29  params.keyword_url = data.keywordURL;
30  params.frame_url = data.frameURL;
31  params.media_flags = data.mediaFlags;
32  params.selection_text = data.selectedText;
33  params.misspelled_word = data.misspelledWord;
34  params.misspelling_hash = data.misspellingHash;
35  params.spellcheck_enabled = data.isSpellCheckingEnabled;
36  params.is_editable = data.isEditable;
37  params.writing_direction_default = data.writingDirectionDefault;
38  params.writing_direction_left_to_right = data.writingDirectionLeftToRight;
39  params.writing_direction_right_to_left = data.writingDirectionRightToLeft;
40  params.edit_flags = data.editFlags;
41  params.frame_charset = data.frameEncoding.utf8();
42  params.referrer_policy = data.referrerPolicy;
43
44  for (size_t i = 0; i < data.dictionarySuggestions.size(); ++i)
45    params.dictionary_suggestions.push_back(data.dictionarySuggestions[i]);
46
47  params.custom_context.is_pepper_menu = false;
48  for (size_t i = 0; i < data.customItems.size(); ++i)
49    params.custom_items.push_back(MenuItemBuilder::Build(data.customItems[i]));
50
51  if (!data.frameHistoryItem.isNull()) {
52    params.frame_page_state =
53        SingleHistoryItemToPageState(data.frameHistoryItem);
54  }
55
56  if (!params.link_url.is_empty()) {
57    blink::WebNode selectedNode = data.node;
58
59    // If there are other embedded tags (like <a ..>Some <b>text</b></a>)
60    // we need to extract the parent <a/> node.
61    while (!selectedNode.isLink() && !selectedNode.parentNode().isNull()) {
62      selectedNode = selectedNode.parentNode();
63    }
64
65    blink::WebElement selectedElement = selectedNode.to<blink::WebElement>();
66    if (selectedNode.isLink() && !selectedElement.isNull()) {
67      params.link_text = selectedElement.innerText();
68    } else {
69      LOG(ERROR) << "Creating a ContextMenuParams for a node that has a link"
70                 << "url but is not an ElementNode or does not have an"
71                 << "ancestor that is a link.";
72    }
73  }
74
75  // Deserialize the SSL info.
76  if (!data.securityInfo.isEmpty()) {
77    DeserializeSecurityInfo(data.securityInfo,
78        &params.security_info.cert_id, &params.security_info.cert_status,
79        &params.security_info.security_bits,
80        &params.security_info.connection_status,
81        &params.security_info.signed_certificate_timestamp_ids);
82  }
83
84  return params;
85}
86
87}  // namespace content
88