OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/bookmark_app_helper.h" | |
6 | |
7 #include "base/run_loop.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "chrome/app/chrome_command_ids.h" | |
10 #include "chrome/browser/extensions/test_bookmark_app_helper.h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/browser_commands.h" | |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
14 #include "chrome/browser/ui/test/test_browser_dialog.h" | |
15 #include "chrome/common/render_messages.h" | |
16 #include "content/public/browser/browser_message_filter.h" | |
17 #include "content/public/browser/render_process_host.h" | |
18 #include "content/public/browser/render_widget_host.h" | |
19 #include "content/public/browser/render_widget_host_view.h" | |
20 #include "content/public/browser/web_contents.h" | |
21 | |
22 namespace extensions { | |
23 namespace { | |
24 | |
25 content::RenderWidgetHost* GetActiveRenderWidgetHost(Browser* browser) { | |
26 return browser->tab_strip_model() | |
27 ->GetActiveWebContents() | |
28 ->GetRenderWidgetHostView() | |
29 ->GetRenderWidgetHost(); | |
30 } | |
31 | |
32 // Extends BookmarkAppHelper to see the call to OnIconsDownloaded. | |
33 class TestBookmarkAppHelperImpl : public test::TestBookmarkAppHelper { | |
34 public: | |
35 TestBookmarkAppHelperImpl(Profile* profile, | |
36 WebApplicationInfo web_app_info, | |
37 content::WebContents* contents, | |
38 base::Closure on_icons_downloaded_closure) | |
39 : TestBookmarkAppHelper(profile, web_app_info, contents), | |
40 on_icons_downloaded_closure_(on_icons_downloaded_closure) {} | |
41 | |
42 // TestBookmarkAppHelper: | |
43 void OnIconsDownloaded( | |
44 bool success, | |
45 const std::map<GURL, std::vector<SkBitmap>>& bitmaps) override { | |
46 on_icons_downloaded_closure_.Run(); | |
47 TestBookmarkAppHelper::OnIconsDownloaded(success, bitmaps); | |
48 } | |
49 | |
50 private: | |
51 base::Closure on_icons_downloaded_closure_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(TestBookmarkAppHelperImpl); | |
54 }; | |
55 | |
56 // Intercepts the ChromeViewHostMsg_DidGetWebApplicationInfo that would usually | |
57 // get sent to extensions::TabHelper to create a BookmarkAppHelper that lets us | |
58 // detect when icons are downloaded and the dialog is ready to show. | |
59 class WebAppReadyMsgWatcher : public content::BrowserMessageFilter { | |
60 public: | |
61 explicit WebAppReadyMsgWatcher(Browser* browser) | |
62 : BrowserMessageFilter(ChromeMsgStart), browser_(browser) {} | |
63 | |
64 content::WebContents* web_contents() { | |
65 return browser_->tab_strip_model()->GetActiveWebContents(); | |
66 } | |
67 | |
68 void OnDidGetWebApplicationInfo(const WebApplicationInfo& const_info) { | |
69 WebApplicationInfo info = const_info; | |
70 // Mimic extensions::TabHelper for fields missing from the manifest. | |
71 if (info.app_url.is_empty()) | |
72 info.app_url = web_contents()->GetURL(); | |
73 if (info.title.empty()) | |
74 info.title = web_contents()->GetTitle(); | |
75 if (info.title.empty()) | |
76 info.title = base::UTF8ToUTF16(info.app_url.spec()); | |
77 | |
78 bookmark_app_helper_ = base::MakeUnique<TestBookmarkAppHelperImpl>( | |
79 browser_->profile(), info, web_contents(), quit_closure_); | |
80 bookmark_app_helper_->Create( | |
81 base::Bind(&WebAppReadyMsgWatcher::FinishCreateBookmarkApp, | |
82 base::Unretained(this))); | |
benwells
2017/06/06 03:31:50
Why unretained?
tapted
2017/06/06 07:44:12
Good point :). I forgot this was scoped_refptr. So
| |
83 } | |
84 | |
85 void FinishCreateBookmarkApp(const Extension* extension, | |
86 const WebApplicationInfo& web_app_info) { | |
87 // ~WebAppReadyMsgWatcher() is called on the IO thread, but | |
88 // |bookmark_app_helper_| must be destroyed on the UI thread. | |
89 bookmark_app_helper_.reset(); | |
90 } | |
91 | |
92 void Wait() { | |
93 base::RunLoop run_loop; | |
94 quit_closure_ = run_loop.QuitClosure(); | |
95 run_loop.Run(); | |
96 } | |
97 | |
98 // BrowserMessageFilter: | |
99 void OverrideThreadForMessage(const IPC::Message& message, | |
100 content::BrowserThread::ID* thread) override { | |
101 if (message.type() == ChromeViewHostMsg_DidGetWebApplicationInfo::ID) | |
102 *thread = content::BrowserThread::UI; | |
103 } | |
104 | |
105 bool OnMessageReceived(const IPC::Message& message) override { | |
106 bool handled = true; | |
107 IPC_BEGIN_MESSAGE_MAP(WebAppReadyMsgWatcher, message) | |
108 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, | |
109 OnDidGetWebApplicationInfo) | |
110 IPC_MESSAGE_UNHANDLED(handled = false) | |
111 IPC_END_MESSAGE_MAP() | |
112 return handled; | |
113 } | |
114 | |
115 private: | |
116 ~WebAppReadyMsgWatcher() override {} | |
117 | |
118 Browser* browser_; | |
119 base::Closure quit_closure_; | |
120 std::unique_ptr<TestBookmarkAppHelperImpl> bookmark_app_helper_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(WebAppReadyMsgWatcher); | |
123 }; | |
124 | |
125 } // namespace | |
126 | |
127 class BookmarkAppHelperTest : public DialogBrowserTest { | |
128 public: | |
129 BookmarkAppHelperTest() {} | |
130 | |
131 // DialogBrowserTest: | |
132 void ShowDialog(const std::string& name) override { | |
133 ASSERT_TRUE(embedded_test_server()->Start()); | |
134 AddTabAtIndex( | |
135 1, | |
136 GURL(embedded_test_server()->GetURL("/favicon/page_with_favicon.html")), | |
137 ui::PAGE_TRANSITION_LINK); | |
138 | |
139 scoped_refptr<WebAppReadyMsgWatcher> filter = | |
140 new WebAppReadyMsgWatcher(browser()); | |
141 GetActiveRenderWidgetHost(browser())->GetProcess()->AddFilter(filter.get()); | |
142 chrome::ExecuteCommand(browser(), IDC_CREATE_HOSTED_APP); | |
143 filter->Wait(); | |
144 } | |
145 | |
146 private: | |
147 DISALLOW_COPY_AND_ASSIGN(BookmarkAppHelperTest); | |
148 }; | |
149 | |
150 IN_PROC_BROWSER_TEST_F(BookmarkAppHelperTest, InvokeDialog_create) { | |
151 RunDialog(); | |
152 } | |
153 | |
154 } // namespace extensions | |
OLD | NEW |