diff options
-rw-r--r-- | mojo/mojo.gyp | 1 | ||||
-rw-r--r-- | mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.cc | 11 | ||||
-rw-r--r-- | mojo/shell/BUILD.gn | 1 | ||||
-rw-r--r-- | mojo/shell/desktop/mojo_main.cc | 55 | ||||
-rw-r--r-- | mojo/shell/mojo_url_resolver.cc | 23 | ||||
-rw-r--r-- | mojo/shell/mojo_url_resolver.h | 11 | ||||
-rw-r--r-- | mojo/shell/mojo_url_resolver_unittest.cc | 32 | ||||
-rw-r--r-- | mojo/shell/switches.cc | 6 | ||||
-rw-r--r-- | mojo/shell/switches.h | 1 |
9 files changed, 111 insertions, 30 deletions
diff --git a/mojo/mojo.gyp b/mojo/mojo.gyp index 2ffb1e5..0f5e11b 100644 --- a/mojo/mojo.gyp +++ b/mojo/mojo.gyp @@ -282,6 +282,7 @@ 'shell/child_process_host_unittest.cc', 'shell/dynamic_application_loader_unittest.cc', 'shell/in_process_dynamic_service_runner_unittest.cc', + 'shell/mojo_url_resolver_unittest.cc', 'shell/shell_test_base.cc', 'shell/shell_test_base.h', 'shell/shell_test_base_unittest.cc', diff --git a/mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.cc b/mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.cc index 699876a..ac54417 100644 --- a/mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.cc +++ b/mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.cc @@ -5,7 +5,6 @@ #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" #include "base/bind.h" -#include "base/command_line.h" #include "base/message_loop/message_loop.h" #include "base/stl_util.h" #include "mojo/public/cpp/application/application_impl.h" @@ -93,16 +92,8 @@ class RootObserver : public ViewObserver { ViewManagerClientImpl::ViewManagerClientImpl(ViewManagerDelegate* delegate, Shell* shell) : connected_(false), connection_id_(0), next_id_(1), delegate_(delegate) { - // TODO(beng): Come up with a better way of establishing a configuration for - // what the active window manager is. - std::string window_manager_url = "mojo:mojo_window_manager"; - if (base::CommandLine::ForCurrentProcess()->HasSwitch("window-manager")) { - window_manager_url = - base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( - "window-manager"); - } InterfacePtr<ServiceProvider> sp; - shell->ConnectToApplication(window_manager_url, GetProxy(&sp)); + shell->ConnectToApplication("mojo:mojo_window_manager", GetProxy(&sp)); ConnectToService(sp.get(), &window_manager_); window_manager_.set_client(this); } diff --git a/mojo/shell/BUILD.gn b/mojo/shell/BUILD.gn index f405bc9..c435a75 100644 --- a/mojo/shell/BUILD.gn +++ b/mojo/shell/BUILD.gn @@ -148,6 +148,7 @@ test("mojo_shell_tests") { "child_process_host_unittest.cc", "dynamic_application_loader_unittest.cc", "in_process_dynamic_service_runner_unittest.cc", + "mojo_url_resolver_unittest.cc", "shell_test_base.cc", "shell_test_base.h", "shell_test_base_unittest.cc", diff --git a/mojo/shell/desktop/mojo_main.cc b/mojo/shell/desktop/mojo_main.cc index 6d4a6d9..4738e3c 100644 --- a/mojo/shell/desktop/mojo_main.cc +++ b/mojo/shell/desktop/mojo_main.cc @@ -15,6 +15,7 @@ #include "mojo/shell/child_process.h" #include "mojo/shell/context.h" #include "mojo/shell/init.h" +#include "mojo/shell/mojo_url_resolver.h" #include "mojo/shell/switches.h" #if defined(COMPONENT_BUILD) @@ -70,20 +71,38 @@ void RunApps(mojo::shell::Context* context) { void Usage() { std::cerr << "Launch Mojo applications.\n"; std::cerr - << "Usage: mojo_shell" - << " [--" << switches::kArgsFor << "=<mojo-app>]" - << " [--" << switches::kContentHandlers << "=<handlers>]" - << " [--" << switches::kEnableExternalApplications << "]" - << " [--" << switches::kDisableCache << "]" - << " [--" << switches::kEnableMultiprocess << "]" - << " [--" << switches::kOrigin << "=<url-lib-path>]" - << " <mojo-app> ...\n\n" - << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within quotes.\n" - << "Example: mojo_shell \"mojo://mojo_js_standalone test.js\".\n" - << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" - << "The value of <handlers> is a comma separated list like:\n" - << "text/html,mojo://mojo_html_viewer," - << "application/javascript,mojo://mojo_js_content_handler\n"; + << "Usage: mojo_shell" + << " [--" << switches::kArgsFor << "=<mojo-app>]" + << " [--" << switches::kContentHandlers << "=<handlers>]" + << " [--" << switches::kEnableExternalApplications << "]" + << " [--" << switches::kDisableCache << "]" + << " [--" << switches::kEnableMultiprocess << "]" + << " [--" << switches::kOrigin << "=<url-lib-path>]" + << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" + << " <mojo-app> ...\n\n" + << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " + << "quotes.\n" + << "Example: mojo_shell \"mojo://mojo_js_standalone test.js\".\n" + << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" + << "The value of <handlers> is a comma separated list like:\n" + << "text/html,mojo://mojo_html_viewer," + << "application/javascript,mojo://mojo_js_content_handler\n"; +} + +bool ConfigureURLMappings(const std::string& mappings, + mojo::shell::MojoURLResolver* resolver) { + base::StringPairs pairs; + if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) + return false; + using StringPair = std::pair<std::string, std::string>; + for (const StringPair& pair : pairs) { + const GURL from(pair.first); + const GURL to(pair.second); + if (!from.is_valid() || !to.is_valid()) + return false; + resolver->AddCustomMapping(from, to); + } + return true; } } // namespace @@ -130,6 +149,14 @@ int main(int argc, char** argv) { GURL(command_line.GetSwitchValueASCII(switches::kOrigin))); } + if (command_line.HasSwitch(switches::kURLMappings) && + !ConfigureURLMappings( + command_line.GetSwitchValueASCII(switches::kURLMappings), + shell_context.mojo_url_resolver())) { + Usage(); + return 0; + } + for (const auto& kv : command_line.GetSwitches()) { if (kv.first == switches::kArgsFor) GetAppURLAndSetArgs(kv.second, &shell_context); diff --git a/mojo/shell/mojo_url_resolver.cc b/mojo/shell/mojo_url_resolver.cc index c3998549..75c625c 100644 --- a/mojo/shell/mojo_url_resolver.cc +++ b/mojo/shell/mojo_url_resolver.cc @@ -70,14 +70,16 @@ void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { } GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { - std::map<GURL, GURL>::const_iterator it = url_map_.find(mojo_url); - if (it != url_map_.end()) - return it->second; + const GURL mapped_url(ApplyCustomMappings(mojo_url)); - std::string lib = MakeSharedLibraryName(mojo_url.host()); + // Continue resolving if the mapped url is a mojo: url. + if (mapped_url.scheme() != "mojo") + return mapped_url; + + std::string lib = MakeSharedLibraryName(mapped_url.host()); if (!base_url_.is_valid() || - local_file_set_.find(mojo_url) != local_file_set_.end()) { + local_file_set_.find(mapped_url) != local_file_set_.end()) { // Resolve to a local file URL. return default_base_url_.Resolve(lib); } @@ -86,5 +88,16 @@ GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { return base_url_.Resolve(lib); } +GURL MojoURLResolver::ApplyCustomMappings(const GURL& url) const { + GURL mapped_url(url); + for (;;) { + std::map<GURL, GURL>::const_iterator it = url_map_.find(mapped_url); + if (it == url_map_.end()) + break; + mapped_url = it->second; + } + return mapped_url; +} + } // namespace shell } // namespace mojo diff --git a/mojo/shell/mojo_url_resolver.h b/mojo/shell/mojo_url_resolver.h index ccd9bf4..b7145dd 100644 --- a/mojo/shell/mojo_url_resolver.h +++ b/mojo/shell/mojo_url_resolver.h @@ -8,6 +8,7 @@ #include <map> #include <set> +#include "base/basictypes.h" #include "url/gurl.h" namespace mojo { @@ -27,7 +28,8 @@ class MojoURLResolver { // inserted. void SetBaseURL(const GURL& base_url); - // Add a custom mapping for a particular "mojo:" URL. + // Add a custom mapping for a particular "mojo:" URL. If |resolved_url| is + // itself a mojo url normal resolution rules apply. void AddCustomMapping(const GURL& mojo_url, const GURL& resolved_url); // Add a local file mapping for a particular "mojo:" URL. This causes the @@ -39,10 +41,17 @@ class MojoURLResolver { GURL Resolve(const GURL& mojo_url) const; private: + // Applies all custom mappings for |url|, returning the last non-mapped url. + // For example, if 'a' maps to 'b' and 'b' maps to 'c' calling this with 'a' + // returns 'c'. + GURL ApplyCustomMappings(const GURL& url) const; + std::map<GURL, GURL> url_map_; std::set<GURL> local_file_set_; GURL default_base_url_; GURL base_url_; + + DISALLOW_COPY_AND_ASSIGN(MojoURLResolver); }; } // namespace shell diff --git a/mojo/shell/mojo_url_resolver_unittest.cc b/mojo/shell/mojo_url_resolver_unittest.cc new file mode 100644 index 0000000..e4deffa --- /dev/null +++ b/mojo/shell/mojo_url_resolver_unittest.cc @@ -0,0 +1,32 @@ +// Copyright 2014 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 "mojo/shell/mojo_url_resolver.h" + +#include "base/logging.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace mojo { +namespace shell { +namespace test { +namespace { + +typedef testing::Test MojoURLResolverTest; + +TEST_F(MojoURLResolverTest, MojoURLsFallThrough) { + MojoURLResolver resolver; + resolver.AddCustomMapping(GURL("mojo:test"), GURL("mojo:foo")); + const GURL base_url("file:/base"); + resolver.SetBaseURL(base_url); + const std::string resolved(resolver.Resolve(GURL("mojo:test")).spec()); + // Resolved must start with |base_url|. + EXPECT_EQ(base_url.spec(), resolved.substr(0, base_url.spec().size())); + // And must contain foo (which is what test mapped to. + EXPECT_NE(std::string::npos, resolved.find("foo")); +} + +} // namespace +} // namespace test +} // namespace shell +} // namespace mojo diff --git a/mojo/shell/switches.cc b/mojo/shell/switches.cc index 9bd3184..ee3ee51 100644 --- a/mojo/shell/switches.cc +++ b/mojo/shell/switches.cc @@ -43,4 +43,10 @@ const char kOrigin[] = "origin"; // message pipes and other activities. This is work in progress. const char kSpy[] = "spy"; +// Specifies a set of mappings to apply when resolving urls. The value is set of +// ',' separated mappings, where each mapping consists of a pair of urls giving +// the to/from url to map. For example, 'a=b,c=d' contains two mappings, the +// first maps 'a' to 'b' and the second 'c' to 'd'. +const char kURLMappings[] = "url-mappings"; + } // namespace switches diff --git a/mojo/shell/switches.h b/mojo/shell/switches.h index 7d4befd..3381eeb 100644 --- a/mojo/shell/switches.h +++ b/mojo/shell/switches.h @@ -19,6 +19,7 @@ extern const char kEnableExternalApplications[]; extern const char kEnableMultiprocess[]; extern const char kOrigin[]; extern const char kSpy[]; +extern const char kURLMappings[]; } // namespace switches |