1 // Copyright 2014 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 "mojo/shell/mojo_url_resolver.h" 6 7 #include "base/base_paths.h" 8 #include "base/files/file_path.h" 9 #include "base/logging.h" 10 #include "base/path_service.h" 11 #include "net/base/filename_util.h" 12 #include "url/url_util.h" 13 14 namespace mojo { 15 namespace shell { 16 namespace { 17 MakeSharedLibraryName(const std::string & host_name)18std::string MakeSharedLibraryName(const std::string& host_name) { 19 #if defined(OS_WIN) 20 return host_name + ".dll"; 21 #elif defined(OS_LINUX) || defined(OS_ANDROID) 22 return "lib" + host_name + ".so"; 23 #elif defined(OS_MACOSX) 24 return "lib" + host_name + ".dylib"; 25 #else 26 NOTREACHED() << "dynamic loading of services not supported"; 27 return std::string(); 28 #endif 29 } 30 31 } // namespace 32 MojoURLResolver()33MojoURLResolver::MojoURLResolver() { 34 // Needed to treat first component of mojo URLs as host, not path. 35 url::AddStandardScheme("mojo"); 36 } 37 ~MojoURLResolver()38MojoURLResolver::~MojoURLResolver() { 39 } 40 AddCustomMapping(const GURL & mojo_url,const GURL & resolved_url)41void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, 42 const GURL& resolved_url) { 43 url_map_[mojo_url] = resolved_url; 44 } 45 AddLocalFileMapping(const GURL & mojo_url)46void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { 47 local_file_set_.insert(mojo_url); 48 } 49 Resolve(const GURL & mojo_url) const50GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { 51 std::map<GURL, GURL>::const_iterator it = url_map_.find(mojo_url); 52 if (it != url_map_.end()) 53 return it->second; 54 55 std::string lib = MakeSharedLibraryName(mojo_url.host()); 56 57 if (local_file_set_.find(mojo_url) != local_file_set_.end()) { 58 // Resolve to a local file URL. 59 base::FilePath path; 60 #if defined(OS_ANDROID) 61 // On Android, additional lib are bundled. 62 PathService::Get(base::DIR_MODULE, &path); 63 #else 64 PathService::Get(base::DIR_EXE, &path); 65 #if !defined(OS_WIN) 66 path = path.Append(FILE_PATH_LITERAL("lib")); 67 #endif // !defined(OS_WIN) 68 #endif // defined(OS_ANDROID) 69 path = path.Append(base::FilePath::FromUTF8Unsafe(lib)); 70 return net::FilePathToFileURL(path); 71 } 72 73 // Otherwise, resolve to an URL relative to origin_. 74 return GURL(origin_ + "/" + lib); 75 } 76 77 } // namespace shell 78 } // namespace mojo 79