1 // Copyright (c) 2019 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that can 3 // be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_COMMON_FRAME_UTIL_H_ 6 #define CEF_LIBCEF_COMMON_FRAME_UTIL_H_ 7 8 #include <stdint.h> 9 #include <string> 10 11 #include "base/logging.h" 12 #include "content/public/browser/global_routing_id.h" 13 #include "content/public/common/child_process_host.h" 14 15 namespace content { 16 class NavigationHandle; 17 } 18 19 namespace frame_util { 20 21 // Create a frame ID in the format exposed by the CEF API. MakeFrameId(int child_id,int frame_routing_id)22inline int64_t MakeFrameId(int child_id, int frame_routing_id) { 23 return (static_cast<uint64_t>(child_id) << 32) | 24 static_cast<uint64_t>(frame_routing_id); 25 } 26 27 // Create a frame ID in the format exposed by the CEF API. MakeFrameId(const content::GlobalRenderFrameHostId & global_id)28inline int64_t MakeFrameId(const content::GlobalRenderFrameHostId& global_id) { 29 return MakeFrameId(global_id.child_id, global_id.frame_routing_id); 30 } 31 32 // Returns true if |child_id| is valid. IsValidChildId(int child_id)33inline bool IsValidChildId(int child_id) { 34 // See comments in ChildProcessHostImpl::GenerateChildProcessUniqueId(). 35 return child_id != content::ChildProcessHost::kInvalidUniqueID && 36 child_id != 0; 37 } 38 39 // Returns true if |frame_routing_id| is valid. IsValidRoutingId(int frame_routing_id)40inline bool IsValidRoutingId(int frame_routing_id) { 41 return frame_routing_id != MSG_ROUTING_NONE; 42 } 43 44 // Returns true if |global_id| is valid. IsValidGlobalId(const content::GlobalRenderFrameHostId & global_id)45inline bool IsValidGlobalId(const content::GlobalRenderFrameHostId& global_id) { 46 return IsValidChildId(global_id.child_id) && 47 IsValidRoutingId(global_id.frame_routing_id); 48 } 49 50 // Create a global ID from components. 51 inline content::GlobalRenderFrameHostId MakeGlobalId( 52 int child_id, 53 int frame_routing_id, 54 bool allow_invalid_frame_id = false) { 55 DCHECK(IsValidChildId(child_id)); 56 DCHECK(allow_invalid_frame_id || IsValidRoutingId(frame_routing_id)); 57 return content::GlobalRenderFrameHostId(child_id, frame_routing_id); 58 } 59 60 // Create a global ID from a frame ID. MakeGlobalId(int64_t frame_id)61inline content::GlobalRenderFrameHostId MakeGlobalId(int64_t frame_id) { 62 uint32_t child_id = frame_id >> 32; 63 uint32_t frame_routing_id = std::numeric_limits<uint32_t>::max() & frame_id; 64 return MakeGlobalId(child_id, frame_routing_id); 65 } 66 67 // Returns an invalid global ID value. InvalidGlobalId()68inline content::GlobalRenderFrameHostId InvalidGlobalId() { 69 return content::GlobalRenderFrameHostId(); 70 } 71 72 // Returns the best match of global ID for |navigation_handle|. For pre-commit 73 // navigations this will return the current RFH, if any, or an invalid ID. 74 content::GlobalRenderFrameHostId GetGlobalId( 75 content::NavigationHandle* navigation_handle); 76 77 // Returns a human-readable version of the ID. 78 std::string GetFrameDebugString(int64_t frame_id); 79 std::string GetFrameDebugString( 80 const content::GlobalRenderFrameHostId& global_id); 81 82 } // namespace frame_util 83 84 #endif // CEF_LIBCEF_COMMON_FRAME_UTIL_H_ 85