extension_util.h revision 513209b27ff55e2841eac0e4120199c23acce758
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#ifndef CHROME_BROWSER_SYNC_GLUE_EXTENSION_UTIL_H_
6#define CHROME_BROWSER_SYNC_GLUE_EXTENSION_UTIL_H_
7#pragma once
8
9// This file contains some low-level utility functions used by
10// extensions sync.
11
12#include <set>
13#include <string>
14
15class Extension;
16class ExtensionPrefs;
17class ExtensionTypeSet;
18class ExtensionsService;
19struct UninstalledExtensionInfo;
20
21namespace sync_pb {
22class ExtensionSpecifics;
23}  // sync_pb
24
25namespace browser_sync {
26
27enum ExtensionType {
28  THEME,
29  EXTENSION,
30  // A converted user script that does not have an update URL.
31  LOCAL_USER_SCRIPT,
32  // A converted user script that has an update URL.  (We don't have a
33  // similar distinction for "regular" extensions as an empty update
34  // URL is interpreted to mean the default update URL [i.e., the
35  // extensions gallery].)
36  UPDATEABLE_USER_SCRIPT,
37  APP,
38};
39
40typedef std::set<ExtensionType> ExtensionTypeSet;
41
42// Returns the type of the given extension.
43//
44// TODO(akalin): Might be useful to move this into extension.cc.
45ExtensionType GetExtensionType(const Extension& extension);
46
47// TODO(akalin): Remove this once we unify ExtensionType and simply
48// have an ExtensionType member in UninstalledExtensionInfo.
49ExtensionType GetExtensionTypeFromUninstalledExtensionInfo(
50    const UninstalledExtensionInfo& uninstalled_extension_info);
51
52// Returns whether or not the given extension is one we want to sync.
53bool IsExtensionValid(const Extension& extension);
54
55// Returns whether or not the given extension is one we want to sync.
56bool IsExtensionValidAndSyncable(const Extension& extension,
57                                 const ExtensionTypeSet& allowed_types);
58
59// Stringifies the given ExtensionSpecifics.
60std::string ExtensionSpecificsToString(
61    const sync_pb::ExtensionSpecifics& specifics);
62
63// Returns whether or not the values of the given specifics are valid,
64// in particular the id, version, and update URL.
65bool IsExtensionSpecificsValid(
66    const sync_pb::ExtensionSpecifics& specifics);
67
68// Equivalent to DCHECK(IsExtensionSpecificsValid(specifics)) <<
69// ExtensionSpecificsToString(specifics);
70void DcheckIsExtensionSpecificsValid(
71    const sync_pb::ExtensionSpecifics& specifics);
72
73// Returns true iff two ExtensionSpecifics denote the same extension
74// state.  Neither |a| nor |b| need to be valid.
75bool AreExtensionSpecificsEqual(const sync_pb::ExtensionSpecifics& a,
76                                const sync_pb::ExtensionSpecifics& b);
77
78// Returns true iff the given ExtensionSpecifics is equal to the empty
79// ExtensionSpecifics object.  |specifics| does not have to be valid
80// and indeed, IsExtensionSpecificsValid(specifics) ->
81// !IsExtensionSpecificsUnset(specifics).
82bool IsExtensionSpecificsUnset(
83    const sync_pb::ExtensionSpecifics& specifics);
84
85// Copies the user properties from |specifics| into |dest_specifics|.
86// User properties are properties that are set by the user, i.e. not
87// inherent to the extension.  Currently they include |enabled| and
88// |incognito_enabled|.  Neither parameter need be valid.
89void CopyUserProperties(
90    const sync_pb::ExtensionSpecifics& specifics,
91    sync_pb::ExtensionSpecifics* dest_specifics);
92
93// Copies everything but non-user properties.  Neither parameter need
94// be valid.
95void CopyNonUserProperties(
96    const sync_pb::ExtensionSpecifics& specifics,
97    sync_pb::ExtensionSpecifics* dest_specifics);
98
99// Returns true iff two ExtensionSpecifics have the same user
100// properties.  Neither |a| nor |b| need to be valid.
101bool AreExtensionSpecificsUserPropertiesEqual(
102    const sync_pb::ExtensionSpecifics& a,
103    const sync_pb::ExtensionSpecifics& b);
104
105// Returns true iff two ExtensionSpecifics have the same non-user
106// properties.  Neither |a| nor |b| need to be valid.
107bool AreExtensionSpecificsNonUserPropertiesEqual(
108    const sync_pb::ExtensionSpecifics& a,
109    const sync_pb::ExtensionSpecifics& b);
110
111// Fills |specifics| with information taken from |extension|, which
112// must be a syncable extension.  |specifics| will be valid after this
113// function is called.
114void GetExtensionSpecifics(const Extension& extension,
115                           ExtensionPrefs* extension_prefs,
116                           sync_pb::ExtensionSpecifics* specifics);
117
118// Exposed only for testing.  Pre- and post-conditions are the same as
119// GetExtensionSpecifics().
120void GetExtensionSpecificsHelper(const Extension& extension,
121                                 bool enabled, bool incognito_enabled,
122                                 sync_pb::ExtensionSpecifics* specifics);
123
124// Returns whether or not the extension should be updated according to
125// the specifics.  |extension| must be syncable and |specifics| must
126// be valid.
127bool IsExtensionOutdated(const Extension& extension,
128                         const sync_pb::ExtensionSpecifics& specifics);
129
130// Sets properties of |extension| according to the information in
131// specifics.  |extension| must be syncable and |specifics| must be
132// valid.
133void SetExtensionProperties(
134    const sync_pb::ExtensionSpecifics& specifics,
135    ExtensionsService* extensions_service, const Extension* extension);
136
137// Merge |specifics| into |merged_specifics|.  Both must be valid and
138// have the same ID.  The merge policy is currently to copy the
139// non-user properties of |specifics| into |merged_specifics| (and the
140// user properties if |merge_user_properties| is set) if |specifics|
141// has a more recent or the same version as |merged_specifics|.
142void MergeExtensionSpecifics(
143    const sync_pb::ExtensionSpecifics& specifics,
144    bool merge_user_properties,
145    sync_pb::ExtensionSpecifics* merged_specifics);
146
147}  // namespace browser_sync
148
149#endif  // CHROME_BROWSER_SYNC_GLUE_EXTENSION_UTIL_H_
150