1 // Copyright 2014 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 #ifndef CONTENT_BROWSER_TRANSITION_REQUEST_MANAGER_H_ 6 #define CONTENT_BROWSER_TRANSITION_REQUEST_MANAGER_H_ 7 8 #include <map> 9 #include <string> 10 #include <utility> 11 #include <vector> 12 13 #include "base/basictypes.h" 14 #include "base/memory/ref_counted.h" 15 #include "content/common/content_export.h" 16 #include "url/gurl.h" 17 18 template <typename T> 19 struct DefaultSingletonTraits; 20 21 namespace net { 22 class HttpResponseHeaders; 23 } 24 class GURL; 25 26 namespace content { 27 28 // This struct passes data about an imminent transition between threads. 29 struct TransitionLayerData { 30 TransitionLayerData(); 31 ~TransitionLayerData(); 32 33 std::string markup; 34 std::string css_selector; 35 scoped_refptr<net::HttpResponseHeaders> response_headers; 36 GURL request_url; 37 }; 38 39 // TransitionRequestManager is used to handle bookkeeping for transition 40 // requests and responses. 41 // 42 // TransitionRequestManager is a singleton and should only be accessed on the IO 43 // thread. 44 // 45 class TransitionRequestManager { 46 public: 47 // Returns the singleton instance. 48 CONTENT_EXPORT static TransitionRequestManager* GetInstance(); 49 50 // Parses out any transition-entering-stylesheet link headers from the 51 // response headers. 52 CONTENT_EXPORT static void ParseTransitionStylesheetsFromHeaders( 53 const scoped_refptr<net::HttpResponseHeaders>& headers, 54 std::vector<GURL>& entering_stylesheets, 55 const GURL& resolve_address); 56 57 // Returns whether the RenderFrameHost specified by the given IDs currently 58 // has any pending transition request data. If so, we will have to delay the 59 // response until the embedder resumes the request. 60 bool HasPendingTransitionRequest(int render_process_id, 61 int render_frame_id, 62 const GURL& request_url, 63 TransitionLayerData* transition_data); 64 65 // Adds pending request data for a transition navigation for the 66 // RenderFrameHost specified by the given IDs. 67 CONTENT_EXPORT void AddPendingTransitionRequestData( 68 int render_process_id, 69 int render_frame_id, 70 const std::string& allowed_destination_host_pattern, 71 const std::string& css_selector, 72 const std::string& markup); 73 74 void ClearPendingTransitionRequestData(int render_process_id, 75 int render_frame_id); 76 77 private: 78 class TransitionRequestData { 79 public: 80 TransitionRequestData(); 81 ~TransitionRequestData(); 82 void AddEntry(const std::string& allowed_destination_host_pattern, 83 const std::string& selector, 84 const std::string& markup); 85 bool FindEntry(const GURL& request_url, 86 TransitionLayerData* transition_data); 87 88 private: 89 struct AllowedEntry { 90 // These strings could have originated from a compromised renderer, 91 // and should not be trusted or assumed safe. They are only used within 92 // a sandboxed iframe with scripts disabled. 93 std::string allowed_destination_host_pattern; 94 std::string css_selector; 95 std::string markup; 96 97 AllowedEntry(const std::string& allowed_destination_host_pattern, 98 const std::string& css_selector, 99 const std::string& markup) : 100 allowed_destination_host_pattern(allowed_destination_host_pattern), 101 css_selector(css_selector), 102 markup(markup) {} 103 }; 104 std::vector<AllowedEntry> allowed_entries_; 105 }; 106 107 friend struct DefaultSingletonTraits<TransitionRequestManager>; 108 typedef std::map<std::pair<int, int>, TransitionRequestData> 109 RenderFrameRequestDataMap; 110 111 TransitionRequestManager(); 112 ~TransitionRequestManager(); 113 114 // Map of (render_process_host_id, render_frame_id) pairs of all 115 // RenderFrameHosts that have pending cross-site requests and their data. 116 // Used to pass information to the CrossSiteResourceHandler without doing a 117 // round-trip between IO->UI->IO threads. 118 RenderFrameRequestDataMap pending_transition_frames_; 119 120 DISALLOW_COPY_AND_ASSIGN(TransitionRequestManager); 121 }; 122 123 } // namespace content 124 125 #endif // CONTENT_BROWSER_TRANSITION_REQUEST_MANAGER_H_ 126