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