OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "chrome/browser/chromeos/plugin_selection_policy.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <iostream> |
| 9 #include <map> |
| 10 #include <sstream> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/file_path.h" |
| 15 #include "base/file_util.h" |
| 16 #include "base/logging.h" |
| 17 #include "base/string_util.h" |
| 18 #include "chrome/browser/browser_thread.h" |
| 19 #include "googleurl/src/gurl.h" |
| 20 |
| 21 #if !defined(OS_CHROMEOS) |
| 22 #error This file is meant to be compiled on ChromeOS only. |
| 23 #endif |
| 24 |
| 25 using std::vector; |
| 26 using std::string; |
| 27 using std::pair; |
| 28 using std::map; |
| 29 |
| 30 namespace chromeos { |
| 31 |
| 32 static const char kPluginSelectionPolicyFile[] = |
| 33 "/usr/share/chromeos-assets/flash/plugin_policy"; |
| 34 |
| 35 PluginSelectionPolicy::PluginSelectionPolicy() : initialized_(false) { |
| 36 } |
| 37 |
| 38 void PluginSelectionPolicy::StartInit() { |
| 39 // Initialize the policy on the FILE thread, since it reads from a |
| 40 // policy file. |
| 41 BrowserThread::PostTask( |
| 42 BrowserThread::FILE, FROM_HERE, |
| 43 NewRunnableMethod(this, &chromeos::PluginSelectionPolicy::Init)); |
| 44 } |
| 45 |
| 46 bool PluginSelectionPolicy::Init() { |
| 47 return InitFromFile(FilePath(kPluginSelectionPolicyFile)); |
| 48 } |
| 49 |
| 50 bool PluginSelectionPolicy::InitFromFile(const FilePath& policy_file) { |
| 51 // This must always be called from the FILE thread. |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 53 |
| 54 string data; |
| 55 // This should be a really small file, so we're OK with just |
| 56 // slurping it. |
| 57 if (!file_util::ReadFileToString(policy_file, &data)) { |
| 58 LOG(ERROR) << "Unable to read plugin policy file \"" |
| 59 << policy_file.value() << "\"."; |
| 60 return false; |
| 61 } |
| 62 |
| 63 std::istringstream input_stream(data); |
| 64 string line; |
| 65 map<string, Policy> policies; |
| 66 Policy policy; |
| 67 string last_plugin; |
| 68 |
| 69 while (std::getline(input_stream, line)) { |
| 70 // Strip comments. |
| 71 string::size_type pos = line.find("#"); |
| 72 if (pos != string::npos) { |
| 73 line = line.substr(0, pos); |
| 74 } |
| 75 TrimWhitespaceASCII(line, TRIM_ALL, &line); |
| 76 if (line.find("allow") == 0) { |
| 77 // Has to be preceeded by a "plugin" statement. |
| 78 if (last_plugin.empty()) { |
| 79 LOG(ERROR) << "Plugin policy file error: 'allow' out of context."; |
| 80 return false; |
| 81 } |
| 82 line = line.substr(6); |
| 83 TrimWhitespaceASCII(line, TRIM_ALL, &line); |
| 84 line = StringToLowerASCII(line); |
| 85 policy.push_back(make_pair(true, line)); |
| 86 } |
| 87 if (line.find("deny") == 0) { |
| 88 // Has to be preceeded by a "plugin" statement. |
| 89 if (last_plugin.empty()) { |
| 90 LOG(ERROR) << "Plugin policy file error: 'deny' out of context."; |
| 91 return false; |
| 92 } |
| 93 line = line.substr(5); |
| 94 TrimWhitespaceASCII(line, TRIM_ALL, &line); |
| 95 line = StringToLowerASCII(line); |
| 96 policy.push_back(make_pair(false, line)); |
| 97 } |
| 98 if (line.find("plugin") == 0) { |
| 99 line = line.substr(7); |
| 100 TrimWhitespaceASCII(line, TRIM_ALL, &line); |
| 101 if (!policy.empty() && !last_plugin.empty()) |
| 102 policies.insert(make_pair(last_plugin, policy)); |
| 103 last_plugin = line; |
| 104 policy.clear(); |
| 105 } |
| 106 } |
| 107 |
| 108 if (!last_plugin.empty()) |
| 109 policies.insert(make_pair(last_plugin, policy)); |
| 110 |
| 111 policies_.swap(policies); |
| 112 initialized_ = true; |
| 113 return true; |
| 114 } |
| 115 |
| 116 int PluginSelectionPolicy::FindFirstAllowed( |
| 117 const GURL& url, |
| 118 const std::vector<WebPluginInfo>& info) { |
| 119 for (std::vector<WebPluginInfo>::size_type i = 0; i < info.size(); ++i) { |
| 120 if (IsAllowed(url, info[i].path)) |
| 121 return i; |
| 122 } |
| 123 return -1; |
| 124 } |
| 125 |
| 126 bool PluginSelectionPolicy::IsAllowed(const GURL& url, |
| 127 const FilePath& path) { |
| 128 // This must always be called from the FILE thread, to be sure |
| 129 // initialization doesn't happen at the same time. |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 131 |
| 132 // Make sure that we notice if this starts being called before |
| 133 // initialization is complete. Right now it is guaranteed only by |
| 134 // the startup order and the fact that InitFromFile runs on the FILE |
| 135 // thread too. |
| 136 DCHECK(initialized_) << "Tried to check policy before policy is initialized."; |
| 137 |
| 138 string name = path.BaseName().value(); |
| 139 |
| 140 PolicyMap::iterator policy_iter = policies_.find(name); |
| 141 if (policy_iter != policies_.end()) { |
| 142 Policy& policy(policy_iter->second); |
| 143 |
| 144 // We deny by default. (equivalent to "deny" at the top of the section) |
| 145 bool allow = false; |
| 146 |
| 147 for (Policy::iterator iter = policy.begin(); iter != policy.end(); ++iter) { |
| 148 bool policy_allow = iter->first; |
| 149 string& policy_domain = iter->second; |
| 150 if (policy_domain.empty() || url.DomainIs(policy_domain.c_str(), |
| 151 policy_domain.size())) { |
| 152 allow = policy_allow; |
| 153 } |
| 154 } |
| 155 return allow; |
| 156 } |
| 157 |
| 158 // If it's not in the policy file, then we assume it's OK to allow |
| 159 // it. |
| 160 return true; |
| 161 } |
| 162 |
| 163 } // namespace chromeos |
OLD | NEW |