1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_CHROMEOS_PLUGIN_SELECTION_POLICY_H_
6#define CHROME_BROWSER_CHROMEOS_PLUGIN_SELECTION_POLICY_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/gtest_prod_util.h"
13#include "base/memory/ref_counted.h"
14#include "webkit/plugins/npapi/webplugininfo.h"
15
16class GURL;
17class FilePath;
18
19namespace chromeos {
20
21#if !defined(OS_CHROMEOS)
22#error This file is meant to be compiled on ChromeOS only.
23#endif
24
25// This class is used to provide logic for selection of a plugin
26// executable path in the browser.  It loads a policy file for
27// selection of particular plugins based on the domain they are be
28// instantiated for.  It is used by the PluginService.  It is (and
29// should be) only used for ChromeOS.
30
31// The functions in this class must only be called on the FILE thread
32// (and will DCHECK if they aren't).
33
34class PluginSelectionPolicy
35    : public base::RefCountedThreadSafe<PluginSelectionPolicy> {
36 public:
37  PluginSelectionPolicy();
38
39  // This should be called before any other method.  This starts the
40  // process of initialization on the FILE thread.
41  void StartInit();
42
43  // Returns the first allowed plugin in the given vector of plugin
44  // information.  Returns -1 if no plugins in the info vector are
45  // allowed (or if the info vector is empty).  InitFromFile must
46  // complete before any calls to FindFirstAllowed happen or it will
47  // assert.
48  int FindFirstAllowed(const GURL& url,
49                       const std::vector<webkit::npapi::WebPluginInfo>& info);
50
51  // Applies the current policy to the given path using the url to
52  // look up what the policy for that domain is.  Returns true if the
53  // given plugin is allowed for that domain.  InitFromFile must
54  // complete before any calls to IsAllowed happen or it will assert.
55  bool IsAllowed(const GURL& url, const FilePath& path);
56
57 private:
58  // To allow access to InitFromFile
59  FRIEND_TEST_ALL_PREFIXES(PluginSelectionPolicyTest, Basic);
60  FRIEND_TEST_ALL_PREFIXES(PluginSelectionPolicyTest, FindFirstAllowed);
61  FRIEND_TEST_ALL_PREFIXES(PluginSelectionPolicyTest, InitFromFile);
62  FRIEND_TEST_ALL_PREFIXES(PluginSelectionPolicyTest, IsAllowed);
63  FRIEND_TEST_ALL_PREFIXES(PluginSelectionPolicyTest, MissingFile);
64
65  // Initializes from the default policy file.
66  bool Init();
67
68  // Initializes from the given file.
69  bool InitFromFile(const FilePath& policy_file);
70
71  typedef std::vector<std::pair<bool, std::string> > Policy;
72  typedef std::map<std::string, Policy> PolicyMap;
73
74  PolicyMap policies_;
75
76  // This is used to DCHECK if we try and call IsAllowed or
77  // FindFirstAllowed before we've finished executing InitFromFile.
78  // Note: We're "finished" even if loading the file fails -- the
79  // point of the DCHECK is to make sure we haven't violated our
80  // ordering/threading assumptions, not to make sure that we're
81  // properly initialized.
82  bool init_from_file_finished_;
83
84  DISALLOW_COPY_AND_ASSIGN(PluginSelectionPolicy);
85};
86
87}  // namespace chromeos
88#endif  // CHROME_BROWSER_CHROMEOS_PLUGIN_SELECTION_POLICY_H_
89