Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: content/common/unique_name_helper.h

Issue 2902253003: Refactor UniqueNameHelper to use an adapter pattern for code sharing. (Closed)
Patch Set: Fix off by one bug Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/common/BUILD.gn ('k') | content/common/unique_name_helper.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_RENDERER_UNIQUE_NAME_HELPER_H_ 5 #ifndef CONTENT_COMMON_UNIQUE_NAME_HELPER_H_
6 #define CONTENT_RENDERER_UNIQUE_NAME_HELPER_H_ 6 #define CONTENT_COMMON_UNIQUE_NAME_HELPER_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector>
9 10
10 #include "base/macros.h" 11 #include "base/macros.h"
11 12 #include "base/strings/string_piece.h"
12 namespace blink {
13 class WebFrame;
14 class WebLocalFrame;
15 } // namespace blink
16 13
17 namespace content { 14 namespace content {
18 15
19 class RenderFrameImpl;
20
21 // Frame helper that manages the details of generating a quasi-stable unique 16 // Frame helper that manages the details of generating a quasi-stable unique
22 // name for the frame. The name is unique within a page, and is used for: 17 // name for the frame. The name is unique within a page, and is used for:
23 // - matching up session history items with recreated frames 18 // - matching up session history items with recreated frames
24 // - layout test results 19 // - layout test results
25 // 20 //
26 // Description of the current unique name format 21 // Description of the current unique name format
27 // --------------------------------------------- 22 // ---------------------------------------------
28 // Changing the format of unique name has a high cost, because it breaks 23 // Changing the format of unique name has a high cost, because it breaks
29 // backwards compatibility of session history (which stores unique names 24 // backwards compatibility of session history (which stores unique names
30 // generated in the past on user's disk). The evolution of unique name in a 25 // generated in the past on user's disk). The evolution of unique name in a
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 // | #1 | #2 | #3 | 59 // | #1 | #2 | #3 |
65 // 60 //
66 // newUniqueSuffix ::= "<!--framePosition" framePosition "/" retryNumber "-->" 61 // newUniqueSuffix ::= "<!--framePosition" framePosition "/" retryNumber "-->"
67 // 62 //
68 // framePosition ::= "-" numberOfSiblingsBeforeChild 63 // framePosition ::= "-" numberOfSiblingsBeforeChild
69 // [ framePosition-forParent? ] 64 // [ framePosition-forParent? ]
70 // 65 //
71 // retryNumber ::= smallest non-negative integer resulting in unique name 66 // retryNumber ::= smallest non-negative integer resulting in unique name
72 class UniqueNameHelper { 67 class UniqueNameHelper {
73 public: 68 public:
74 explicit UniqueNameHelper(RenderFrameImpl* render_frame); 69 // Adapter class so UniqueNameHelper can be used with both RenderFrameImpl and
70 // ExplodedFrameState.
71 class FrameAdapter {
72 public:
73 FrameAdapter() {}
74 virtual ~FrameAdapter();
75
76 virtual bool IsMainFrame() const = 0;
77 virtual bool IsCandidateUnique(const std::string& name) const = 0;
78 // Returns the number of sibling frames of this frame. Note this should not
79 // include this frame in the count.
80 virtual int GetSiblingCount() const = 0;
81 virtual int GetChildCount() const = 0;
82 // Sets the reference point for iterations that walk up the frame tree.
83 enum class BeginPoint {
84 // This should be the default: it indicates the logical iteration
85 // operation began on this frame and the walking logic should retrieve the
86 // parent frame as normal.
87 kParentFrame,
88 // For implementing the pending child frame adapter, which delegates to
89 // its future parent's FrameAdapter. Walking up the tree should not skip
90 // this frame; instead it should treat this frame as the parent, since the
91 // logical iteration began with a pending child frame.
92 kThisFrame,
93 };
94 // Returns a vector of the strings representing the name of each frame in
95 // the chain from this frame to the root frame. |begin_point| indicates the
96 // reference point for starting the collection. |should_stop| is a
97 // boolean predicate that indicates when to stop collection of names.
98 virtual std::vector<base::StringPiece> CollectAncestorNames(
99 BeginPoint begin_point,
100 bool (*should_stop)(base::StringPiece)) const = 0;
101 // Returns a vector of ints representing the child index of each frame in
102 // the chain from this frame to the root.
103 virtual std::vector<int> GetFramePosition(BeginPoint begin_point) const = 0;
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(FrameAdapter);
107 };
108
109 explicit UniqueNameHelper(FrameAdapter* frame);
75 ~UniqueNameHelper(); 110 ~UniqueNameHelper();
76 111
77 // Returns the generated unique name. 112 // Returns the generated unique name.
78 const std::string& value() const { return unique_name_; } 113 const std::string& value() const { return unique_name_; }
79 114
80 // Used to propagate an already calculated unique name. 115 // Used to propagate an already calculated unique name.
81 // 116 //
82 // TODO(lukasza): It would be nice to assert uniqueness of the propagated 117 // TODO(lukasza): It would be nice to assert uniqueness of the propagated
83 // name here but: 118 // name here but:
84 // 1) uniqueness is currently violated by provisional/old frame pairs. 119 // 1) uniqueness is currently violated by provisional/old frame pairs.
85 // 2) there is an unresolved race between 2 OOPIFs, that can result in a 120 // 2) there is an unresolved race between 2 OOPIFs, that can result in a
86 // non-unique unique name: see https://crbug.com/558680#c14. 121 // non-unique unique name: see https://crbug.com/558680#c14.
87 void set_propagated_name(const std::string& name) { unique_name_ = name; } 122 void set_propagated_name(const std::string& name) { unique_name_ = name; }
88 123
89 // Note: when creating a new child frame, the unique name needs to be 124 // Note: when creating a new child frame, the unique name needs to be
90 // calculated before the RenderFrameImpl is created. To avoid this chicken and 125 // calculated before the child frame is created. To avoid this chicken and
91 // egg problem, this method is static, which means that |parent| needs to be 126 // egg problem, this method is designed to be called on the *parent* frame of
92 // passed as a parameter. 127 // the future new child frame and return the value the new child frame should
93 static std::string GenerateNameForNewChildFrame(blink::WebFrame* parent, 128 // use.
94 const std::string& name); 129 std::string GenerateNameForNewChildFrame(const std::string& name) const;
95 130
96 // Called after a browsing context name change to generate a new name. Note 131 // Called after a browsing context name change to generate a new name. Note
97 // that this should not be called if the frame is no longer displaying the 132 // that this should not be called if the frame is no longer displaying the
98 // initial empty document, as unique name changes after that point will break 133 // initial empty document, as unique name changes after that point will break
99 // history navigations. See https://crbug.com/607205. 134 // history navigations. See https://crbug.com/607205.
100 void UpdateName(const std::string& name); 135 void UpdateName(const std::string& name);
101 136
102 private: 137 private:
103 blink::WebLocalFrame* GetWebFrame() const; 138 FrameAdapter* const frame_;
104
105 RenderFrameImpl* const render_frame_;
106 std::string unique_name_; 139 std::string unique_name_;
107 140
108 DISALLOW_COPY_AND_ASSIGN(UniqueNameHelper); 141 DISALLOW_COPY_AND_ASSIGN(UniqueNameHelper);
109 }; 142 };
110 143
111 } // namespace content 144 } // namespace content
112 145
113 #endif // CONTENT_RENDERER_UNIQUE_NAME_HELPER_H_ 146 #endif // CONTENT_COMMON_UNIQUE_NAME_HELPER_H_
OLDNEW
« no previous file with comments | « content/common/BUILD.gn ('k') | content/common/unique_name_helper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698