OLD | NEW |
(Empty) | |
| 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 |
| 13 namespace mojo { |
| 14 namespace shell { |
| 15 namespace { |
| 16 |
| 17 std::string MakeSharedLibraryName(const std::string& file_name) { |
| 18 #if defined(OS_WIN) |
| 19 return file_name + ".dll"; |
| 20 #elif defined(OS_LINUX) |
| 21 return "lib" + file_name + ".so"; |
| 22 #elif defined(OS_MACOSX) |
| 23 return "lib" + file_name + ".dylib"; |
| 24 #else |
| 25 NOTREACHED() << "dynamic loading of services not supported"; |
| 26 return std::string(); |
| 27 #endif |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 MojoURLResolver::MojoURLResolver() { |
| 33 } |
| 34 |
| 35 MojoURLResolver::~MojoURLResolver() { |
| 36 } |
| 37 |
| 38 void MojoURLResolver::AddCustomMapping(const GURL& mojo_url, |
| 39 const GURL& resolved_url) { |
| 40 url_map_[mojo_url] = resolved_url; |
| 41 } |
| 42 |
| 43 void MojoURLResolver::AddLocalFileMapping(const GURL& mojo_url) { |
| 44 local_file_set_.insert(mojo_url); |
| 45 } |
| 46 |
| 47 GURL MojoURLResolver::Resolve(const GURL& mojo_url) const { |
| 48 std::map<GURL, GURL>::const_iterator it = url_map_.find(mojo_url); |
| 49 if (it != url_map_.end()) |
| 50 return it->second; |
| 51 |
| 52 std::string lib = MakeSharedLibraryName(mojo_url.ExtractFileName()); |
| 53 |
| 54 if (local_file_set_.find(mojo_url) != local_file_set_.end()) { |
| 55 // Resolve to a local file URL. |
| 56 base::FilePath path; |
| 57 PathService::Get(base::DIR_EXE, &path); |
| 58 #if !defined(OS_WIN) |
| 59 path = path.Append(FILE_PATH_LITERAL("lib")); |
| 60 #endif |
| 61 path = path.Append(base::FilePath::FromUTF8Unsafe(lib)); |
| 62 return net::FilePathToFileURL(path); |
| 63 } |
| 64 |
| 65 // Otherwise, resolve to an URL relative to origin_. |
| 66 return GURL(origin_ + "/" + lib); |
| 67 } |
| 68 |
| 69 } // namespace shell |
| 70 } // namespace mojo |
OLD | NEW |