Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: chrome/browser/extensions/extension_util.cc

Issue 25366003: Moved some functions off ExtensionService into a new file extension_util. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Windows compile Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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
16 using extensions::Extension;
17 using extensions::ExtensionPrefs;
18
19 namespace extension_util {
20
21 bool 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
40 void 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 they are
Yoyo Zhou 2013/10/15 21:25:49 Can you add a reference to crbug.com/112290 here (
benwells 2013/10/17 06:56:55 Done.
48 // syncable.
49 DCHECK(extensions::sync_helper::IsSyncable(extension));
50
51 // If we are here, make sure the we aren't trying to change the value.
52 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id, service));
53 return;
54 }
55
56 ExtensionPrefs* extension_prefs = service->extension_prefs();
57 // Broadcast unloaded and loaded events to update browser state. Only bother
58 // if the value changed and the extension is actually enabled, since there is
59 // no UI otherwise.
60 bool old_enabled = extension_prefs->IsIncognitoEnabled(extension_id);
61 if (enabled == old_enabled)
62 return;
63
64 extension_prefs->SetIsIncognitoEnabled(extension_id, enabled);
65
66 bool extension_is_enabled = service->extensions()->Contains(extension_id);
67
68 // When we reload the extension the ID may be invalidated if we've passed it
69 // by const ref everywhere. Make a copy to be safe.
70 std::string id = extension_id;
71 if (extension_is_enabled)
72 service->ReloadExtension(id);
73
74 // Reloading the extension invalidates the |extension| pointer.
75 extension = service->GetInstalledExtension(id);
76 if (extension)
77 service->SyncExtensionChangeIfNeeded(*extension);
78 }
79
80 bool CanCrossIncognito(const Extension* extension,
81 const ExtensionService* service) {
82 // We allow the extension to see events and data from another profile iff it
83 // uses "spanning" behavior and it has incognito access. "split" mode
84 // extensions only see events for a matching profile.
85 CHECK(extension);
86 return extension_util::IsIncognitoEnabled(extension->id(),
Yoyo Zhou 2013/10/15 21:25:49 nit: extraneous line break
benwells 2013/10/17 06:56:55 Done.
87 service) &&
88 !extensions::IncognitoInfo::IsSplitMode(extension);
89 }
90
91 bool 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(),
Yoyo Zhou 2013/10/15 21:25:49 ditto
benwells 2013/10/17 06:56:55 Done.
99 service);
100 }
101
102 bool AllowFileAccess(const Extension* extension,
103 const ExtensionService* service) {
104 return (CommandLine::ForCurrentProcess()->HasSwitch(
105 switches::kDisableExtensionsFileAccessCheck) ||
106 service->extension_prefs()->AllowFileAccess(extension->id()));
107 }
108
109 void SetAllowFileAccess(const Extension* extension,
110 ExtensionService* service,
111 bool allow) {
112 // Reload to update browser state. Only bother if the value changed and the
113 // extension is actually enabled, since there is no UI otherwise.
114 bool old_allow = AllowFileAccess(extension, service);
115 if (allow == old_allow)
116 return;
117
118 service->extension_prefs()->SetAllowFileAccess(extension->id(), allow);
119
120 bool extension_is_enabled = service->extensions()->Contains(extension->id());
121 if (extension_is_enabled)
122 service->ReloadExtension(extension->id());
123 }
124
125 } // namespace extension_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698