blob: 71375ddcfc7460a27cd84675e68b9499cfd7ff19 [file] [log] [blame]
[email protected]a7ff4b72013-10-17 20:56:021// Copyright 2013 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/extensions/extension_util.h"
6
7#include "base/command_line.h"
8#include "chrome/browser/extensions/extension_prefs.h"
9#include "chrome/browser/extensions/extension_service.h"
10#include "chrome/common/chrome_switches.h"
11#include "chrome/common/extensions/extension.h"
12#include "chrome/common/extensions/incognito_handler.h"
13#include "chrome/common/extensions/sync_helper.h"
14#include "extensions/common/manifest.h"
15
16using extensions::Extension;
17using extensions::ExtensionPrefs;
18
19namespace extension_util {
20
21bool IsIncognitoEnabled(const std::string& extension_id,
22 const ExtensionService* service) {
23 if (!service)
24 return false;
25
26 const Extension* extension = service->GetInstalledExtension(extension_id);
27 if (extension && !extension->can_be_incognito_enabled())
28 return false;
29 // If this is an existing component extension we always allow it to
30 // work in incognito mode.
31 if (extension && extension->location() == extensions::Manifest::COMPONENT)
32 return true;
33 if (extension && extension->force_incognito_enabled())
34 return true;
35
36 // Check the prefs.
37 return service->extension_prefs()->IsIncognitoEnabled(extension_id);
38}
39
40void SetIsIncognitoEnabled(const std::string& extension_id,
41 ExtensionService* service,
42 bool enabled) {
43 const Extension* extension = service->GetInstalledExtension(extension_id);
44 if (extension && !extension->can_be_incognito_enabled())
45 return;
46 if (extension && extension->location() == extensions::Manifest::COMPONENT) {
47 // This shouldn't be called for component extensions unless it is called
48 // by sync, for syncable component extensions.
49 // See http://crbug.com/112290 and associated CLs for the sordid history.
50 DCHECK(extensions::sync_helper::IsSyncable(extension));
51
52 // If we are here, make sure the we aren't trying to change the value.
53 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, service));
54 return;
55 }
56
57 ExtensionPrefs* extension_prefs = service->extension_prefs();
58 // Broadcast unloaded and loaded events to update browser state. Only bother
59 // if the value changed and the extension is actually enabled, since there is
60 // no UI otherwise.
61 bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id);
62 if (enabled == old_enabled)
63 return;
64
65 extension_prefs->SetIsIncognitoEnabled(extension_id, enabled);
66
67 bool extension_is_enabled = service->extensions()->Contains(extension_id);
68
69 // When we reload the extension the ID may be invalidated if we've passed it
70 // by const ref everywhere. Make a copy to be safe.
71 std::string id = extension_id;
72 if (extension_is_enabled)
73 service->ReloadExtension(id);
74
75 // Reloading the extension invalidates the |extension| pointer.
76 extension = service->GetInstalledExtension(id);
77 if (extension)
78 service->SyncExtensionChangeIfNeeded(*extension);
79}
80
81bool CanCrossIncognito(const Extension* extension,
82 const ExtensionService* service) {
83 // We allow the extension to see events and data from another profile iff it
84 // uses "spanning" behavior and it has incognito access. "split" mode
85 // extensions only see events for a matching profile.
86 CHECK(extension);
87 return extension_util::IsIncognitoEnabled(extension->id(), service) &&
88 !extensions::IncognitoInfo::IsSplitMode(extension);
89}
90
91bool CanLoadInIncognito(const Extension* extension,
92 const ExtensionService* service) {
93 if (extension->is_hosted_app())
94 return true;
95 // Packaged apps and regular extensions need to be enabled specifically for
96 // incognito (and split mode should be set).
97 return extensions::IncognitoInfo::IsSplitMode(extension) &&
98 extension_util::IsIncognitoEnabled(extension->id(), service);
99}
100
101bool AllowFileAccess(const Extension* extension,
102 const ExtensionService* service) {
103 return (CommandLine::ForCurrentProcess()->HasSwitch(
104 switches::kDisableExtensionsFileAccessCheck) ||
105 service->extension_prefs()->AllowFileAccess(extension->id()));
106}
107
108void SetAllowFileAccess(const Extension* extension,
109 ExtensionService* service,
110 bool allow) {
111 // Reload to update browser state. Only bother if the value changed and the
112 // extension is actually enabled, since there is no UI otherwise.
113 bool old_allow = AllowFileAccess(extension, service);
114 if (allow == old_allow)
115 return;
116
117 service->extension_prefs()->SetAllowFileAccess(extension->id(), allow);
118
119 bool extension_is_enabled = service->extensions()->Contains(extension->id());
120 if (extension_is_enabled)
121 service->ReloadExtension(extension->id());
122}
123
124} // namespace extension_util