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