OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "chrome/browser/android/webapps/single_tab_mode_tab_helper.h" | 5 #include "chrome/browser/android/webapps/single_tab_mode_tab_helper.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "chrome/browser/android/tab_android.h" | 8 #include "chrome/browser/android/tab_android.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/render_frame_host.h" | 10 #include "content/public/browser/render_frame_host.h" |
11 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
13 | 13 |
14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SingleTabModeTabHelper); | 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SingleTabModeTabHelper); |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // TODO(csharrison): This should be a content::GlobalFrameRoutingId. | |
18 typedef std::pair<int32_t, int32_t> RenderFrameHostID; | 19 typedef std::pair<int32_t, int32_t> RenderFrameHostID; |
19 typedef std::set<RenderFrameHostID> SingleTabIDSet; | 20 typedef std::set<RenderFrameHostID> SingleTabIDSet; |
20 base::LazyInstance<SingleTabIDSet>::DestructorAtExit g_blocked_ids = | 21 base::LazyInstance<SingleTabIDSet>::DestructorAtExit g_blocked_ids = |
21 LAZY_INSTANCE_INITIALIZER; | 22 LAZY_INSTANCE_INITIALIZER; |
22 | 23 |
23 void AddPairOnIOThread(int32_t process_id, int32_t frame_routing_id) { | |
24 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
25 RenderFrameHostID single_tab_pair(process_id, frame_routing_id); | |
26 g_blocked_ids.Get().insert(single_tab_pair); | |
27 } | |
28 | |
29 void RemovePairOnIOThread(int32_t process_id, int32_t frame_routing_id) { | |
30 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
31 RenderFrameHostID single_tab_pair(process_id, frame_routing_id); | |
32 SingleTabIDSet::iterator itr = g_blocked_ids.Get().find(single_tab_pair); | |
33 DCHECK(itr != g_blocked_ids.Get().end()); | |
34 g_blocked_ids.Get().erase(itr); | |
35 } | |
36 | |
37 void AddPair(content::RenderFrameHost* render_frame_host) { | 24 void AddPair(content::RenderFrameHost* render_frame_host) { |
38 if (!render_frame_host) | 25 if (!render_frame_host) |
39 return; | 26 return; |
40 | |
41 int32_t process_id = render_frame_host->GetProcess()->GetID(); | 27 int32_t process_id = render_frame_host->GetProcess()->GetID(); |
42 int32_t frame_routing_id = render_frame_host->GetRoutingID(); | 28 int32_t frame_routing_id = render_frame_host->GetRoutingID(); |
43 content::BrowserThread::PostTask( | 29 g_blocked_ids.Get().insert(RenderFrameHostID(process_id, frame_routing_id)); |
44 content::BrowserThread::IO, FROM_HERE, | |
45 base::Bind(&AddPairOnIOThread, process_id, frame_routing_id)); | |
46 } | 30 } |
47 | 31 |
48 void RemovePair(content::RenderFrameHost* render_frame_host) { | 32 void RemovePair(content::RenderFrameHost* render_frame_host) { |
49 if (!render_frame_host) | 33 if (!render_frame_host) |
50 return; | 34 return; |
51 | 35 |
52 int32_t process_id = render_frame_host->GetProcess()->GetID(); | 36 int32_t process_id = render_frame_host->GetProcess()->GetID(); |
53 int32_t frame_routing_id = render_frame_host->GetRoutingID(); | 37 int32_t frame_routing_id = render_frame_host->GetRoutingID(); |
54 content::BrowserThread::PostTask( | 38 RenderFrameHostID single_tab_pair(process_id, frame_routing_id); |
55 content::BrowserThread::IO, FROM_HERE, | 39 SingleTabIDSet::iterator itr = g_blocked_ids.Get().find(single_tab_pair); |
56 base::Bind(&RemovePairOnIOThread, process_id, frame_routing_id)); | 40 DCHECK(itr != g_blocked_ids.Get().end()); |
41 g_blocked_ids.Get().erase(itr); | |
57 } | 42 } |
58 | 43 |
59 } // namespace | 44 } // namespace |
60 | 45 |
61 SingleTabModeTabHelper::SingleTabModeTabHelper( | 46 SingleTabModeTabHelper::SingleTabModeTabHelper( |
62 content::WebContents* web_contents) | 47 content::WebContents* web_contents) |
63 : content::WebContentsObserver(web_contents), | 48 : content::WebContentsObserver(web_contents), |
64 block_all_new_windows_(false) { | 49 block_all_new_windows_(false) { |
65 } | 50 } |
66 | 51 |
(...skipping 15 matching lines...) Expand all Loading... | |
82 } | 67 } |
83 | 68 |
84 void SingleTabModeTabHelper::PermanentlyBlockAllNewWindows() { | 69 void SingleTabModeTabHelper::PermanentlyBlockAllNewWindows() { |
85 block_all_new_windows_ = true; | 70 block_all_new_windows_ = true; |
86 for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) | 71 for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) |
87 AddPair(frame); | 72 AddPair(frame); |
88 } | 73 } |
89 | 74 |
90 bool SingleTabModeTabHelper::IsRegistered(int32_t process_id, | 75 bool SingleTabModeTabHelper::IsRegistered(int32_t process_id, |
91 int32_t frame_routing_id) { | 76 int32_t frame_routing_id) { |
92 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
93 RenderFrameHostID single_tab_pair(process_id, frame_routing_id); | 78 RenderFrameHostID single_tab_pair(process_id, frame_routing_id); |
94 SingleTabIDSet::iterator itr = g_blocked_ids.Get().find(single_tab_pair); | 79 SingleTabIDSet::iterator itr = g_blocked_ids.Get().find(single_tab_pair); |
95 return itr != g_blocked_ids.Get().end(); | 80 return itr != g_blocked_ids.Get().end(); |
ncarter (slow)
2017/04/17 20:44:26
On the UI thread, I think we we don't need g_block
Charlie Harrison
2017/04/18 20:38:52
Looks good, done.
| |
96 } | 81 } |
97 | 82 |
98 void SingleTabModeTabHelper::HandleOpenUrl(const BlockedWindowParams& params) { | 83 void SingleTabModeTabHelper::HandleOpenUrl(const BlockedWindowParams& params) { |
99 TabAndroid* tab = TabAndroid::FromWebContents(web_contents()); | 84 TabAndroid* tab = TabAndroid::FromWebContents(web_contents()); |
100 if (!tab) | 85 if (!tab) |
101 return; | 86 return; |
102 | 87 |
103 chrome::NavigateParams nav_params = | 88 chrome::NavigateParams nav_params = |
104 params.CreateNavigateParams(web_contents()); | 89 params.CreateNavigateParams(web_contents()); |
105 tab->HandlePopupNavigation(&nav_params); | 90 tab->HandlePopupNavigation(&nav_params); |
106 } | 91 } |
OLD | NEW |