diff options
author | kalman <kalman@chromium.org> | 2015-09-18 10:21:58 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-09-18 17:22:38 +0000 |
commit | 6f984aed2ba46212d23d28a0f4ca075d142b6de4 (patch) | |
tree | 6f1de36c3e8702afbe0ae5236e32aa82eb004192 /extensions/test | |
parent | 7653d3b301093051d2ac6cf805f13aeb65d511fa (diff) | |
download | chromium_src-6f984aed2ba46212d23d28a0f4ca075d142b6de4.zip chromium_src-6f984aed2ba46212d23d28a0f4ca075d142b6de4.tar.gz chromium_src-6f984aed2ba46212d23d28a0f4ca075d142b6de4.tar.bz2 |
Finish the implementation of chrome.runtime.getBackgroundClient() for extension service workers.
Until now it assumed that the extension's background page was open. Now it uses
WakeEventPage IPC call to wake the event page if it's asleep. This required
making WakeEventPage thread safe, to be able to be called on a service worker
thread, then when the response comes from the browser, call back on that same
thread.
It was also incorrectly called chrome.getBackgroundClient() rather than
chrome.runtime.getBackgroundClient().
I also did a rewrite of the extension service worker tests to be better
reusable, and easier to add more tests. It was hard to test a closed event page
in the current framework. Then I added more tests.
BUG=501569, 532720
R=rdevlin.cronin@chromium.org
Review URL: https://codereview.chromium.org/1344243003
Cr-Commit-Position: refs/heads/master@{#349700}
Diffstat (limited to 'extensions/test')
-rw-r--r-- | extensions/test/background_page_watcher.cc | 77 | ||||
-rw-r--r-- | extensions/test/background_page_watcher.h | 57 |
2 files changed, 134 insertions, 0 deletions
diff --git a/extensions/test/background_page_watcher.cc b/extensions/test/background_page_watcher.cc new file mode 100644 index 0000000..4d80515 --- /dev/null +++ b/extensions/test/background_page_watcher.cc @@ -0,0 +1,77 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "extensions/test/background_page_watcher.h" + +#include "base/auto_reset.h" +#include "base/logging.h" +#include "base/run_loop.h" +#include "base/scoped_observer.h" +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/web_contents.h" +#include "extensions/browser/extension_host.h" +#include "extensions/browser/process_manager.h" +#include "extensions/common/extension.h" + +namespace extensions { + +BackgroundPageWatcher::BackgroundPageWatcher(ProcessManager* process_manager, + const Extension* extension) + : process_manager_(process_manager), + extension_id_(extension->id()), + is_waiting_for_open_(false), + is_waiting_for_close_(false) {} + +BackgroundPageWatcher::~BackgroundPageWatcher() {} + +void BackgroundPageWatcher::WaitForOpen() { + WaitForOpenState(true); +} + +void BackgroundPageWatcher::WaitForClose() { + WaitForOpenState(false); +} + +void BackgroundPageWatcher::WaitForOpenState(bool wait_for_open) { + if (IsBackgroundPageOpen() == wait_for_open) + return; + ScopedObserver<ProcessManager, ProcessManagerObserver> observer(this); + observer.Add(process_manager_); + bool* flag = wait_for_open ? &is_waiting_for_open_ : &is_waiting_for_close_; + base::AutoReset<bool> set_flag(flag, true); + base::RunLoop run_loop; + base::AutoReset<base::Closure> set_quit_run_loop(&quit_run_loop_, + run_loop.QuitClosure()); + run_loop.Run(); + DCHECK_EQ(wait_for_open, IsBackgroundPageOpen()); +} + +bool BackgroundPageWatcher::IsBackgroundPageOpen() { + ExtensionHost* host = + process_manager_->GetBackgroundHostForExtension(extension_id_); + if (!host) + return false; + content::RenderProcessHost* rph = + host->host_contents()->GetRenderProcessHost(); + return rph && rph->HasConnection(); +} + +void BackgroundPageWatcher::OnExtensionFrameRegistered( + const std::string& extension_id, + content::RenderFrameHost* rfh) { + if (is_waiting_for_open_ && extension_id == extension_id_ && + IsBackgroundPageOpen()) + quit_run_loop_.Run(); +} + +void BackgroundPageWatcher::OnExtensionFrameUnregistered( + const std::string& extension_id, + content::RenderFrameHost* rfh) { + if (is_waiting_for_close_ && extension_id == extension_id_ && + !IsBackgroundPageOpen()) + quit_run_loop_.Run(); +} + +} // namespace extensions diff --git a/extensions/test/background_page_watcher.h b/extensions/test/background_page_watcher.h new file mode 100644 index 0000000..0ef3825 --- /dev/null +++ b/extensions/test/background_page_watcher.h @@ -0,0 +1,57 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <string> + +#include "base/callback.h" +#include "base/macros.h" +#include "extensions/browser/process_manager_observer.h" + +namespace extensions { +class Extension; +class ExtensionHost; +class ProcessManager; + +// Observes the background page load state of an extension. Use this in tests +// which rely on a specific open or close state of a background page. +class BackgroundPageWatcher : public ProcessManagerObserver { + public: + BackgroundPageWatcher(ProcessManager* process_manager, + const Extension* extension); + + ~BackgroundPageWatcher(); + + // Returns when the background page is open. If the background page is + // already open, returns immediately. + void WaitForOpen(); + + // Returns when the background page is closed. If the background page is + // already closed, returns immediately. + void WaitForClose(); + + private: + // Returns when the background page has open state of |wait_for_open|. If the + // background page is already in that state, returns immediately. + void WaitForOpenState(bool wait_for_open); + + bool IsBackgroundPageOpen(); + + // ProcessManagerObserver: + void OnExtensionFrameRegistered( + const std::string& extension_id, + content::RenderFrameHost* render_frame_host) override; + void OnExtensionFrameUnregistered( + const std::string& extension_id, + content::RenderFrameHost* render_frame_host) override; + + ProcessManager* process_manager_; + const std::string extension_id_; + base::Closure quit_run_loop_; + bool is_waiting_for_open_; + bool is_waiting_for_close_; + + DISALLOW_COPY_AND_ASSIGN(BackgroundPageWatcher); +}; + +} // namespace extensions |